@@ -25,6 +25,19 @@ const defaultOptions = {
25
25
auto : false
26
26
} ;
27
27
28
+ function hasDefault ( prompt = { } ) {
29
+ return isObject ( prompt ) && prompt . hasOwnProperty ( 'default' ) ;
30
+ }
31
+
32
+ function getDefault ( prompt = { } ) {
33
+ if ( prompt . choices && prompt . choices . length ) {
34
+ const defaultChoice = prompt . choices [ prompt . default ] ;
35
+ return isObject ( defaultChoice ) ? defaultChoice . value : defaultChoice ;
36
+ }
37
+
38
+ return prompt . default ;
39
+ }
40
+
28
41
/**
29
42
* UI class. Handles all interaction with the user via the terminal
30
43
*
@@ -136,18 +149,22 @@ class UI {
136
149
137
150
if ( this . auto ) {
138
151
if ( Array . isArray ( prompts ) ) {
139
- const defaultedPrompts = prompts . filter ( prompt => Boolean ( prompt . default ) ) . reduce (
140
- ( obj , prompt ) => Object . assign ( { } , obj , { [ prompt . name ] : prompt . default } ) ,
152
+ const defaultedPrompts = prompts . filter ( hasDefault ) . reduce (
153
+ ( obj , prompt ) => Object . assign ( { } , obj , { [ prompt . name ] : getDefault ( prompt ) } ) ,
141
154
{ }
142
155
) ;
143
- const promptsToAsk = prompts . filter ( prompt => ! prompt . default ) ;
156
+ const promptsToAsk = prompts . filter ( prompt => ! hasDefault ( prompt ) ) ;
157
+
158
+ if ( ! promptsToAsk . length ) {
159
+ return Promise . resolve ( defaultedPrompts ) ;
160
+ }
144
161
145
162
return this . noSpin ( ( ) => this . inquirer ( promptsToAsk ) )
146
163
. then ( answers => Object . assign ( answers , defaultedPrompts ) ) ;
147
164
}
148
165
149
166
/* istanbul ignore else */
150
- if ( isObject ( prompts ) && prompts . default ) {
167
+ if ( hasDefault ( prompts ) ) {
151
168
return Promise . resolve ( { [ prompts . name ] : prompts . default } ) ;
152
169
}
153
170
}
0 commit comments