diff --git a/lib/base.js b/lib/base.js index 8b59d1a9..c87a734c 100644 --- a/lib/base.js +++ b/lib/base.js @@ -236,10 +236,12 @@ Base.prototype.welcome = deprecate( Base.prototype.prompt = function (questions) { questions = promptSuggestion.prefillQuestions(this._globalConfig, questions); + questions = promptSuggestion.prefillQuestions(this.config, questions); return this.env.adapter.prompt(questions).then(function (answers) { if (!this.options['skip-cache']) { - promptSuggestion.storeAnswers(this._globalConfig, questions, answers); + promptSuggestion.storeAnswers(this._globalConfig, questions, answers, false); + promptSuggestion.storeAnswers(this.config, questions, answers, true); } return answers; diff --git a/lib/util/prompt-suggestion.js b/lib/util/prompt-suggestion.js index 1755f380..9849f216 100644 --- a/lib/util/prompt-suggestion.js +++ b/lib/util/prompt-suggestion.js @@ -54,15 +54,21 @@ var getListDefault = function (question, defaultValue) { * * @param {Object} question Inquirer prompt item * @param {String|Array} answer The inquirer answer + * @param {Boolean} storeAll Should store default values * @return {Boolean} Answer to be stored * @private */ -var storeListAnswer = function (question, answer) { - var choiceValues = _.map(question.choices, 'value'); +var storeListAnswer = function (question, answer, storeAll) { + var choiceValues = _.map(question.choices, function(choice) { + if (choice.hasOwnProperty('value')) { + return choice.value; + } + return choice; + }); var choiceIndex = choiceValues.indexOf(answer); // Check if answer is not equal to default value - if (question.default !== choiceIndex) { + if (storeAll || question.default !== choiceIndex) { return true; } @@ -75,12 +81,13 @@ var storeListAnswer = function (question, answer) { * * @param {Object} question Inquirer prompt item * @param {String|Array} answer The inquirer answer + * @param {Boolean} storeAll Should store default values * @return {Boolean} Answer to be stored * @private */ -var storeAnswer = function (question, answer) { +var storeAnswer = function (question, answer, storeAll) { // Check if answer is not equal to default value or is undefined - if (answer !== undefined && question.default !== answer) { + if (answer !== undefined && (storeAll || question.default !== answer)) { return true; } @@ -142,13 +149,15 @@ promptSuggestion.prefillQuestions = function (store, questions) { * @param {Store} store `.yo-rc-global` global config * @param {Array|Object} questions Original prompt questions * @param {Object} answers The inquirer answers + * @param {Boolean} storeAll Should store default values */ -promptSuggestion.storeAnswers = function (store, questions, answers) { +promptSuggestion.storeAnswers = function (store, questions, answers, storeAll) { 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'); + storeAll = storeAll || false; var promptValues = store.get('promptValues') || {}; if (!Array.isArray(questions)) { @@ -167,11 +176,11 @@ promptSuggestion.storeAnswers = function (store, questions, answers) { switch (question.type) { case 'rawlist': case 'expand': - saveAnswer = storeListAnswer(question, answer); + saveAnswer = storeListAnswer(question, answer, storeAll); break; default: - saveAnswer = storeAnswer(question, answer); + saveAnswer = storeAnswer(question, answer, storeAll); break; } diff --git a/test/prompt-suggestion.js b/test/prompt-suggestion.js index 18e2651c..6c76623d 100644 --- a/test/prompt-suggestion.js +++ b/test/prompt-suggestion.js @@ -243,6 +243,10 @@ describe('PromptSuggestion', function () { promptSuggestion.storeAnswers(this.store, [], {}); }); + it('take a storeAll parameter', function () { + promptSuggestion.storeAnswers(this.store, [], {}, true); + }); + it('store answer in global store', function () { var question = { name: 'respuesta', @@ -259,6 +263,40 @@ describe('PromptSuggestion', function () { assert.equal(this.store.get('promptValues').respuesta, 'baz'); }); + it('don\`t store default answer in global store', function () { + var question = { + name: 'respuesta', + default: 'bar', + store: true + }; + + var mockAnswers = { + respuesta: 'bar' + }; + + this.store.delete('promptValues'); + promptSuggestion.prefillQuestions(this.store, question); + promptSuggestion.storeAnswers(this.store, question, mockAnswers, false); + assert.equal(this.store.get('promptValues'), undefined); + }); + + it('force store default answer in global store', function () { + var question = { + name: 'respuesta', + default: 'bar', + store: true + }; + + var mockAnswers = { + respuesta: 'bar' + }; + + this.store.delete('promptValues'); + promptSuggestion.prefillQuestions(this.store, question); + promptSuggestion.storeAnswers(this.store, question, mockAnswers, true); + assert.equal(this.store.get('promptValues').respuesta, 'bar'); + }); + it('don\'t store answer in global store', function () { var question = { name: 'respuesta', @@ -293,6 +331,47 @@ describe('PromptSuggestion', function () { assert.equal(this.store.get('promptValues').respuesta, 'baz'); }); + describe('empty sotre', function () { + beforeEach(function () { + this.store.delete('promptValues'); + }); + it('don\`t store default answer from rawlist type', function () { + var question = { + type: 'rawlist', + name: 'respuesta', + default: 0, + store: true, + choices: ['foo', new inquirer.Separator('spacer'), 'bar', 'baz'] + }; + + var mockAnswers = { + respuesta: 'foo' + }; + + promptSuggestion.prefillQuestions(this.store, question); + promptSuggestion.storeAnswers(this.store, question, mockAnswers, false); + assert.equal(this.store.get('promptValues'), undefined); + }); + + it('force store default answer from rawlist type', function () { + var question = { + type: 'rawlist', + name: 'respuesta', + default: 0, + store: true, + choices: ['foo', new inquirer.Separator('spacer'), 'bar', 'baz'] + }; + + var mockAnswers = { + respuesta: 'foo' + }; + + promptSuggestion.prefillQuestions(this.store, question); + promptSuggestion.storeAnswers(this.store, question, mockAnswers, true); + assert.equal(this.store.get('promptValues').respuesta, 'foo'); + }); + }); + it('store falsy answer (but not undefined) in global store', function () { var question = { name: 'respuesta',