Skip to content

cli: handle spinning states #657

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ function head(text, length = 11) {
}

export default class CLI {
constructor(stream) {
constructor(stream, options = {}) {
const spinnerOptions = options.spinner ?? {};
this.stream = stream || process.stderr;
this.spinner = ora({ stream: this.stream });
this.spinner = ora({ stream: this.stream, ...spinnerOptions });
this.SPINNER_STATUS = SPINNER_STATUS;
this.QUESTION_TYPE = QUESTION_TYPE;
this.figureIndent = ' ';
Expand Down Expand Up @@ -67,7 +68,16 @@ export default class CLI {
'defaultAnswer must be provided for non-confirmation prompts');
}

const { isSpinning, text: spinningMessage } = this.spinner;

if (isSpinning) {
this.spinner.stop();
}

if (this.assumeYes) {
if (isSpinning) {
this.spinner.start(spinningMessage);
}
return defaultAnswer;
}

Expand All @@ -78,6 +88,10 @@ export default class CLI {
default: defaultAnswer
}]);

if (isSpinning) {
this.spinner.start(spinningMessage);
}

return answer;
}

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
},
"scripts": {
"test": "npm run test-unit && npm run lint",
"test-unit": "mocha --timeout 60000 test/unit/*.test.js",
"test-all": "mocha --timeout 60000 test/**/*.test.js",
"test-unit": "mocha --timeout 60000 test/unit/*.test.js --exit",
"test-all": "mocha --timeout 60000 test/**/*.test.js --exit",
"coverage": "c8 --reporter=html --reporter=text --reporter=text-summary npm test",
"coverage-all": "c8 --reporter=lcov --reporter=text --reporter=text-summary npm run test-all",
"lint": "eslint . --cache",
Expand Down
1 change: 1 addition & 0 deletions test/unit/ci_start.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ describe('Jenkins', () => {
it('should fail if starting node-pull-request throws', async() => {
const cli = new TestCLI();
const request = {
fetch: sinon.stub().returns(Promise.resolve({ status: 400 })),
text: sinon.stub().throws(),
json: sinon.stub().withArgs(CI_CRUMB_URL)
.returns(Promise.resolve({ crumb }))
Expand Down
21 changes: 21 additions & 0 deletions test/unit/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,27 @@ describe('cli', () => {
});
});

describe('prompt cares about spinner', () => {
beforeEach(() => {
stream = new LogStream();
cli = new CLI(stream, {
spinner: { isEnabled: true }
});
cli.setAssumeYes();
});

it('pauses when prompt a question', async() => {
cli.startSpinner('foo');
assert.deepEqual(cli.spinner.text, 'foo');
assert.deepEqual(cli.spinner.isSpinning, true);
await cli.prompt('So you think darkness is your ally?', {
defaultAnswer: 'Yes, I was born in it. Molded by it.',
questionType: cli.QUESTION_TYPE.INPUT
});
assert.strictEqual(cli.spinner.isSpinning, true);
});
});

describe('prompt assume yes', () => {
beforeEach(() => {
stream = new LogStream();
Expand Down