Skip to content

Commit d3c442e

Browse files
committed
refactor init prompts
1 parent 78aaa25 commit d3c442e

File tree

1 file changed

+98
-61
lines changed

1 file changed

+98
-61
lines changed

packages/cli/src/commands/init.ts

Lines changed: 98 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -425,10 +425,6 @@ async function processInitForm(
425425
}
426426
| undefined
427427
> {
428-
let abiFromEtherscan: EthereumABI | undefined = undefined;
429-
let startBlockFromEtherscan: string | undefined = undefined;
430-
let contractNameFromEtherscan: string | undefined = undefined;
431-
432428
try {
433429
const registry = await NetworksRegistry.fromLatestVersion();
434430
const contractService = new ContractService(registry);
@@ -461,11 +457,26 @@ async function processInitForm(
461457
];
462458
};
463459

464-
const { networkId } = await prompt.ask<{ networkId: string }>({
460+
let network: Network = networks[0];
461+
let protocolInstance: Protocol = new Protocol('ethereum');
462+
let isComposedSubgraph = false;
463+
let isSubstreams = false;
464+
let subgraphName = initSubgraphName ?? '';
465+
let directory = initDirectory;
466+
let ipfsNode: string = '';
467+
let source = initContract;
468+
let contractName = initContractName;
469+
let abiFromFile: EthereumABI | undefined = undefined;
470+
let abiFromApi: EthereumABI | undefined = undefined;
471+
let startBlock: string | undefined = undefined;
472+
let spkgPath: string | undefined;
473+
let spkgCleanup: (() => void) | undefined;
474+
let indexEvents = initIndexEvents;
475+
476+
await prompt.ask({
465477
type: 'autocomplete',
466478
name: 'networkId',
467479
required: true,
468-
linebreak: true,
469480
message: 'Network',
470481
choices: formatChoices(networks.map(networkToChoice)),
471482
format: value => `${value}`,
@@ -476,11 +487,14 @@ async function processInitForm(
476487
.filter(({ value }) => (value ?? '').includes(input.toLowerCase())),
477488
),
478489
validate: value => (networks.find(n => n.id === value) ? true : 'Pick a network'),
490+
result: value => {
491+
initDebugger.extend('processInitForm')('networkId: %O', value);
492+
network = networks.find(n => n.id === value)!;
493+
return value;
494+
},
479495
});
480496

481-
const network = networks.find(n => n.id === networkId)!;
482-
483-
const { protocol } = await prompt.ask<{ protocol: string }>({
497+
await prompt.ask({
484498
type: 'select',
485499
name: 'protocol',
486500
message: 'Source',
@@ -498,42 +512,50 @@ async function processInitForm(
498512
}
499513
return true;
500514
},
515+
result: protocol => {
516+
protocolInstance = new Protocol(protocol);
517+
isComposedSubgraph = protocolInstance.isComposedSubgraph();
518+
isSubstreams = protocolInstance.isSubstreams();
519+
initDebugger.extend('processInitForm')('protocol: %O', protocol);
520+
return protocol;
521+
},
501522
});
502523

503-
initDebugger.extend('processInitForm')('protocol: %O', protocol);
504-
505-
const protocolInstance = new Protocol(protocol);
506-
const isComposedSubgraph = protocolInstance.isComposedSubgraph();
507-
const isSubstreams = protocolInstance.isSubstreams();
508-
initDebugger.extend('processInitForm')('isSubstreams: %O', isSubstreams);
509-
510-
const { subgraphName } = await prompt.ask<{ subgraphName: string }>([
524+
await prompt.ask([
511525
{
512526
type: 'input',
513527
name: 'subgraphName',
514528
message: 'Subgraph slug',
515529
initial: initSubgraphName,
530+
result: value => {
531+
initDebugger.extend('processInitForm')('subgraphName: %O', value);
532+
subgraphName = value;
533+
return value;
534+
},
516535
},
517536
]);
518537

519-
const { directory } = await prompt.ask<{ directory: string }>([
538+
await prompt.ask([
520539
{
521540
type: 'input',
522541
name: 'directory',
523542
message: 'Directory to create the subgraph in',
524543
initial: () => initDirectory || getSubgraphBasename(subgraphName),
544+
result: value => {
545+
directory = value;
546+
initDebugger.extend('processInitForm')('directory: %O', value);
547+
return value;
548+
},
525549
},
526550
]);
527551

528-
const sourceMessage = isComposedSubgraph
529-
? 'Source subgraph deployment ID'
530-
: `Contract ${protocolInstance.getContract()?.identifierName()}`;
531-
532-
const { source } = await prompt.ask<{ source: string }>([
552+
await prompt.ask([
533553
{
534554
type: 'input',
535555
name: 'source',
536-
message: sourceMessage,
556+
message: isComposedSubgraph
557+
? 'Source subgraph deployment ID'
558+
: `Contract ${protocolInstance.getContract()?.identifierName()}`,
537559
skip: () =>
538560
initFromExample !== undefined ||
539561
isSubstreams ||
@@ -551,58 +573,60 @@ async function processInitForm(
551573
return valid ? true : error;
552574
},
553575
result: async (address: string) => {
576+
initDebugger.extend('processInitForm')("source: '%s'", address);
554577
if (
555578
initFromExample !== undefined ||
556579
initAbiPath ||
557580
protocolInstance.name !== 'ethereum' // we can only validate against Etherscan API
558581
) {
559-
initDebugger("value: '%s'", address);
582+
source = address;
560583
return address;
561584
}
562585

563586
// If ABI is not provided, try to fetch it from Etherscan API
564587
if (protocolInstance.hasABIs() && !initAbi) {
565-
abiFromEtherscan = await retryWithPrompt(() =>
566-
contractService.getABI(protocolInstance.getABI(), networkId, address),
588+
abiFromApi = await retryWithPrompt(() =>
589+
contractService.getABI(protocolInstance.getABI(), network.id, address),
567590
);
591+
initDebugger.extend('processInitForm')("abiFromEtherscan len: '%s'", abiFromApi?.name);
568592
}
569593
// If startBlock is not provided, try to fetch it from Etherscan API
570594
if (!initStartBlock) {
571-
const startBlock = await retryWithPrompt(() =>
572-
contractService.getStartBlock(networkId, address),
595+
startBlock = await retryWithPrompt(() =>
596+
contractService.getStartBlock(network.id, address),
573597
);
574-
if (startBlock) {
575-
startBlockFromEtherscan = Number(startBlock).toString();
576-
}
598+
initDebugger.extend('processInitForm')("startBlockFromEtherscan: '%s'", startBlock);
577599
}
578600

579601
// If contract name is not provided, try to fetch it from Etherscan API
580602
if (!initContractName) {
581-
const contractName = await retryWithPrompt(() =>
582-
contractService.getContractName(networkId, address),
603+
contractName = await retryWithPrompt(() =>
604+
contractService.getContractName(network.id, address),
583605
);
584-
if (contractName) {
585-
contractNameFromEtherscan = contractName;
586-
}
606+
initDebugger.extend('processInitForm')("contractNameFromEtherscan: '%s'", contractName);
587607
}
588608

609+
source = address;
589610
return address;
590611
},
591612
},
592613
]);
593614

594-
const { ipfs } = await prompt.ask<{ ipfs: string }>([
615+
await prompt.ask([
595616
{
596617
type: 'input',
597618
name: 'ipfs',
598619
message: `IPFS node to use for fetching subgraph manifest`,
599620
initial: ipfsUrl,
600621
skip: () => !isComposedSubgraph,
622+
result: value => {
623+
ipfsNode = value;
624+
initDebugger.extend('processInitForm')('ipfs: %O', value);
625+
return value;
626+
},
601627
},
602628
]);
603629

604-
let spkgPath: string | undefined;
605-
let spkgCleanup: (() => void) | undefined;
606630
await prompt.ask<{ spkg: string }>([
607631
{
608632
type: 'input',
@@ -621,6 +645,7 @@ async function processInitForm(
621645
const { path, cleanup } = await resolveFile(value, 'substreams.spkg', 10_000);
622646
spkgPath = path;
623647
spkgCleanup = cleanup;
648+
initDebugger.extend('processInitForm')('spkgPath: %O', path);
624649
return true;
625650
} catch (e) {
626651
return e.message;
@@ -631,7 +656,7 @@ async function processInitForm(
631656
},
632657
]);
633658

634-
const { abi: abiFromFile } = await prompt.ask<{ abi: EthereumABI }>([
659+
await prompt.ask([
635660
{
636661
type: 'input',
637662
name: 'abi',
@@ -640,14 +665,14 @@ async function processInitForm(
640665
skip: () =>
641666
!protocolInstance.hasABIs() ||
642667
initFromExample !== undefined ||
643-
abiFromEtherscan !== undefined ||
668+
abiFromApi !== undefined ||
644669
isSubstreams ||
645670
!!initAbiPath ||
646671
isComposedSubgraph,
647672
validate: async (value: string) => {
648673
if (
649674
initFromExample ||
650-
abiFromEtherscan ||
675+
abiFromApi ||
651676
!protocolInstance.hasABIs() ||
652677
isSubstreams ||
653678
isComposedSubgraph
@@ -666,72 +691,84 @@ async function processInitForm(
666691
}
667692
},
668693
result: async (value: string) => {
669-
if (
670-
initFromExample ||
671-
abiFromEtherscan ||
672-
!protocolInstance.hasABIs() ||
673-
isComposedSubgraph
674-
) {
694+
initDebugger.extend('processInitForm')('abiFromFile: %O', value);
695+
if (initFromExample || abiFromApi || !protocolInstance.hasABIs() || isComposedSubgraph) {
675696
return null;
676697
}
677698

678699
const ABI = protocolInstance.getABI();
679700
if (initAbiPath) value = initAbiPath;
680701

681702
try {
682-
return loadAbiFromFile(ABI, value);
703+
abiFromFile = loadAbiFromFile(ABI, value);
704+
return value;
683705
} catch (e) {
684706
return e.message;
685707
}
686708
},
687709
},
688710
]);
689711

690-
const { startBlock } = await prompt.ask<{ startBlock: string }>([
712+
await prompt.ask([
691713
{
692714
type: 'input',
693715
name: 'startBlock',
694716
message: 'Start block',
695-
initial: initStartBlock || startBlockFromEtherscan || '0',
717+
initial: initStartBlock || startBlock || '0',
696718
skip: () => initFromExample !== undefined || isSubstreams,
697719
validate: value => parseInt(value) >= 0,
720+
result: value => {
721+
startBlock = value;
722+
initDebugger.extend('processInitForm')('startBlock: %O', value);
723+
return value;
724+
},
698725
},
699726
]);
700727

701-
const { contractName } = await prompt.ask<{ contractName: string }>([
728+
await prompt.ask([
702729
{
703730
type: 'input',
704731
name: 'contractName',
705732
message: 'Contract name',
706-
initial: initContractName || contractNameFromEtherscan || 'Contract',
733+
initial: initContractName || 'Contract',
707734
skip: () =>
708735
initFromExample !== undefined || !protocolInstance.hasContract() || isSubstreams,
709736
validate: value => value && value.length > 0,
737+
result: value => {
738+
contractName = value;
739+
initDebugger.extend('processInitForm')('contractName: %O', value);
740+
return value;
741+
},
710742
},
711743
]);
712744

713-
const { indexEvents } = await prompt.ask<{ indexEvents: boolean }>([
745+
await prompt.ask([
714746
{
715747
type: 'confirm',
716748
name: 'indexEvents',
717749
message: 'Index contract events as entities',
718750
initial: true,
719751
skip: () => !!initIndexEvents || isSubstreams || isComposedSubgraph,
752+
result: value => {
753+
indexEvents = value === 'true';
754+
initDebugger.extend('processInitForm')('indexEvents: %O', value);
755+
return value;
756+
},
720757
},
721758
]);
722759

723760
return {
724-
abi: abiFromEtherscan || abiFromFile,
761+
abi: (abiFromApi || abiFromFile)!,
725762
protocolInstance,
726763
subgraphName,
727-
directory,
728-
startBlock,
764+
directory: directory!,
765+
startBlock: startBlock!,
729766
fromExample: !!initFromExample,
730767
network: network.id,
731-
contractName,
732-
source,
768+
contractName: contractName!,
769+
source: source!,
733770
indexEvents,
734-
ipfs,
771+
ipfs: ipfsNode,
735772
spkgPath,
736773
cleanup: spkgCleanup,
737774
};

0 commit comments

Comments
 (0)