Skip to content

Commit 2343be2

Browse files
committed
handle skipped steps on escape
1 parent f75cbd5 commit 2343be2

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ export class PromptManager {
2828

2929
while (this.currentStep < this.steps.length) {
3030
try {
31-
const result = await prompt.ask(this.steps[this.currentStep]);
32-
this.results[this.currentStep] = result;
31+
this.results[this.currentStep] = await prompt.ask(this.steps[this.currentStep]);
3332
this.currentStep++;
3433
} catch (error) {
3534
if (error instanceof Error) {
@@ -42,6 +41,12 @@ export class PromptManager {
4241
// delete 2 lines
4342
process.stdout.write('\x1b[1A\x1b[2K\x1b[1A\x1b[2K');
4443
this.currentStep--;
44+
while (this.currentStep > 0) {
45+
const skip = this.steps[this.currentStep].skip;
46+
const shouldSkip = typeof skip === 'function' ? await skip({}) : skip;
47+
if (!shouldSkip) break;
48+
this.currentStep--;
49+
}
4550
}
4651
}
4752

packages/cli/src/commands/init.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,7 @@ async function processInitForm(
529529
name: 'subgraphName',
530530
message: 'Subgraph slug',
531531
initial: initSubgraphName,
532+
validate: value => value.length > 0 || 'Subgraph slug must not be empty',
532533
result: value => {
533534
initDebugger.extend('processInitForm')('subgraphName: %O', value);
534535
subgraphName = value;
@@ -541,6 +542,7 @@ async function processInitForm(
541542
name: 'directory',
542543
message: 'Directory to create the subgraph in',
543544
initial: () => initDirectory || getSubgraphBasename(subgraphName),
545+
validate: value => value.length > 0 || 'Directory must not be empty',
544546
result: value => {
545547
directory = value;
546548
initDebugger.extend('processInitForm')('directory: %O', value);
@@ -707,7 +709,11 @@ async function processInitForm(
707709
message: 'Start block',
708710
initial: () => initStartBlock || startBlock || '0',
709711
skip: () => initFromExample !== undefined || isSubstreams,
710-
validate: value => parseInt(value) >= 0,
712+
validate: value =>
713+
initFromExample !== undefined ||
714+
isSubstreams ||
715+
parseInt(value) >= 0 ||
716+
'Invalid start block',
711717
result: value => {
712718
startBlock = value;
713719
initDebugger.extend('processInitForm')('startBlock: %O', value);
@@ -721,7 +727,12 @@ async function processInitForm(
721727
message: 'Contract name',
722728
initial: () => initContractName || contractName || 'Contract',
723729
skip: () => initFromExample !== undefined || !protocolInstance.hasContract() || isSubstreams,
724-
validate: value => value && value.length > 0,
730+
validate: value =>
731+
initFromExample !== undefined ||
732+
!protocolInstance.hasContract() ||
733+
isSubstreams ||
734+
value.length > 0 ||
735+
'Contract name must not be empty',
725736
result: value => {
726737
contractName = value;
727738
initDebugger.extend('processInitForm')('contractName: %O', value);
@@ -742,8 +753,7 @@ async function processInitForm(
742753
},
743754
});
744755

745-
const results = await promptManager.executeInteractive();
746-
console.log('results', results);
756+
await promptManager.executeInteractive();
747757

748758
return {
749759
abi: (abiFromApi || abiFromFile)!,
@@ -761,7 +771,6 @@ async function processInitForm(
761771
cleanup: spkgCleanup,
762772
};
763773
} catch (e) {
764-
console.error(e);
765774
this.error(e, { exit: 1 });
766775
}
767776
}

0 commit comments

Comments
 (0)