Skip to content

Commit

Permalink
chore: simplify promptModule return (#1533)
Browse files Browse the repository at this point in the history
Co-authored-by: Simon Boudrias <[email protected]>
  • Loading branch information
mshima and SBoudrias authored Sep 2, 2024
1 parent 83f771a commit cc1f4e0
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 30 deletions.
9 changes: 2 additions & 7 deletions packages/inquirer/src/index.mts
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,8 @@ export function createPromptModule(opt?: StreamOptions) {
): PromptReturnType<Answers> {
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 };
Expand Down
29 changes: 6 additions & 23 deletions packages/inquirer/src/ui/prompt.mts
Original file line number Diff line number Diff line change
Expand Up @@ -212,14 +212,14 @@ export default class PromptsRunner<A extends Answers> {
this.prompts = prompts;
}

run(
async run(
questions:
| QuestionArray<A>
| QuestionAnswerMap<A>
| QuestionObservable<A>
| Question<A>,
answers?: Partial<A>,
): Promise<A> & { ui: PromptsRunner<A> } {
): Promise<A> {
// Keep global reference to the answers
this.answers = typeof answers === 'object' ? { ...answers } : {};

Expand All @@ -244,33 +244,16 @@ export default class PromptsRunner<A extends Answers> {

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<A>;

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<A>) {
Expand Down

0 comments on commit cc1f4e0

Please sign in to comment.