Skip to content

Commit 71a243a

Browse files
Merge pull request #53 from aavegotchi/codex/devex-env-example-validator
DevEx: Env hygiene (.env.example + env validator)
2 parents d5644ba + 6d320bd commit 71a243a

File tree

3 files changed

+114
-1
lines changed

3 files changed

+114
-1
lines changed

.env.example

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Copy to `.env` (this file is intentionally committed; `.env` is not).
2+
#
3+
# Never commit secrets. This file should contain variable NAMES only (values left blank).
4+
5+
# RPCs (used for local fork tests / scripts)
6+
BASE_RPC_URL=
7+
MATIC_URL=
8+
BASE_SEPOLIA_RPC_URL=
9+
AMOY_URL=
10+
GEIST_URL=
11+
MAINNET_URL=
12+
POLTER_TESTNET_URL=
13+
14+
# Private keys / signers (never commit)
15+
SECRET=
16+
ITEM_MANAGER=
17+
DIAMOND_UPGRADER=
18+
GEIST_SIGNER_PK=
19+
20+
# Block explorer / verification
21+
ETHERSCAN=
22+
POLYGON_API_KEY=
23+
24+
# Providers / APIs
25+
ALCHEMY_API_KEY=
26+
MORALIS_URL=
27+
28+
# OpenZeppelin Defender
29+
DEFENDER_APIKEY=
30+
DEFENDER_SECRET=
31+
32+
# Subgraphs
33+
SUBGRAPH_CORE_BASE=
34+
SUBGRAPH_CORE_MATIC=
35+
36+
# Tenderly (optional)
37+
TENDERLY_USER=
38+
TENDERLY_ACCESS_KEY=
39+
TENDERLY_FORK=
40+
TENDERLY_NETWORK_ID=
41+

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ node_modules
44
cache
55
bin
66
.env
7+
.env.*
8+
!.env.example
79
artifacts
810
coverage.json
911
coverage
@@ -14,4 +16,4 @@ typechain/
1416
flat
1517
botDaddies.ts
1618
botDaddies2.ts
17-
out
19+
out

scripts/check-env.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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

Comments
 (0)