Skip to content

Commit 35ef99f

Browse files
authored
cli: handle spinning states (#657)
1 parent 3b5c232 commit 35ef99f

File tree

4 files changed

+40
-4
lines changed

4 files changed

+40
-4
lines changed

lib/cli.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ function head(text, length = 11) {
2626
}
2727

2828
export default class CLI {
29-
constructor(stream) {
29+
constructor(stream, options = {}) {
30+
const spinnerOptions = options.spinner ?? {};
3031
this.stream = stream || process.stderr;
31-
this.spinner = ora({ stream: this.stream });
32+
this.spinner = ora({ stream: this.stream, ...spinnerOptions });
3233
this.SPINNER_STATUS = SPINNER_STATUS;
3334
this.QUESTION_TYPE = QUESTION_TYPE;
3435
this.figureIndent = ' ';
@@ -67,7 +68,16 @@ export default class CLI {
6768
'defaultAnswer must be provided for non-confirmation prompts');
6869
}
6970

71+
const { isSpinning, text: spinningMessage } = this.spinner;
72+
73+
if (isSpinning) {
74+
this.spinner.stop();
75+
}
76+
7077
if (this.assumeYes) {
78+
if (isSpinning) {
79+
this.spinner.start(spinningMessage);
80+
}
7181
return defaultAnswer;
7282
}
7383

@@ -78,6 +88,10 @@ export default class CLI {
7888
default: defaultAnswer
7989
}]);
8090

91+
if (isSpinning) {
92+
this.spinner.start(spinningMessage);
93+
}
94+
8195
return answer;
8296
}
8397

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
},
1313
"scripts": {
1414
"test": "npm run test-unit && npm run lint",
15-
"test-unit": "mocha --timeout 60000 test/unit/*.test.js",
16-
"test-all": "mocha --timeout 60000 test/**/*.test.js",
15+
"test-unit": "mocha --timeout 60000 test/unit/*.test.js --exit",
16+
"test-all": "mocha --timeout 60000 test/**/*.test.js --exit",
1717
"coverage": "c8 --reporter=html --reporter=text --reporter=text-summary npm test",
1818
"coverage-all": "c8 --reporter=lcov --reporter=text --reporter=text-summary npm run test-all",
1919
"lint": "eslint . --cache",

test/unit/ci_start.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ describe('Jenkins', () => {
4444
it('should fail if starting node-pull-request throws', async() => {
4545
const cli = new TestCLI();
4646
const request = {
47+
fetch: sinon.stub().returns(Promise.resolve({ status: 400 })),
4748
text: sinon.stub().throws(),
4849
json: sinon.stub().withArgs(CI_CRUMB_URL)
4950
.returns(Promise.resolve({ crumb }))

test/unit/cli.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,27 @@ describe('cli', () => {
165165
});
166166
});
167167

168+
describe('prompt cares about spinner', () => {
169+
beforeEach(() => {
170+
stream = new LogStream();
171+
cli = new CLI(stream, {
172+
spinner: { isEnabled: true }
173+
});
174+
cli.setAssumeYes();
175+
});
176+
177+
it('pauses when prompt a question', async() => {
178+
cli.startSpinner('foo');
179+
assert.deepEqual(cli.spinner.text, 'foo');
180+
assert.deepEqual(cli.spinner.isSpinning, true);
181+
await cli.prompt('So you think darkness is your ally?', {
182+
defaultAnswer: 'Yes, I was born in it. Molded by it.',
183+
questionType: cli.QUESTION_TYPE.INPUT
184+
});
185+
assert.strictEqual(cli.spinner.isSpinning, true);
186+
});
187+
});
188+
168189
describe('prompt assume yes', () => {
169190
beforeEach(() => {
170191
stream = new LogStream();

0 commit comments

Comments
 (0)