33/*
44 * Environment setup script for Arbitrum Tutorials.
55 * Usage:
6- * yarn setup-envs
6+ * yarn setup-envs # prompts for values (L1_RPC optional), does NOT overwrite existing .env
7+ * yarn setup-envs --update # prompts and updates existing .env files as well
78 */
89
910/* eslint-disable no-await-in-loop */
@@ -13,6 +14,8 @@ const path = require('path');
1314const readline = require ( 'readline' ) ;
1415
1516const VARS = [ 'PRIVATE_KEY' , 'CHAIN_RPC' , 'PARENT_CHAIN_RPC' , 'L1_RPC' ] ;
17+ const ARGS = new Set ( process . argv . slice ( 2 ) ) ;
18+ const UPDATE_EXISTING = ARGS . has ( '--update' ) ;
1619
1720function log ( msg ) {
1821 console . log ( msg ) ;
@@ -87,8 +90,12 @@ function processDirectory(dir, values, summary) {
8790 processSampleFile ( samplePath , envPath , values ) ;
8891 summary . updated . push ( dir ) ;
8992 } else if ( hasEnv ) {
90- processSampleFile ( envPath , envPath , values ) ;
91- summary . updated . push ( dir ) ;
93+ if ( UPDATE_EXISTING ) {
94+ processSampleFile ( envPath , envPath , values ) ;
95+ summary . updated . push ( dir ) ;
96+ } else {
97+ summary . skipped . push ( dir ) ;
98+ }
9299 }
93100 } catch ( e ) {
94101 summary . errors . push ( { dir, error : e . message } ) ;
@@ -101,11 +108,11 @@ function validate(values) {
101108 throw new Error ( 'PRIVATE_KEY must be 0x + 64 hex characters.' ) ;
102109 }
103110 [ 'CHAIN_RPC' , 'PARENT_CHAIN_RPC' ] . forEach ( ( k ) => {
104- if ( ! / ^ h t t p s ? : \/ \/ \\ S + $ / i. test ( values [ k ] ) ) {
111+ if ( ! / ^ h t t p s ? : \/ \/ \S + $ / i. test ( values [ k ] ) ) {
105112 throw new Error ( `${ k } must be an http(s) URL.` ) ;
106113 }
107114 } ) ;
108- if ( values . L1_RPC && ! / ^ h t t p s ? : \/ \/ \\ S + $ / i. test ( values . L1_RPC ) ) {
115+ if ( values . L1_RPC && ! / ^ h t t p s ? : \/ \/ \S + $ / i. test ( values . L1_RPC ) ) {
109116 throw new Error ( 'L1_RPC must be an http(s) URL if provided.' ) ;
110117 }
111118}
@@ -124,7 +131,7 @@ async function main() {
124131 const rootDir = path . resolve ( __dirname , '..' ) ;
125132 const packagesDir = path . join ( rootDir , 'packages' ) ;
126133
127- const summary = { updated : [ ] , errors : [ ] } ;
134+ const summary = { updated : [ ] , skipped : [ ] , errors : [ ] } ;
128135
129136 processDirectory ( rootDir , values , summary ) ;
130137
@@ -140,6 +147,8 @@ async function main() {
140147
141148 log ( 'Environment setup complete.' ) ;
142149 log ( `Updated: ${ summary . updated . length } ` ) ;
150+ if ( summary . skipped . length )
151+ log ( `Skipped (existing .env, no --update): ${ summary . skipped . length } ` ) ;
143152 if ( summary . errors . length ) {
144153 warn ( 'Errors encountered:' ) ;
145154 for ( const e of summary . errors ) warn ( ` - ${ e . dir } : ${ e . error } ` ) ;
0 commit comments