|
| 1 | +#!/usr/bin/env node |
| 2 | +/* eslint-disable no-console */ |
| 3 | + |
| 4 | +const fs = require("fs"); |
| 5 | +const path = require("path"); |
| 6 | + |
| 7 | +function parseArgs(argv) { |
| 8 | + const args = { require: null, help: false }; |
| 9 | + for (let i = 0; i < argv.length; i++) { |
| 10 | + const a = argv[i]; |
| 11 | + if (a === "-h" || a === "--help") args.help = true; |
| 12 | + else if (a === "--require") args.require = argv[++i] || ""; |
| 13 | + else if (a.startsWith("--require=")) args.require = a.slice("--require=".length); |
| 14 | + } |
| 15 | + return args; |
| 16 | +} |
| 17 | + |
| 18 | +function usage() { |
| 19 | + return `Usage: |
| 20 | + node scripts/check-env.js |
| 21 | + node scripts/check-env.js --require BASE_RPC_URL,MATIC_URL |
| 22 | +
|
| 23 | +Defaults: |
| 24 | + Checks BASE_RPC_URL (needed for fork-based Hardhat tests).`; |
| 25 | +} |
| 26 | + |
| 27 | +function main() { |
| 28 | + const args = parseArgs(process.argv.slice(2)); |
| 29 | + if (args.help) { |
| 30 | + console.log(usage()); |
| 31 | + process.exit(0); |
| 32 | + } |
| 33 | + |
| 34 | + const repoRoot = path.resolve(__dirname, ".."); |
| 35 | + const envPath = path.join(repoRoot, ".env"); |
| 36 | + |
| 37 | + if (fs.existsSync(envPath)) { |
| 38 | + require("dotenv").config({ path: envPath }); |
| 39 | + } |
| 40 | + |
| 41 | + const required = |
| 42 | + args.require && args.require.trim().length > 0 |
| 43 | + ? args.require |
| 44 | + .split(",") |
| 45 | + .map((s) => s.trim()) |
| 46 | + .filter(Boolean) |
| 47 | + : ["BASE_RPC_URL"]; |
| 48 | + |
| 49 | + const missing = required.filter((k) => { |
| 50 | + const v = process.env[k]; |
| 51 | + return typeof v !== "string" || v.trim().length === 0; |
| 52 | + }); |
| 53 | + |
| 54 | + if (missing.length > 0) { |
| 55 | + console.error(`Missing required environment variables: ${missing.join(", ")}`); |
| 56 | + console.error("Create a local .env file (ignored by git) with these values."); |
| 57 | + |
| 58 | + const envExamplePath = path.join(repoRoot, ".env.example"); |
| 59 | + if (fs.existsSync(envExamplePath)) { |
| 60 | + console.error("Tip: cp .env.example .env"); |
| 61 | + } |
| 62 | + |
| 63 | + process.exit(1); |
| 64 | + } |
| 65 | + |
| 66 | + console.log(`OK: required env vars present: ${required.join(", ")}`); |
| 67 | +} |
| 68 | + |
| 69 | +main(); |
| 70 | + |
0 commit comments