]> git.ipfire.org Git - thirdparty/vuejs/create-vue.git/commitdiff
feat: support `-t/--template` command-line option
authorHaoqun Jiang <haoqunjiang@gmail.com>
Tue, 27 Jul 2021 04:10:04 +0000 (12:10 +0800)
committerHaoqun Jiang <haoqunjiang@gmail.com>
Tue, 27 Jul 2021 04:10:04 +0000 (12:10 +0800)
index.js

index 8bd4705010ee3056172d3d1a09b224fb5263746d..e872a344599dad6c92e73830dda172587fa2c916 100755 (executable)
--- a/index.js
+++ b/index.js
@@ -48,7 +48,14 @@ async function init() {
   let targetDir = argv._[0]
   const defaultProjectName = !targetDir ? 'vue-project' : targetDir
 
+  const TEMPLATE_LIST = ['default', 'spa']
+    .flatMap(x => [x, x + '-ts'])
+    .flatMap(x => [x, x + '-with-tests'])
+  let template = argv.template || argv.t
+  const isValidTemplate = TEMPLATE_LIST.includes(template)
+
   let result = {}
+
   try {
     // Prompts:
     // - Project name:
@@ -95,7 +102,7 @@ async function init() {
       },
       {
         name: 'shouldUseTypeScript',
-        type: 'toggle',
+        type: () => isValidTemplate ? null : 'toggle',
         message: 'Add TypeScript?',
         initial: false,
         active: 'Yes',
@@ -103,15 +110,15 @@ async function init() {
       },
       {
         name: 'isSPA',
-        type: 'toggle',
-        message: 'Install Vue Router & Vuex for Single Page Application development?',
+        type: () => isValidTemplate ? null : 'toggle',
+        message: 'Add Vue Router & Vuex for Single Page Application development?',
         initial: false,
         active: 'Yes',
         inactive: 'No'
       },
       {
         name: 'shouldAddCypress',
-        type: 'toggle',
+        type: () => isValidTemplate ? null : 'toggle',
         message: 'Add Cypress for testing?',
         initial: false,
         active: 'Yes',
@@ -127,7 +134,15 @@ async function init() {
     process.exit(1)
   }
 
-  const { packageName, shouldOverwrite, shouldUseTypeScript, isSPA, shouldAddCypress } = result
+  // `initial` won't take effect if the prompt type is null
+  // so we still have to assign the default values here
+  const {
+    packageName = toValidPackageName(defaultProjectName),
+    shouldOverwrite,
+    shouldUseTypeScript = isValidTemplate && template.includes('-ts'),
+    isSPA = isValidTemplate && template.includes('spa'),
+    shouldAddCypress = isValidTemplate && template.includes('-with-tests')
+  } = result
   const root = path.join(cwd, targetDir)
 
   if (shouldOverwrite) {
@@ -136,11 +151,6 @@ async function init() {
     fs.mkdirSync(root)
   }
 
-  // TODO:
-  // Add command-line option as a template-shortcut,
-  // so that we can generate them in playgrounds
-  // e.g. `--template typescript-spa` and `--with-tests`
-
   console.log(`\nScaffolding project in ${root}...`)
 
   const pkg = { name: packageName, version: '0.0.0' }