From 623019a62689bcbf2630e6e298d9f03b5c21a6a8 Mon Sep 17 00:00:00 2001 From: Marcelo Shima Date: Thu, 25 May 2023 18:33:19 -0300 Subject: [PATCH] stub inquirer's registerPrompt to allow custom prompt modules --- src/adapter.ts | 16 ++++++++++++---- src/helpers.ts | 12 +++--------- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/adapter.ts b/src/adapter.ts index c7355e2..86489e3 100644 --- a/src/adapter.ts +++ b/src/adapter.ts @@ -77,6 +77,7 @@ export class TestAdapter implements InputOutputAdapter { promptModule: PromptModule; diff: any; log: Logger; + registerDummyPrompt: (promptName: string, customPromptOptions?: DummyPromptOptions) => PromptModule; constructor(options: TestAdapterOptions = {}) { const { log = createLogger(), ...promptOptions } = options; @@ -86,15 +87,22 @@ export class TestAdapter implements InputOutputAdapter { skipTTYChecks: true, }); - for (const promptName of Object.keys(this.promptModule.prompts)) { - this.promptModule.registerPrompt( - promptName, + const actualRegisterPrompt = this.promptModule.registerPrompt.bind(this.promptModule); + + this.registerDummyPrompt = (promptModuleName: string, customPromptOptions?: DummyPromptOptions) => + actualRegisterPrompt( + promptModuleName, class CustomDummyPrompt extends DummyPrompt { constructor(question: PromptQuestion, rl: any, answers: PromptAnswers) { - super(question, rl, answers, promptOptions); + super(question, rl, answers, customPromptOptions ?? promptOptions); } } as any, ); + + this.promptModule.registerPrompt = (name: string) => this.registerDummyPrompt(name); + + for (const promptName of Object.keys(this.promptModule.prompts)) { + this.promptModule.registerPrompt(promptName, undefined as any); } this.diff = sinonSpy(); diff --git a/src/helpers.ts b/src/helpers.ts index abab896..ac6fbfc 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -115,17 +115,11 @@ export class YeomanTest { throw new Error('environment is not an Environment instance'); } - const { promptModule } = environment.adapter as TestAdapter; + const testAdapter = environment.adapter as TestAdapter; + const { promptModule } = testAdapter; for (const name of Object.keys(promptModule.prompts)) { - promptModule.registerPrompt( - name, - class CustomDummyPrompt extends DummyPrompt { - constructor(question: PromptQuestion, rl: any, answers: PromptAnswers) { - super(question, rl, answers, { ...options, mockedAnswers }); - } - } as any, - ); + testAdapter.registerDummyPrompt(name, { ...options, mockedAnswers }); } }