Skip to content

Commit f3fb1d5

Browse files
pitrewSBoudrias
authored andcommittedNov 25, 2016
Save current config to .yo-rc.json (#963)
* Save current config to .yo-rc.json * Store all answers in local storage * Store all tests added, rawList array with strings storing fixed * Tests refactored
1 parent 4eccb2a commit f3fb1d5

File tree

3 files changed

+99
-9
lines changed

3 files changed

+99
-9
lines changed
 

‎lib/base.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -236,10 +236,12 @@ Base.prototype.welcome = deprecate(
236236

237237
Base.prototype.prompt = function (questions) {
238238
questions = promptSuggestion.prefillQuestions(this._globalConfig, questions);
239+
questions = promptSuggestion.prefillQuestions(this.config, questions);
239240

240241
return this.env.adapter.prompt(questions).then(function (answers) {
241242
if (!this.options['skip-cache']) {
242-
promptSuggestion.storeAnswers(this._globalConfig, questions, answers);
243+
promptSuggestion.storeAnswers(this._globalConfig, questions, answers, false);
244+
promptSuggestion.storeAnswers(this.config, questions, answers, true);
243245
}
244246

245247
return answers;

‎lib/util/prompt-suggestion.js

+17-8
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,21 @@ var getListDefault = function (question, defaultValue) {
5454
*
5555
* @param {Object} question Inquirer prompt item
5656
* @param {String|Array} answer The inquirer answer
57+
* @param {Boolean} storeAll Should store default values
5758
* @return {Boolean} Answer to be stored
5859
* @private
5960
*/
60-
var storeListAnswer = function (question, answer) {
61-
var choiceValues = _.map(question.choices, 'value');
61+
var storeListAnswer = function (question, answer, storeAll) {
62+
var choiceValues = _.map(question.choices, function(choice) {
63+
if (choice.hasOwnProperty('value')) {
64+
return choice.value;
65+
}
66+
return choice;
67+
});
6268
var choiceIndex = choiceValues.indexOf(answer);
6369

6470
// Check if answer is not equal to default value
65-
if (question.default !== choiceIndex) {
71+
if (storeAll || question.default !== choiceIndex) {
6672
return true;
6773
}
6874

@@ -75,12 +81,13 @@ var storeListAnswer = function (question, answer) {
7581
*
7682
* @param {Object} question Inquirer prompt item
7783
* @param {String|Array} answer The inquirer answer
84+
* @param {Boolean} storeAll Should store default values
7885
* @return {Boolean} Answer to be stored
7986
* @private
8087
*/
81-
var storeAnswer = function (question, answer) {
88+
var storeAnswer = function (question, answer, storeAll) {
8289
// Check if answer is not equal to default value or is undefined
83-
if (answer !== undefined && question.default !== answer) {
90+
if (answer !== undefined && (storeAll || question.default !== answer)) {
8491
return true;
8592
}
8693

@@ -142,13 +149,15 @@ promptSuggestion.prefillQuestions = function (store, questions) {
142149
* @param {Store} store `.yo-rc-global` global config
143150
* @param {Array|Object} questions Original prompt questions
144151
* @param {Object} answers The inquirer answers
152+
* @param {Boolean} storeAll Should store default values
145153
*/
146-
promptSuggestion.storeAnswers = function (store, questions, answers) {
154+
promptSuggestion.storeAnswers = function (store, questions, answers, storeAll) {
147155
assert(store, 'A store parameter is required');
148156
assert(answers, 'A answers parameter is required');
149157
assert(questions, 'A questions parameter is required');
150158
assert.ok(_.isObject(answers), 'answers must be a object');
151159

160+
storeAll = storeAll || false;
152161
var promptValues = store.get('promptValues') || {};
153162

154163
if (!Array.isArray(questions)) {
@@ -167,11 +176,11 @@ promptSuggestion.storeAnswers = function (store, questions, answers) {
167176
switch (question.type) {
168177
case 'rawlist':
169178
case 'expand':
170-
saveAnswer = storeListAnswer(question, answer);
179+
saveAnswer = storeListAnswer(question, answer, storeAll);
171180
break;
172181

173182
default:
174-
saveAnswer = storeAnswer(question, answer);
183+
saveAnswer = storeAnswer(question, answer, storeAll);
175184
break;
176185
}
177186

‎test/prompt-suggestion.js

+79
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,10 @@ describe('PromptSuggestion', function () {
243243
promptSuggestion.storeAnswers(this.store, [], {});
244244
});
245245

246+
it('take a storeAll parameter', function () {
247+
promptSuggestion.storeAnswers(this.store, [], {}, true);
248+
});
249+
246250
it('store answer in global store', function () {
247251
var question = {
248252
name: 'respuesta',
@@ -259,6 +263,40 @@ describe('PromptSuggestion', function () {
259263
assert.equal(this.store.get('promptValues').respuesta, 'baz');
260264
});
261265

266+
it('don\`t store default answer in global store', function () {
267+
var question = {
268+
name: 'respuesta',
269+
default: 'bar',
270+
store: true
271+
};
272+
273+
var mockAnswers = {
274+
respuesta: 'bar'
275+
};
276+
277+
this.store.delete('promptValues');
278+
promptSuggestion.prefillQuestions(this.store, question);
279+
promptSuggestion.storeAnswers(this.store, question, mockAnswers, false);
280+
assert.equal(this.store.get('promptValues'), undefined);
281+
});
282+
283+
it('force store default answer in global store', function () {
284+
var question = {
285+
name: 'respuesta',
286+
default: 'bar',
287+
store: true
288+
};
289+
290+
var mockAnswers = {
291+
respuesta: 'bar'
292+
};
293+
294+
this.store.delete('promptValues');
295+
promptSuggestion.prefillQuestions(this.store, question);
296+
promptSuggestion.storeAnswers(this.store, question, mockAnswers, true);
297+
assert.equal(this.store.get('promptValues').respuesta, 'bar');
298+
});
299+
262300
it('don\'t store answer in global store', function () {
263301
var question = {
264302
name: 'respuesta',
@@ -293,6 +331,47 @@ describe('PromptSuggestion', function () {
293331
assert.equal(this.store.get('promptValues').respuesta, 'baz');
294332
});
295333

334+
describe('empty sotre', function () {
335+
beforeEach(function () {
336+
this.store.delete('promptValues');
337+
});
338+
it('don\`t store default answer from rawlist type', function () {
339+
var question = {
340+
type: 'rawlist',
341+
name: 'respuesta',
342+
default: 0,
343+
store: true,
344+
choices: ['foo', new inquirer.Separator('spacer'), 'bar', 'baz']
345+
};
346+
347+
var mockAnswers = {
348+
respuesta: 'foo'
349+
};
350+
351+
promptSuggestion.prefillQuestions(this.store, question);
352+
promptSuggestion.storeAnswers(this.store, question, mockAnswers, false);
353+
assert.equal(this.store.get('promptValues'), undefined);
354+
});
355+
356+
it('force store default answer from rawlist type', function () {
357+
var question = {
358+
type: 'rawlist',
359+
name: 'respuesta',
360+
default: 0,
361+
store: true,
362+
choices: ['foo', new inquirer.Separator('spacer'), 'bar', 'baz']
363+
};
364+
365+
var mockAnswers = {
366+
respuesta: 'foo'
367+
};
368+
369+
promptSuggestion.prefillQuestions(this.store, question);
370+
promptSuggestion.storeAnswers(this.store, question, mockAnswers, true);
371+
assert.equal(this.store.get('promptValues').respuesta, 'foo');
372+
});
373+
});
374+
296375
it('store falsy answer (but not undefined) in global store', function () {
297376
var question = {
298377
name: 'respuesta',

0 commit comments

Comments
 (0)
Please sign in to comment.