Skip to content

Commit

Permalink
Save current config to .yo-rc.json (#963)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
pitrew authored and SBoudrias committed Nov 25, 2016
1 parent 4eccb2a commit f3fb1d5
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 9 deletions.
4 changes: 3 additions & 1 deletion lib/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
25 changes: 17 additions & 8 deletions lib/util/prompt-suggestion.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -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)) {
Expand All @@ -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;
}

Expand Down
79 changes: 79 additions & 0 deletions test/prompt-suggestion.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand Down Expand Up @@ -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',
Expand Down

0 comments on commit f3fb1d5

Please sign in to comment.