11const fs = require ( "fs" ) ;
22const path = require ( "path" ) ;
33const chainConfigs = require ( "../chainOptions.json" ) ;
4+ const readline = require ( "readline" ) ;
45
56// Parse command line arguments
67const args = process . argv . slice ( 2 ) ;
78const chainIndex = args . indexOf ( "-c" ) ;
89if ( chainIndex === - 1 || ! args [ chainIndex + 1 ] ) {
9- console . error ( "Error: Please provide a chain shortname using -c flag" ) ;
10- process . exit ( 1 ) ;
10+ const availableChains = chainConfigs
11+ . map ( ( chain ) => ` [${ chain . shortName } ] ${ chain . displayName } ` )
12+ . join ( "\n" ) ;
13+ const exampleShortName = chainConfigs [ 0 ] ?. shortName || "<shortname>" ;
14+ const exampleUsage = `\nExample: yarn chain -c ${ exampleShortName } ` ;
15+ console . log (
16+ "No chain shortname provided with -c flag.\nAvailable chains (use the value in [brackets]):\n" +
17+ exampleUsage +
18+ availableChains
19+ ) ;
20+
21+ const rl = readline . createInterface ( {
22+ input : process . stdin ,
23+ output : process . stdout ,
24+ } ) ;
25+
26+ rl . question ( "\nPlease enter a chain shortname: " , function ( answer ) {
27+ const chainShortName = answer . trim ( ) ;
28+ const chainConfig = chainConfigs . find (
29+ ( chain ) => chain . shortName === chainShortName
30+ ) ;
31+ if ( ! chainConfig ) {
32+ console . error (
33+ `Error: Chain with shortname "${ chainShortName } " not found`
34+ ) ;
35+ rl . close ( ) ;
36+ process . exit ( 1 ) ;
37+ }
38+ writeChainConfig ( chainConfig ) ;
39+ rl . close ( ) ;
40+ process . exit ( 0 ) ;
41+ } ) ;
42+ return ;
1143}
1244
1345const chainShortName = args [ chainIndex + 1 ] ;
@@ -22,22 +54,44 @@ if (!chainConfig) {
2254 process . exit ( 1 ) ;
2355}
2456
25- // Create the new configuration object
26- const newConfig = {
27- mainnetName : chainConfig . mainnetName ,
28- mainnetChainId : chainConfig . mainnetChainId ,
29- testnetChainId : chainConfig . testnetChainId ,
30- testnetChainName : chainConfig . testnetChainName ,
31- } ;
32-
33- // Write to chainConfig.json
34- const configPath = path . join ( __dirname , ".." , "chainConfig.json" ) ;
35- try {
36- fs . writeFileSync ( configPath , JSON . stringify ( newConfig , null , 2 ) ) ;
37- console . log (
38- `Successfully updated chain configuration to ${ chainConfig . displayName } `
57+ writeChainConfig ( chainConfig ) ;
58+
59+ function writeChainConfig ( chainConfig ) {
60+ const newConfig = {
61+ mainnetName : chainConfig . mainnetName ,
62+ mainnetChainId : chainConfig . mainnetChainId ,
63+ testnetChainId : chainConfig . testnetChainId ,
64+ testnetChainName : chainConfig . testnetChainName ,
65+ } ;
66+ const nextjsConfigPath = path . join (
67+ __dirname ,
68+ ".." ,
69+ ".." ,
70+ "packages" ,
71+ "nextjs" ,
72+ "config" ,
73+ "chainConfig.json"
3974 ) ;
40- } catch ( error ) {
41- console . error ( "Error writing to chainConfig.json:" , error . message ) ;
42- process . exit ( 1 ) ;
75+ const hardhatConfigPath = path . join (
76+ __dirname ,
77+ ".." ,
78+ ".." ,
79+ "packages" ,
80+ "hardhat" ,
81+ "config" ,
82+ "chainConfig.json"
83+ ) ;
84+ try {
85+ fs . writeFileSync ( nextjsConfigPath , JSON . stringify ( newConfig , null , 2 ) ) ;
86+ fs . writeFileSync ( hardhatConfigPath , JSON . stringify ( newConfig , null , 2 ) ) ;
87+ console . log (
88+ `Successfully updated chain configuration to ${ chainConfig . displayName } in both nextjs and hardhat packages`
89+ ) ;
90+ } catch ( error ) {
91+ console . error (
92+ "Error writing to chainConfig.json in one or both locations:" ,
93+ error . message
94+ ) ;
95+ process . exit ( 1 ) ;
96+ }
4397}
0 commit comments