From cc1f4e0ae047c61490cf186110972b0325740d40 Mon Sep 17 00:00:00 2001 From: Marcelo Shima Date: Mon, 2 Sep 2024 16:16:05 -0300 Subject: [PATCH] chore: simplify promptModule return (#1533) Co-authored-by: Simon Boudrias --- packages/inquirer/src/index.mts | 9 ++------- packages/inquirer/src/ui/prompt.mts | 29 ++++++----------------------- 2 files changed, 8 insertions(+), 30 deletions(-) diff --git a/packages/inquirer/src/index.mts b/packages/inquirer/src/index.mts index 6bedbeaf6..0de17eb7f 100644 --- a/packages/inquirer/src/index.mts +++ b/packages/inquirer/src/index.mts @@ -93,13 +93,8 @@ export function createPromptModule(opt?: StreamOptions) { ): PromptReturnType { const runner = new PromptsRunner(promptModule.prompts, opt); - try { - return runner.run(questions, answers); - } catch (error) { - // eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors - const promise = Promise.reject(error); - return Object.assign(promise, { ui: runner }); - } + const promptPromise = runner.run(questions, answers); + return Object.assign(promptPromise, { ui: runner }); } promptModule.prompts = { ...defaultPrompts }; diff --git a/packages/inquirer/src/ui/prompt.mts b/packages/inquirer/src/ui/prompt.mts index 7d4d65fe5..79ad4c297 100644 --- a/packages/inquirer/src/ui/prompt.mts +++ b/packages/inquirer/src/ui/prompt.mts @@ -212,14 +212,14 @@ export default class PromptsRunner { this.prompts = prompts; } - run( + async run( questions: | QuestionArray | QuestionAnswerMap | QuestionObservable | Question, answers?: Partial, - ): Promise & { ui: PromptsRunner } { + ): Promise { // Keep global reference to the answers this.answers = typeof answers === 'object' ? { ...answers } : {}; @@ -244,33 +244,16 @@ export default class PromptsRunner { this.process = obs.pipe(concatMap((question) => this.processQuestion(question))); - const promise = lastValueFrom( + return lastValueFrom( this.process.pipe( reduce((answersObj, answer: { name: string; answer: unknown }) => { _.set(answersObj, answer.name, answer.answer); return answersObj; }, this.answers), ), - ).then( - () => this.onCompletion(), - (error: Error) => this.onError(error), - ) as Promise; - - return Object.assign(promise, { ui: this }); - } - - /** - * Once all prompt are over - */ - onCompletion() { - this.close(); - - return this.answers; - } - - onError(error: Error) { - this.close(); - return Promise.reject(error); + ) + .then(() => this.answers as A) + .finally(() => this.close()); } processQuestion(question: Question) {