Skip to content

Commit 66037a6

Browse files
authored
refactor: extend cli.prompt (#389)
1 parent 4ea4dfa commit 66037a6

File tree

4 files changed

+30
-8
lines changed

4 files changed

+30
-8
lines changed

lib/backport_session.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ class BackportSession extends Session {
162162
const newBranch = `backport-${this.prid}-to-${this.target}`;
163163
const shouldCheckout = await cli.prompt(
164164
`Do you want to checkout to a new branch \`${newBranch}\`` +
165-
' to start backporting?', false);
165+
' to start backporting?', { defaultAnswer: false });
166166
if (shouldCheckout) {
167167
await runAsync('git', ['checkout', '-b', newBranch]);
168168
}

lib/cli.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,35 @@ class CLI {
3535
this.figureIndent = ' '.repeat(indent);
3636
}
3737

38-
async prompt(question, defaultAnswer = true) {
39-
this.separator();
38+
async prompt(question, opts = {
39+
defaultAnswer: true,
40+
noSeparator: false,
41+
questionType: 'confirm'
42+
}) {
43+
if (!opts.noSeparator) {
44+
this.separator();
45+
}
46+
47+
const questionType = opts.questionType || 'confirm';
48+
const availableTypes = ['input', 'number', 'confirm'];
49+
if (!availableTypes.includes(questionType)) {
50+
throw new Error(
51+
`${questionType} must be one of ${availableTypes.join(', ')}`);
52+
}
53+
54+
const defaultAnswer =
55+
(opts.defaultAnswer !== 'undefined') ? opts.defaultAnswer : true;
4056
if (this.assumeYes) {
4157
return defaultAnswer;
4258
}
59+
4360
const { answer } = await inquirer.prompt([{
44-
type: 'confirm',
61+
type: questionType,
4562
name: 'answer',
4663
message: question,
4764
default: defaultAnswer
4865
}]);
66+
4967
return answer;
5068
}
5169

lib/landing_session.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class LandingSession extends Session {
3434
// metadata check.
3535
const defaultAnswer = !cli.assumeYes ? true : metadata.status;
3636
const shouldContinue = await cli.prompt(
37-
`This PR ${status} to land, do you want to continue?`, defaultAnswer);
37+
`This PR ${status} to land, do you want to continue?`, { defaultAnswer });
3838
if (!shouldContinue) {
3939
return this.abort(false);
4040
}
@@ -234,7 +234,7 @@ class LandingSession extends Session {
234234
forceLand = await cli.prompt(
235235
'The commit did not pass the validation. ' +
236236
'Do you still want to land it?',
237-
false);
237+
{ defaultAnswer: false });
238238
}
239239

240240
if (!forceLand) {

test/unit/cli.test.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,11 +178,15 @@ describe('cli', () => {
178178
});
179179

180180
it('should return true if default is set to true', async() => {
181-
assert.strictEqual(await cli.prompt('Question?', true), true);
181+
assert.strictEqual(await cli.prompt('Question?', {
182+
defaultAnswer: true
183+
}), true);
182184
});
183185

184186
it('should return false if default is set to false', async() => {
185-
assert.strictEqual(await cli.prompt('Question?', false), false);
187+
assert.strictEqual(await cli.prompt('Question?', {
188+
defaultAnswer: false
189+
}), false);
186190
});
187191
});
188192
});

0 commit comments

Comments
 (0)