Skip to content

Commit

Permalink
make prompt default value dynamic
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Jun 7, 2022
1 parent 6518394 commit ea460eb
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 18 deletions.
14 changes: 4 additions & 10 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ class Generator extends Base {
storageForQuestion[name] = questionStorage;
const value = questionStorage.getPath(name);
if (value !== undefined) {
question.default = value;
question.default = (answers) => answers[name];
return [name, value];
}
}
Expand All @@ -566,20 +566,14 @@ class Generator extends Base {
}
}

// Pre-fill answers with storage values.
const answers = {};

questions = promptSuggestion.prefillQuestions(
this._globalConfig,
questions
);
questions = promptSuggestion.prefillQuestions(this.config, questions);
questions
.map(getAnswerFromStorage)
.filter((a) => a)
.forEach(([key, value]) => {
answers[key] = value;
});
const answers = Object.fromEntries(
questions.map(getAnswerFromStorage).filter(Boolean)
);

return this.env.adapter.prompt(questions, answers).then((answers) => {
Object.entries(storageForQuestion).forEach(([name, questionStorage]) => {
Expand Down
11 changes: 3 additions & 8 deletions test/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -1847,14 +1847,9 @@ describe('Base', () => {
return this.dummy
.prompt([input1Prompt, input2Prompt], this.dummy.config)
.then((_) => {
assert.deepEqual(
promptSpy.getCall(0).args[0][0].default,
'prompt1Value'
);
assert.deepEqual(
promptSpy.getCall(0).args[0][1].default,
'prompt2Value'
);
const [prompts, answers] = promptSpy.getCall(0).args;
assert.deepEqual(prompts[0].default(answers), 'prompt1Value');
assert.deepEqual(prompts[1].default(answers), 'prompt2Value');
});
});

Expand Down

0 comments on commit ea460eb

Please sign in to comment.