Skip to content

Commit f75cbd5

Browse files
committed
simplify prompt manager, add non-interactive mode
1 parent 35f0cb8 commit f75cbd5

File tree

2 files changed

+12
-15
lines changed

2 files changed

+12
-15
lines changed

packages/cli/src/command-helpers/prompt-manager.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,22 @@
11
import { prompt } from 'gluegun';
22
import { PromptOptions } from 'gluegun/build/types/toolbox/prompt-enquirer-types';
33

4-
interface PromptStep {
5-
execute: () => Promise<any>;
6-
}
7-
84
export class PromptManager {
9-
private steps: PromptStep[] = [];
5+
private steps: PromptOptions[] = [];
106
private currentStep = 0;
117
private results: any[] = [];
128

139
addStep(options: PromptOptions) {
14-
this.steps.push({
15-
execute: async () => {
16-
const result = await prompt.ask(options);
17-
return result;
18-
},
19-
});
10+
this.steps.push(options);
11+
}
12+
13+
// runs all steps and returns the results
14+
async execute() {
15+
return prompt.ask(this.steps);
2016
}
2117

22-
async executePrompts() {
18+
// allows going back with Escape and returns the results
19+
async executeInteractive() {
2320
let isCtrlC = false;
2421
// gluegun always throws empty string, so we have this workaround
2522
const keypressHandler = (_: string, key: { name: string; ctrl: boolean }) => {
@@ -31,7 +28,7 @@ export class PromptManager {
3128

3229
while (this.currentStep < this.steps.length) {
3330
try {
34-
const result = await this.steps[this.currentStep].execute();
31+
const result = await prompt.ask(this.steps[this.currentStep]);
3532
this.results[this.currentStep] = result;
3633
this.currentStep++;
3734
} catch (error) {

packages/cli/src/commands/init.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -742,8 +742,7 @@ async function processInitForm(
742742
},
743743
});
744744

745-
// Execute all prompts with ESC navigation support
746-
const results = await promptManager.executePrompts();
745+
const results = await promptManager.executeInteractive();
747746
console.log('results', results);
748747

749748
return {
@@ -762,6 +761,7 @@ async function processInitForm(
762761
cleanup: spkgCleanup,
763762
};
764763
} catch (e) {
764+
console.error(e);
765765
this.error(e, { exit: 1 });
766766
}
767767
}

0 commit comments

Comments
 (0)