From e151036d013f3c695f4028f8934964a99091c8ff Mon Sep 17 00:00:00 2001 From: Marcelo Shima Date: Thu, 9 Feb 2023 13:42:47 -0300 Subject: [PATCH] close prompt on error --- lib/adapter.js | 24 ++++++++++++++---------- lib/environment.js | 1 + 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/lib/adapter.js b/lib/adapter.js index 4a467b25..7a7c2cbb 100644 --- a/lib/adapter.js +++ b/lib/adapter.js @@ -37,6 +37,12 @@ class TerminalAdapter { return string.split('\n').map(line => this[`_colorDiff${name}`](line)).join('\n'); } + close() { + if (this.promptUi) { + this.promptUi.close(); + } + } + /** * Prompt a user for one or more questions and pass * the answer(s) to the provided callback. @@ -51,16 +57,14 @@ class TerminalAdapter { * @param {Function} [cb] Deprecated: callback for backward compatibility. * @return {Object} promise answers */ - prompt(questions, answers, cb) { - if (typeof answers === 'function') { - cb = answers; - answers = undefined; - } - const promise = this.promptModule(questions, answers); - if (typeof cb === 'function') { - promise.then(answers => cb(answers)); - } - return promise; + prompt(questions, answers, cb = () => {}) { + const promptPromise = this.promptModule(questions, typeof answers === 'function' ? undefined : answers); + this.promptUi = promptPromise.ui; + promptPromise.then(result => { + delete this.promptUi; + (typeof answers === 'function' ? answers : cb)(result); + }); + return promptPromise; } /** diff --git a/lib/environment.js b/lib/environment.js index 7a2b2b3f..23072f06 100644 --- a/lib/environment.js +++ b/lib/environment.js @@ -1058,6 +1058,7 @@ class Environment extends Base { this.runLoop.on('error', error => { this.emit('error', error); + this.adapter.close(); }); this.runLoop.on('paused', () => {