-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
prompt-suggestion.js
177 lines (152 loc) · 4.65 KB
/
prompt-suggestion.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
'use strict';
var assert = require('assert');
var _ = require('lodash');
/**
* @mixin
* @alias util/prompt-suggestion
*/
var promptSuggestion = module.exports;
/**
* Returns the default value for a checkbox.
*
* @param {Object} question Inquirer prompt item
* @param {*} defaultValue The stored default value
* @return {*} Default value to set
* @private
*/
var getCheckboxDefault = function (question, defaultValue) {
// For simplicity we uncheck all boxes and
// use .default to set the active ones.
_.each(question.choices, function (choice) {
if (typeof choice === 'object') {
choice.checked = false;
}
});
return defaultValue;
};
/**
* Returns the default value for a list.
*
* @param {Object} question Inquirer prompt item
* @param {*} defaultValue The stored default value
* @return {*} Default value to set
* @private
*/
var getListDefault = function (question, defaultValue) {
var choiceValues = _.map(question.choices, function (choice) {
if (typeof choice === 'object') {
return choice.value;
} else {
return choice;
}
});
return choiceValues.indexOf(defaultValue);
};
/**
* Return true if the answer should be store in
* the global store, otherwise false.
*
* @param {Object} question Inquirer prompt item
* @param {String|Array} answer The inquirer answer
* @return {Boolean} Answer to be stored
* @private
*/
var storeListAnswer = function (question, answer) {
var choiceValues = _.pluck(question.choices, 'value');
var choiceIndex = choiceValues.indexOf(answer);
// Check if answer is not equal to default value
if (question.default !== choiceIndex) {
return true;
}
return false;
};
/**
* Return true if the answer should be store in
* the global store, otherwise false.
*
* @param {Object} question Inquirer prompt item
* @param {String|Array} answer The inquirer answer
* @return {Boolean} Answer to be stored
* @private
*/
var storeAnswer = function (question, answer) {
// Check if answer is not equal to default value or is undefined
if (answer && question.default !== answer) {
return true;
}
return false;
};
/**
* Prefill the defaults with values from the global store.
*
* @param {Store} store `.yo-rc-global` global config
* @param {Array|Object} questions Original prompt questions
* @return {Array} Prompt questions array with prefilled values.
*/
promptSuggestion.prefillQuestions = function (store, questions) {
assert(store, 'A store parameter is required');
assert(questions, 'A questions parameter is required');
var promptValues = store.get('promptValues') || {};
questions = _.cloneDeep(questions);
if (!Array.isArray(questions)) {
questions = [questions];
}
// Write user defaults back to prompt
return questions.map(function (question) {
if (question.store !== true) return question;
var storedValue = promptValues[question.name];
if (storedValue == null) return question;
// Override prompt default with the user's default
switch (question.type) {
case 'rawlist':
case 'expand':
question.default = getListDefault(question, storedValue);
break;
case 'checkbox':
question.default = getCheckboxDefault(question, storedValue);
break;
default:
question.default = storedValue;
break;
}
return question;
}.bind(this));
};
/**
* Store the answers in the global store.
*
* @param {Store} store `.yo-rc-global` global config
* @param {Array|Object} questions Original prompt questions
* @param {Object} answers The inquirer answers
*/
promptSuggestion.storeAnswers = function (store, questions, answers) {
assert(store, 'A store parameter is required');
assert(answers, 'A answers parameter is required');
assert(questions, 'A questions parameter is required');
assert.ok(_.isObject(answers), 'answers must be a object');
var promptValues = store.get('promptValues') || {};
if (!Array.isArray(questions)) {
questions = [questions];
}
_.each(questions, function (question) {
if (question.store !== true) return;
var saveAnswer;
var key = question.name;
var answer = answers[key];
switch (question.type) {
case 'rawlist':
case 'expand':
saveAnswer = storeListAnswer(question, answer);
break;
default:
saveAnswer = storeAnswer(question, answer);
break;
}
if (saveAnswer) {
promptValues[key] = answer;
}
}.bind(this));
if (Object.keys(promptValues).length) {
store.set('promptValues', promptValues);
}
};