@@ -48,7 +48,14 @@ async function init() {
48
48
let targetDir = argv . _ [ 0 ]
49
49
const defaultProjectName = ! targetDir ? 'vue-project' : targetDir
50
50
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
+
51
57
let result = { }
58
+
52
59
try {
53
60
// Prompts:
54
61
// - Project name:
@@ -95,23 +102,23 @@ async function init() {
95
102
} ,
96
103
{
97
104
name : 'shouldUseTypeScript' ,
98
- type : 'toggle' ,
105
+ type : ( ) => isValidTemplate ? null : 'toggle' ,
99
106
message : 'Add TypeScript?' ,
100
107
initial : false ,
101
108
active : 'Yes' ,
102
109
inactive : 'No'
103
110
} ,
104
111
{
105
112
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?' ,
108
115
initial : false ,
109
116
active : 'Yes' ,
110
117
inactive : 'No'
111
118
} ,
112
119
{
113
120
name : 'shouldAddCypress' ,
114
- type : 'toggle' ,
121
+ type : ( ) => isValidTemplate ? null : 'toggle' ,
115
122
message : 'Add Cypress for testing?' ,
116
123
initial : false ,
117
124
active : 'Yes' ,
@@ -127,7 +134,15 @@ async function init() {
127
134
process . exit ( 1 )
128
135
}
129
136
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
131
146
const root = path . join ( cwd , targetDir )
132
147
133
148
if ( shouldOverwrite ) {
@@ -136,11 +151,6 @@ async function init() {
136
151
fs . mkdirSync ( root )
137
152
}
138
153
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
-
144
154
console . log ( `\nScaffolding project in ${ root } ...` )
145
155
146
156
const pkg = { name : packageName , version : '0.0.0' }
0 commit comments