Skip to content

Commit bb0fa6f

Browse files
committed
fix regex and add --update flag
1 parent f4f1adc commit bb0fa6f

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ To simplify providing environment variables across all tutorials, a helper scrip
1818

1919
```bash
2020
yarn setup-envs
21+
22+
# To update existing .env files as well
23+
yarn setup-envs --update
2124
```
2225

2326
## Testing

scripts/setup-envs.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
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');
1314
const readline = require('readline');
1415

1516
const 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

1720
function 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 (!/^https?:\/\/\\S+$/i.test(values[k])) {
111+
if (!/^https?:\/\/\S+$/i.test(values[k])) {
105112
throw new Error(`${k} must be an http(s) URL.`);
106113
}
107114
});
108-
if (values.L1_RPC && !/^https?:\/\/\\S+$/i.test(values.L1_RPC)) {
115+
if (values.L1_RPC && !/^https?:\/\/\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

Comments
 (0)