Skip to content

Commit

Permalink
#132 Override questions with prompts.override (#144)
Browse files Browse the repository at this point in the history
  • Loading branch information
lumio authored and terkelg committed Feb 24, 2019
1 parent 54de9ab commit 6c55528
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
26 changes: 23 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,17 @@ const noop = () => {};
*/
async function prompt(questions=[], { onSubmit=noop, onCancel=noop }={}) {
const answers = {};
const override = prompt._override || {};
questions = [].concat(questions);
let answer, question, quit, name, type;

const getFormattedAnswer = async (question, answer, skipValidation = false) => {
if (!skipValidation && question.validate && question.validate(answer) !== true) {
return;
}
return question.format ? await question.format(answer, answers) : answer
};

for (question of questions) {
({ name, type } = question);

Expand All @@ -39,10 +47,18 @@ async function prompt(questions=[], { onSubmit=noop, onCancel=noop }={}) {
throw new Error(`prompt type (${type}) is not defined`);
}

if (override[question.name] !== undefined) {
answer = await getFormattedAnswer(question, override[question.name]);
if (answer !== undefined) {
answers[name] = answer;
continue;
}
}

try {
// Get the injected answer if there is one or prompt the user
answer = prompt._injected ? getInjectedAnswer(prompt._injected) : await prompts[type](question);
answers[name] = answer = question.format ? await question.format(answer, answers) : answer;
answers[name] = answer = await getFormattedAnswer(question, answer, true);
quit = await onSubmit(question, answer, answers);
} catch (err) {
quit = !(await onCancel(question, answers));
Expand All @@ -64,7 +80,11 @@ function getInjectedAnswer(injected) {
}

function inject(answers) {
prompt._injected = (prompt._injected || []).concat(answers)
prompt._injected = (prompt._injected || []).concat(answers);
}

function override(answers) {
prompt._override = Object.assign({}, answers);
}

module.exports = Object.assign(prompt, { prompt, prompts, inject });
module.exports = Object.assign(prompt, { prompt, prompts, inject, override });
32 changes: 32 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,38 @@ let onCancel = prompt => {
let response = await prompts(questions, { onCancel });
```

### override

Type: `Function`

Preanswer questions by passing an object with answers to `prompts.override`.
Powerful when combined with arguments of process.

**Example**
```js
const prompts = require('prompts');
prompts.override(require('yargs').argv);

const response = await prompts([
{
type: 'text',
name: 'twitter',
message: `What's your twitter handle?`
},
{
type: 'multiselect',
name: 'color',
message: 'Pick colors',
choices: [
{ title: 'Red', value: '#ff0000' },
{ title: 'Green', value: '#00ff00' },
{ title: 'Blue', value: '#0000ff' }
],
}
]);

console.log(response);
```

### inject(values)

Expand Down

0 comments on commit 6c55528

Please sign in to comment.