@@ -3,12 +3,6 @@ const ObjectUtil = require('../../util/object')
3
3
let answers = { }
4
4
let prompts = [ ]
5
5
6
- function getPrompt ( id ) {
7
- return prompts . find (
8
- p => p . id === id
9
- )
10
- }
11
-
12
6
function generatePromptError ( value ) {
13
7
let message
14
8
if ( typeof value === 'string' ) {
@@ -21,28 +15,6 @@ function generatePromptError (value) {
21
15
}
22
16
}
23
17
24
- function getDefaultValue ( prompt ) {
25
- if ( typeof prompt . raw . value !== 'undefined' ) {
26
- return prompt . raw . value
27
- }
28
- const defaultValue = prompt . raw . default
29
- if ( typeof defaultValue === 'function' ) {
30
- return defaultValue ( answers )
31
- } else if ( prompt . type === 'checkbox' ) {
32
- const choices = getChoices ( prompt )
33
- if ( choices ) {
34
- return choices . filter (
35
- c => c . checked
36
- ) . map (
37
- c => c . value
38
- )
39
- }
40
- } else if ( prompt . type === 'confirm' ) {
41
- return false
42
- }
43
- return defaultValue
44
- }
45
-
46
18
function getEnabled ( value ) {
47
19
const type = typeof value
48
20
if ( type === 'function' ) {
@@ -78,12 +50,13 @@ function getDisplayedValue (prompt, value) {
78
50
return JSON . stringify ( value )
79
51
}
80
52
81
- function generatePromptChoice ( prompt , data ) {
53
+ function generatePromptChoice ( prompt , data , defaultValue ) {
82
54
return {
83
55
value : getDisplayedValue ( prompt , data . value ) ,
84
56
name : data . name ,
85
57
checked : data . checked ,
86
- disabled : data . disabled
58
+ disabled : data . disabled ,
59
+ isDefault : data . value === defaultValue
87
60
}
88
61
}
89
62
@@ -99,8 +72,9 @@ function getChoices (prompt) {
99
72
} else {
100
73
result = data
101
74
}
75
+ const defaultValue = getDefaultValue ( prompt )
102
76
return result . map (
103
- item => generatePromptChoice ( prompt , item )
77
+ item => generatePromptChoice ( prompt , item , defaultValue )
104
78
)
105
79
}
106
80
@@ -149,10 +123,13 @@ function updatePrompts () {
149
123
const answer = getAnswer ( prompt . id )
150
124
if ( typeof answer !== 'undefined' ) {
151
125
value = answer
126
+ } else if ( typeof prompt . raw . value !== 'undefined' ) {
127
+ value = prompt . raw . value
152
128
} else {
153
129
value = getDefaultValue ( prompt )
154
130
}
155
131
prompt . value = getDisplayedValue ( prompt , value )
132
+ prompt . rawValue = value
156
133
setAnswer ( prompt . id , getValue ( prompt , value ) )
157
134
}
158
135
}
@@ -197,7 +174,7 @@ function remove (id) {
197
174
}
198
175
199
176
function setValue ( { id, value } ) {
200
- const prompt = getPrompt ( id )
177
+ const prompt = findOne ( id )
201
178
if ( ! prompt ) {
202
179
console . warn ( `Prompt '${ prompt } ' not found` )
203
180
return null
@@ -210,13 +187,39 @@ function setValue ({ id, value }) {
210
187
prompt . error = null
211
188
}
212
189
prompt . value = getDisplayedValue ( prompt , value )
190
+ prompt . rawValue = value
213
191
const finalValue = getValue ( prompt , value )
214
192
prompt . valueChanged = true
215
193
setAnswer ( prompt . id , finalValue )
216
194
updatePrompts ( )
217
195
return prompt
218
196
}
219
197
198
+ function findOne ( id ) {
199
+ return prompts . find (
200
+ p => p . id === id
201
+ )
202
+ }
203
+
204
+ function getDefaultValue ( prompt ) {
205
+ const defaultValue = prompt . raw . default
206
+ if ( typeof defaultValue === 'function' ) {
207
+ return defaultValue ( answers )
208
+ } else if ( prompt . type === 'checkbox' ) {
209
+ const choices = getChoices ( prompt )
210
+ if ( choices ) {
211
+ return choices . filter (
212
+ c => c . checked
213
+ ) . map (
214
+ c => c . value
215
+ )
216
+ }
217
+ } else if ( prompt . type === 'confirm' ) {
218
+ return defaultValue || false
219
+ }
220
+ return defaultValue
221
+ }
222
+
220
223
module . exports = {
221
224
setAnswers,
222
225
changeAnswers,
@@ -226,5 +229,7 @@ module.exports = {
226
229
add,
227
230
remove,
228
231
start,
229
- setValue
232
+ setValue,
233
+ findOne,
234
+ getDefaultValue
230
235
}
0 commit comments