Skip to content

Commit 3c0ce26

Browse files
committed
feat: support -t/--template command-line option
1 parent ecdd912 commit 3c0ce26

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

index.js

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,14 @@ async function init() {
4848
let targetDir = argv._[0]
4949
const defaultProjectName = !targetDir ? 'vue-project' : targetDir
5050

51+
const TEMPLATE_LIST = ['default', 'spa']
52+
.flatMap(x => [x, x + '-ts'])
53+
.flatMap(x => [x, x + '-with-tests'])
54+
let template = argv.template || argv.t
55+
const isValidTemplate = TEMPLATE_LIST.includes(template)
56+
5157
let result = {}
58+
5259
try {
5360
// Prompts:
5461
// - Project name:
@@ -95,23 +102,23 @@ async function init() {
95102
},
96103
{
97104
name: 'shouldUseTypeScript',
98-
type: 'toggle',
105+
type: () => isValidTemplate ? null : 'toggle',
99106
message: 'Add TypeScript?',
100107
initial: false,
101108
active: 'Yes',
102109
inactive: 'No'
103110
},
104111
{
105112
name: 'isSPA',
106-
type: 'toggle',
107-
message: 'Install Vue Router & Vuex for Single Page Application development?',
113+
type: () => isValidTemplate ? null : 'toggle',
114+
message: 'Add Vue Router & Vuex for Single Page Application development?',
108115
initial: false,
109116
active: 'Yes',
110117
inactive: 'No'
111118
},
112119
{
113120
name: 'shouldAddCypress',
114-
type: 'toggle',
121+
type: () => isValidTemplate ? null : 'toggle',
115122
message: 'Add Cypress for testing?',
116123
initial: false,
117124
active: 'Yes',
@@ -127,7 +134,15 @@ async function init() {
127134
process.exit(1)
128135
}
129136

130-
const { packageName, shouldOverwrite, shouldUseTypeScript, isSPA, shouldAddCypress } = result
137+
// `initial` won't take effect if the prompt type is null
138+
// so we still have to assign the default values here
139+
const {
140+
packageName = toValidPackageName(defaultProjectName),
141+
shouldOverwrite,
142+
shouldUseTypeScript = isValidTemplate && template.includes('-ts'),
143+
isSPA = isValidTemplate && template.includes('spa'),
144+
shouldAddCypress = isValidTemplate && template.includes('-with-tests')
145+
} = result
131146
const root = path.join(cwd, targetDir)
132147

133148
if (shouldOverwrite) {
@@ -136,11 +151,6 @@ async function init() {
136151
fs.mkdirSync(root)
137152
}
138153

139-
// TODO:
140-
// Add command-line option as a template-shortcut,
141-
// so that we can generate them in playgrounds
142-
// e.g. `--template typescript-spa` and `--with-tests`
143-
144154
console.log(`\nScaffolding project in ${root}...`)
145155

146156
const pkg = { name: packageName, version: '0.0.0' }

0 commit comments

Comments
 (0)