Skip to content

Commit

Permalink
refactor: extend cli.prompt (#389)
Browse files Browse the repository at this point in the history
  • Loading branch information
codebytere committed Mar 2, 2020
1 parent 4ea4dfa commit 66037a6
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 8 deletions.
2 changes: 1 addition & 1 deletion lib/backport_session.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ class BackportSession extends Session {
const newBranch = `backport-${this.prid}-to-${this.target}`;
const shouldCheckout = await cli.prompt(
`Do you want to checkout to a new branch \`${newBranch}\`` +
' to start backporting?', false);
' to start backporting?', { defaultAnswer: false });
if (shouldCheckout) {
await runAsync('git', ['checkout', '-b', newBranch]);
}
Expand Down
24 changes: 21 additions & 3 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,35 @@ class CLI {
this.figureIndent = ' '.repeat(indent);
}

async prompt(question, defaultAnswer = true) {
this.separator();
async prompt(question, opts = {
defaultAnswer: true,
noSeparator: false,
questionType: 'confirm'
}) {
if (!opts.noSeparator) {
this.separator();
}

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

const defaultAnswer =
(opts.defaultAnswer !== 'undefined') ? opts.defaultAnswer : true;
if (this.assumeYes) {
return defaultAnswer;
}

const { answer } = await inquirer.prompt([{
type: 'confirm',
type: questionType,
name: 'answer',
message: question,
default: defaultAnswer
}]);

return answer;
}

Expand Down
4 changes: 2 additions & 2 deletions lib/landing_session.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class LandingSession extends Session {
// metadata check.
const defaultAnswer = !cli.assumeYes ? true : metadata.status;
const shouldContinue = await cli.prompt(
`This PR ${status} to land, do you want to continue?`, defaultAnswer);
`This PR ${status} to land, do you want to continue?`, { defaultAnswer });
if (!shouldContinue) {
return this.abort(false);
}
Expand Down Expand Up @@ -234,7 +234,7 @@ class LandingSession extends Session {
forceLand = await cli.prompt(
'The commit did not pass the validation. ' +
'Do you still want to land it?',
false);
{ defaultAnswer: false });
}

if (!forceLand) {
Expand Down
8 changes: 6 additions & 2 deletions test/unit/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,15 @@ describe('cli', () => {
});

it('should return true if default is set to true', async() => {
assert.strictEqual(await cli.prompt('Question?', true), true);
assert.strictEqual(await cli.prompt('Question?', {
defaultAnswer: true
}), true);
});

it('should return false if default is set to false', async() => {
assert.strictEqual(await cli.prompt('Question?', false), false);
assert.strictEqual(await cli.prompt('Question?', {
defaultAnswer: false
}), false);
});
});
});

0 comments on commit 66037a6

Please sign in to comment.