Skip to content

Commit

Permalink
fix: enforce default for non-confirmation prompts
Browse files Browse the repository at this point in the history
  • Loading branch information
codebytere committed May 10, 2020
1 parent 157b508 commit a473834
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
23 changes: 19 additions & 4 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,15 @@ const SPINNER_STATUS = {
WARN: 'warn',
INFO: 'info'
};

const { SUCCESS, FAILED, WARN, INFO } = SPINNER_STATUS;

const QUESTION_TYPE = {
INPUT: 'input',
NUMBER: 'number',
CONFIRM: 'confirm'
};

function head(text, length = 11) {
return chalk.bold(text.padEnd(length));
}
Expand All @@ -23,6 +30,7 @@ class CLI {
this.stream = stream || process.stderr;
this.spinner = ora({ stream: this.stream });
this.SPINNER_STATUS = SPINNER_STATUS;
this.QUESTION_TYPE = QUESTION_TYPE;
this.figureIndent = ' ';
this.assumeYes = false;
}
Expand All @@ -44,15 +52,21 @@ class CLI {
this.separator();
}

const questionType = opts.questionType || 'confirm';
const availableTypes = ['input', 'number', 'confirm'];
const questionType = opts.questionType || QUESTION_TYPE.CONFIRM;
const availableTypes = Object.values(QUESTION_TYPE);
if (!availableTypes.includes(questionType)) {
throw new Error(
`${questionType} must be one of ${availableTypes.join(', ')}`);
}

const defaultAnswer =
(opts.defaultAnswer !== 'undefined') ? opts.defaultAnswer : true;
const defaultAnswer = (opts.defaultAnswer === undefined)
? true : opts.defaultAnswer;
if (typeof defaultAnswer === 'boolean' &&
questionType !== QUESTION_TYPE.CONFIRM) {
throw new Error(
'defaultAnswer must be provided for non-confirmation prompts');
}

if (this.assumeYes) {
return defaultAnswer;
}
Expand Down Expand Up @@ -153,5 +167,6 @@ class CLI {
};

CLI.SPINNER_STATUS = SPINNER_STATUS;
CLI.QUESTION_TYPE = QUESTION_TYPE;

module.exports = CLI;
30 changes: 30 additions & 0 deletions test/unit/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,36 @@ describe('cli', () => {
cli.setAssumeYes();
});

it('rejects when no default answer is provided for \'input\' type', () => {
return assert.rejects(async() => {
await cli.prompt('What is your favorite color', {
questionType: cli.QUESTION_TYPE.INPUT
});
}, /defaultAnswer must be provided for non-confirmation prompts/);
});

it('rejects when no default answer is provided for \'number\' type', () => {
return assert.rejects(async() => {
await cli.prompt('Pick a number from 1-10', {
questionType: cli.QUESTION_TYPE.NUMBER
});
}, /defaultAnswer must be provided for non-confirmation prompts/);
});

it('should return the default answer for an \'input\' type', async() => {
assert.strictEqual(await cli.prompt('What is your favorite color', {
defaultAnswer: 'blue',
questionType: cli.QUESTION_TYPE.INPUT
}), 'blue');
});

it('should return the default answer for a \'number\' type', async() => {
assert.strictEqual(await cli.prompt('Pick a number from 1-10', {
defaultAnswer: 10,
questionType: cli.QUESTION_TYPE.NUMBER
}), 10);
});

it('should return true if no default is given', async() => {
assert.strictEqual(await cli.prompt('Question?'), true);
});
Expand Down

0 comments on commit a473834

Please sign in to comment.