-
Notifications
You must be signed in to change notification settings - Fork 313
feat: add env-script #143
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
feat: add env-script #143
Changes from 2 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
6cc8c6f
feat: automate editing env files
Gowtham118 f4f1adc
chore: update about env script
Gowtham118 bb0fa6f
fix regex and add --update flag
Gowtham118 f9e7ca3
fix: don't delete .env-sample
Gowtham118 19cab9b
fix: make regex inclusive for local testing
Gowtham118 dfd2b87
reorg if case
Jason-W123 2d542ad
update custom network address
Jason-W123 3315ae4
Merge pull request #144 from OffchainLabs/update-custom-newtrok-address
Gowtham118 599a3d1
fix: TransferTo optional env
Gowtham118 93fe181
Merge branch 'feat/add-env-script' of https://github.com/OffchainLabs…
Gowtham118 f127cc1
update: block verification in parent chain assertion
Gowtham118 471b382
remove sdk version specific
Jason-W123 5ba44cb
use custom func insert
Jason-W123 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| #!/usr/bin/env node | ||
|
|
||
| /* | ||
| * Environment setup script for Arbitrum Tutorials. | ||
| * Usage: | ||
| * yarn setup-envs | ||
| */ | ||
|
|
||
| /* eslint-disable no-await-in-loop */ | ||
|
|
||
| const fs = require('fs'); | ||
| const path = require('path'); | ||
| const readline = require('readline'); | ||
|
|
||
| const VARS = ['PRIVATE_KEY', 'CHAIN_RPC', 'PARENT_CHAIN_RPC', 'L1_RPC']; | ||
|
|
||
| function log(msg) { | ||
| console.log(msg); | ||
| } | ||
| function warn(msg) { | ||
| console.warn(msg); | ||
| } | ||
| function error(msg) { | ||
| console.error(msg); | ||
| } | ||
|
|
||
| async function promptForValues() { | ||
| const values = {}; | ||
| const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); | ||
| const ask = (q) => new Promise((res) => rl.question(q, (ans) => res(ans.trim()))); | ||
| for (const v of VARS) { | ||
| const optional = v === 'L1_RPC'; | ||
| const existing = process.env[v] ? ` [default: ${process.env[v]}]` : ''; | ||
| const prompt = optional ? `${v} (optional)${existing}: ` : `${v}${existing}: `; | ||
| const ans = await ask(prompt); | ||
| if (ans) { | ||
| values[v] = ans; | ||
| } else if (process.env[v]) { | ||
| values[v] = process.env[v]; | ||
| } else if (!optional) { | ||
| error(`Required value missing: ${v}`); | ||
| process.exit(1); | ||
| } | ||
| } | ||
| rl.close(); | ||
| return values; | ||
| } | ||
|
|
||
| function replaceOrAppend(contentLines, key, value) { | ||
| const prefix = key + '='; | ||
| let replaced = false; | ||
| for (let i = 0; i < contentLines.length; i++) { | ||
| const line = contentLines[i]; | ||
| if (line.startsWith(prefix)) { | ||
| contentLines[i] = `${key}="${value}"`; | ||
| replaced = true; | ||
| break; | ||
| } | ||
| } | ||
| if (!replaced) contentLines.push(`${key}="${value}"`); | ||
| } | ||
|
|
||
| function processSampleFile(samplePath, envPath, values) { | ||
| const lines = fs.readFileSync(samplePath, 'utf8').split(/\r?\n/); | ||
| while (lines.length && lines[lines.length - 1].trim() === '') lines.pop(); | ||
| for (const v of VARS) { | ||
| if (values[v]) replaceOrAppend(lines, v, values[v]); | ||
| } | ||
|
|
||
| const newContent = lines.join('\n') + '\n'; | ||
| fs.writeFileSync(envPath, newContent, 'utf8'); | ||
| if (samplePath !== envPath) { | ||
| fs.unlinkSync(samplePath); // remove sample after successful creation | ||
| } | ||
| } | ||
|
|
||
| function processDirectory(dir, values, summary) { | ||
| const samplePath = path.join(dir, '.env-sample'); | ||
| const envPath = path.join(dir, '.env'); | ||
| const hasSample = fs.existsSync(samplePath); | ||
| const hasEnv = fs.existsSync(envPath); | ||
|
|
||
| if (!hasSample && !hasEnv) return; | ||
|
|
||
| try { | ||
| if (hasSample) { | ||
| processSampleFile(samplePath, envPath, values); | ||
| summary.updated.push(dir); | ||
| } else if (hasEnv) { | ||
| processSampleFile(envPath, envPath, values); | ||
|
Jason-W123 marked this conversation as resolved.
Outdated
|
||
| summary.updated.push(dir); | ||
| } | ||
| } catch (e) { | ||
| summary.errors.push({ dir, error: e.message }); | ||
| } | ||
| } | ||
|
|
||
| function validate(values) { | ||
| const pk = values.PRIVATE_KEY; | ||
| if (!/^0x[a-fA-F0-9]{64}$/.test(pk)) { | ||
| throw new Error('PRIVATE_KEY must be 0x + 64 hex characters.'); | ||
| } | ||
| ['CHAIN_RPC', 'PARENT_CHAIN_RPC'].forEach((k) => { | ||
| if (!/^https?:\/\/\\S+$/i.test(values[k])) { | ||
|
Jason-W123 marked this conversation as resolved.
Outdated
|
||
| throw new Error(`${k} must be an http(s) URL.`); | ||
| } | ||
| }); | ||
| if (values.L1_RPC && !/^https?:\/\/\\S+$/i.test(values.L1_RPC)) { | ||
|
Jason-W123 marked this conversation as resolved.
Outdated
|
||
| throw new Error('L1_RPC must be an http(s) URL if provided.'); | ||
| } | ||
| } | ||
|
|
||
| async function main() { | ||
| log('Arbitrum Tutorials environment setup starting...'); | ||
| const values = await promptForValues(); | ||
|
|
||
| if (!values.PRIVATE_KEY || !values.CHAIN_RPC || !values.PARENT_CHAIN_RPC) { | ||
| error('PRIVATE_KEY, CHAIN_RPC, and PARENT_CHAIN_RPC are required.'); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| validate(values); | ||
|
|
||
| const rootDir = path.resolve(__dirname, '..'); | ||
| const packagesDir = path.join(rootDir, 'packages'); | ||
|
|
||
| const summary = { updated: [], errors: [] }; | ||
|
|
||
| processDirectory(rootDir, values, summary); | ||
|
|
||
| if (fs.existsSync(packagesDir)) { | ||
| const entries = fs.readdirSync(packagesDir); | ||
| for (const entry of entries) { | ||
| const fullPath = path.join(packagesDir, entry); | ||
| if (fs.statSync(fullPath).isDirectory()) { | ||
| processDirectory(fullPath, values, summary); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| log('Environment setup complete.'); | ||
| log(`Updated: ${summary.updated.length}`); | ||
| if (summary.errors.length) { | ||
| warn('Errors encountered:'); | ||
| for (const e of summary.errors) warn(` - ${e.dir}: ${e.error}`); | ||
| } | ||
|
|
||
| log('\nExample: run a tutorial script after env creation:'); | ||
| log(' cd packages/greeter && npx hardhat run scripts/sendParentMessage.ts'); | ||
| } | ||
|
|
||
| main().catch((e) => { | ||
| error(e.stack || e.message); | ||
| process.exit(1); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.