|
| 1 | +#!/usr/bin/env node |
| 2 | +// @ts-check |
| 3 | + |
| 4 | +import fs from 'fs' |
| 5 | +import minimist from 'minimist' |
1 | 6 | import prompts from 'prompts'
|
2 |
| -import kolorist from 'kolorist' |
| 7 | +import { red, green, bold } from 'kolorist' |
3 | 8 |
|
| 9 | +import emptyDir from './emptyDir.js' |
4 | 10 | import renderTemplate from './renderTemplate.js'
|
| 11 | +import path from 'path' |
| 12 | + |
| 13 | +function isValidPackageName(projectName) { |
| 14 | + return /^(?:@[a-z0-9-*~][a-z0-9-*._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$/.test( |
| 15 | + projectName |
| 16 | + ) |
| 17 | +} |
| 18 | + |
| 19 | +function toValidPackageName(projectName) { |
| 20 | + return projectName |
| 21 | + .trim() |
| 22 | + .toLowerCase() |
| 23 | + .replace(/\s+/g, '-') |
| 24 | + .replace(/^[._]/, '') |
| 25 | + .replace(/[^a-z0-9-~]+/g, '-') |
| 26 | +} |
| 27 | + |
| 28 | +function canSafelyOverwrite(dir) { |
| 29 | + return !fs.existsSync(dir) || fs.readdirSync(dir).length === 0 |
| 30 | +} |
| 31 | + |
| 32 | +async function init() { |
| 33 | + const cwd = process.cwd() |
| 34 | + const argv = minimist(process.argv.slice(2)) |
| 35 | + |
| 36 | + let targetDir = argv._[0] |
| 37 | + const defaultProjectName = !targetDir ? 'vue-project' : targetDir |
| 38 | + |
| 39 | + let result = {} |
| 40 | + try { |
| 41 | + // Prompts: |
| 42 | + // - Project name: |
| 43 | + // - whether to overwrite the existing directory or not? |
| 44 | + // - enter a valid package name for package.json |
| 45 | + // - Project language: JavaScript / TypeScript |
| 46 | + // - Install Vue Router & Vuex for SPA development? |
| 47 | + // - Add Cypress for testing? |
| 48 | + result = await prompts([ |
| 49 | + { |
| 50 | + name: 'projectName', |
| 51 | + type: targetDir ? null : 'text', |
| 52 | + message: 'Project name:', |
| 53 | + initial: defaultProjectName, |
| 54 | + onState: (state) => |
| 55 | + (targetDir = String(state.value).trim() || defaultProjectName) |
| 56 | + }, |
| 57 | + { |
| 58 | + name: 'shouldOverwrite', |
| 59 | + type: () => canSafelyOverwrite(targetDir) ? null : 'confirm', |
| 60 | + message: () => { |
| 61 | + const dirForPrompt = targetDir === '.' |
| 62 | + ? 'Current directory' |
| 63 | + : `Target directory "${targetDir}"` |
| 64 | + |
| 65 | + return `${dirForPrompt} is not empty. Remove existing files and continue?` |
| 66 | + } |
| 67 | + }, |
| 68 | + { |
| 69 | + name: 'overwriteChecker', |
| 70 | + type: (prev, values = {}) => { |
| 71 | + if (values.shouldOverwrite === false) { |
| 72 | + throw new Error(red('✖') + ' Operation cancelled') |
| 73 | + } |
| 74 | + return null |
| 75 | + } |
| 76 | + }, |
| 77 | + { |
| 78 | + name: 'packageName', |
| 79 | + type: () => (isValidPackageName(targetDir) ? null : 'text'), |
| 80 | + message: 'Package name:', |
| 81 | + initial: () => toValidPackageName(targetDir), |
| 82 | + validate: (dir) => isValidPackageName(dir) || 'Invalid package.json name' |
| 83 | + }, |
| 84 | + { |
| 85 | + name: 'shouldUseTypeScript', |
| 86 | + type: 'toggle', |
| 87 | + message: 'Add TypeScript?', |
| 88 | + initial: false, |
| 89 | + active: 'Yes', |
| 90 | + inactive: 'No' |
| 91 | + }, |
| 92 | + { |
| 93 | + name: 'isSPA', |
| 94 | + type: 'toggle', |
| 95 | + message: 'Install Vue Router & Vuex for Single Page Application development?', |
| 96 | + initial: false, |
| 97 | + active: 'Yes', |
| 98 | + inactive: 'No' |
| 99 | + }, |
| 100 | + { |
| 101 | + name: 'shouldAddCypress', |
| 102 | + type: 'toggle', |
| 103 | + message: 'Add Cypress for testing?', |
| 104 | + initial: false, |
| 105 | + active: 'Yes', |
| 106 | + inactive: 'No' |
| 107 | + } |
| 108 | + ], { |
| 109 | + onCancel: () => { |
| 110 | + throw new Error(red('✖') + ' Operation cancelled') |
| 111 | + } |
| 112 | + }) |
| 113 | + } catch (cancelled) { |
| 114 | + console.log(cancelled.message) |
| 115 | + process.exit(1) |
| 116 | + } |
| 117 | + |
| 118 | + const { packageName, shouldOverwrite, shouldUseTypeScript, isSPA, shouldAddCypress } = result |
| 119 | + const root = path.join(cwd, targetDir) |
| 120 | + |
| 121 | + if (shouldOverwrite) { |
| 122 | + emptyDir(root) |
| 123 | + } else if (!fs.existsSync(root)) { |
| 124 | + fs.mkdirSync(root) |
| 125 | + } |
| 126 | + |
| 127 | + // TODO: |
| 128 | + // Add command-line option as a template-shortcut, |
| 129 | + // so that we can generate them in playgrounds |
| 130 | + // e.g. `--template typescript-spa` and `--with-tests` |
| 131 | + |
| 132 | + console.log(`\nScaffolding project in ${root}...`) |
| 133 | + |
| 134 | + const pkg = { name: packageName, version: '0.0.0' } |
| 135 | + fs.writeFileSync( |
| 136 | + path.resolve(root, 'package.json'), |
| 137 | + JSON.stringify(pkg, null, 2) |
| 138 | + ) |
| 139 | + |
| 140 | + const templateRoot = new URL('./template', import.meta.url).pathname |
| 141 | + const render = function render(templateName) { |
| 142 | + const templateDir = path.resolve(templateRoot, templateName) |
| 143 | + renderTemplate(templateDir, root) |
| 144 | + } |
| 145 | + |
| 146 | + // Add configs. |
| 147 | + render('config/base') |
| 148 | + if (shouldAddCypress) { |
| 149 | + render('config/cypress') |
| 150 | + } |
| 151 | + if (shouldUseTypeScript) { |
| 152 | + render('config/typescript') |
| 153 | + } |
| 154 | + |
| 155 | + // Render code template. |
| 156 | + const codeTemplate = |
| 157 | + (shouldUseTypeScript ? 'typescript-' : '') + |
| 158 | + (isSPA ? 'spa' : 'default') |
| 159 | + render(`code/${codeTemplate}`) |
| 160 | + |
| 161 | + // TODO: README generation |
| 162 | + |
| 163 | + // Cleanup. |
| 164 | + |
| 165 | + if (shouldUseTypeScript) { |
| 166 | + // Should remove the `vite.config.js` from the base config |
| 167 | + fs.unlinkSync(path.resolve(root, 'vite.config.js')) |
| 168 | + } |
| 169 | + |
| 170 | + if (!shouldAddCypress) { |
| 171 | + // All templates assumes the need of tests. |
| 172 | + // If the user doesn't need it: |
| 173 | + // rm -rf cypress **/__tests__/ |
| 174 | + function removeTestDirectories (dir) { |
| 175 | + for (const filename of fs.readdirSync(dir)) { |
| 176 | + const subdir = path.resolve(dir, filename) |
| 177 | + const stats = fs.lstatSync(subdir) |
| 178 | + |
| 179 | + if (!stats.isDirectory()) { continue } |
| 180 | + |
| 181 | + if (filename === 'cypress' || filename === '__tests__') { |
| 182 | + emptyDir(subdir) |
| 183 | + fs.rmdirSync(subdir) |
| 184 | + continue |
| 185 | + } |
| 186 | + |
| 187 | + removeTestDirectories(subdir) |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + removeTestDirectories(root) |
| 192 | + } |
| 193 | + |
| 194 | + // Instructions: |
| 195 | + // Supported package managers: pnpm > yarn > npm |
| 196 | + const packageManager = /pnpm/.test(process.env.npm_execpath) |
| 197 | + ? 'pnpm' |
| 198 | + : /yarn/.test(process.env.npm_execpath) |
| 199 | + ?'yarn' |
| 200 | + : 'npm' |
| 201 | + |
| 202 | + const commandsMap = { |
| 203 | + install: { |
| 204 | + pnpm: 'pnpm install', |
| 205 | + yarn: 'yarn', |
| 206 | + npm: 'npm install' |
| 207 | + }, |
| 208 | + dev: { |
| 209 | + pnpm: 'pnpm dev', |
| 210 | + yarn: 'yarn dev', |
| 211 | + npm: 'npm run dev' |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + console.log(`\nDone. Now run:\n`) |
| 216 | + if (root !== cwd) { |
| 217 | + console.log(` ${bold(green(`cd ${path.relative(cwd, root)}`))}`) |
| 218 | + } |
| 219 | + console.log(` ${bold(green(commandsMap.install[packageManager]))}`) |
| 220 | + console.log(` ${bold(green(commandsMap.dev[packageManager]))}`) |
| 221 | + console.log() |
| 222 | +} |
5 | 223 |
|
6 |
| -// Prompts: |
7 |
| -// - Project name: |
8 |
| -// - Project language: JavaScript / TypeScript |
9 |
| -// - Install Vue Router & Vuex for Single Page Applications? |
10 |
| -// - Adding tests? |
11 |
| - |
12 |
| -// TODO: |
13 |
| -// add command-line for all possible option combinations |
14 |
| -// so that we can generate them in playgrounds |
15 |
| - |
16 |
| -// Add configs. |
17 |
| -// renderTemplate('config/base') |
18 |
| -// if (needs tests) { |
19 |
| -// renderTemplate('config/cypress') |
20 |
| -// } |
21 |
| -// if (is typescript) { |
22 |
| -// renderTemplate('config/typescript') |
23 |
| -// } |
24 |
| - |
25 |
| -// templateName = |
26 |
| -// (isTs ? 'typescript-' : '') + |
27 |
| -// (isSPA ? 'spa' : 'default') |
28 |
| -// renderTemplate(`code/${templateName}`) |
29 |
| - |
30 |
| -// Cleanup. |
31 |
| - |
32 |
| -// All templates assumes the need of tests. |
33 |
| -// If the user doesn't need it: |
34 |
| -// rm -rf cypress **/__tests__/ |
35 |
| - |
36 |
| -// TS config template may add redundant tsconfig.json. |
37 |
| -// Should clean that too. |
38 |
| - |
39 |
| -// Instructions: |
40 |
| -// Supported package managers: pnpm > yarn > npm |
| 224 | +init().catch((e) => { |
| 225 | + console.error(e) |
| 226 | +}) |
0 commit comments