Skip to content

Commit

Permalink
Fix: cleanup when the prompt fails. (#947)
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima authored Jul 23, 2020
1 parent 96db664 commit 833a4cd
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
7 changes: 5 additions & 2 deletions packages/inquirer/lib/prompts/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,11 @@ class Prompt {
*/

run() {
return new Promise((resolve) => {
this._run((value) => resolve(value));
return new Promise((resolve, reject) => {
this._run(
(value) => resolve(value),
(error) => reject(error)
);
});
}

Expand Down
7 changes: 6 additions & 1 deletion packages/inquirer/lib/ui/prompt.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class PromptUI extends Base {
}, this.answers)
)
.toPromise(Promise)
.then(this.onCompletion.bind(this));
.then(this.onCompletion.bind(this), this.onError.bind(this));
}

/**
Expand All @@ -68,6 +68,11 @@ class PromptUI extends Base {
return this.answers;
}

onError(error) {
this.close();
return Promise.reject(error);
}

processQuestion(question) {
question = _.clone(question);
return defer(() => {
Expand Down
21 changes: 21 additions & 0 deletions packages/inquirer/test/specs/inquirer.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,27 @@ describe('inquirer.prompt', function () {
});
});

it('should close readline instance on rejected promise', function (done) {
this.prompt.registerPrompt('stub', function () {
this.run = () => {
return Promise.reject(new Error('test error'));
};
});

var promise = this.prompt({
type: 'stub',
name: 'q1',
});

var rl1 = promise.ui.rl;

promise.catch(() => {
expect(rl1.close.called).to.equal(true);
expect(rl1.output.end.called).to.equal(true);
done();
});
});

it('should take a prompts array and return answers', function () {
var prompts = [
{
Expand Down

0 comments on commit 833a4cd

Please sign in to comment.