-
Notifications
You must be signed in to change notification settings - Fork 23
chore: add env consistency check script #11
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
Open
JonesRoy3
wants to merge
1
commit into
Polymarket:main
Choose a base branch
from
JonesRoy3:examples-env-check-tool
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| ```ts | ||
| import fs from "node:fs"; | ||
| import path from "node:path"; | ||
|
|
||
| async function loadKeys(filePath: string): Promise<Set<string>> { | ||
| const raw = await fs.promises.readFile(filePath, "utf8"); | ||
| const keys = new Set<string>(); | ||
|
|
||
| for (const line of raw.split(/\r?\n/)) { | ||
| const trimmed = line.trim(); | ||
|
|
||
| // Skip empty lines and comments | ||
| if (!trimmed || trimmed.startsWith("#")) { | ||
| continue; | ||
| } | ||
|
|
||
| const equalsIndex = trimmed.indexOf("="); | ||
| if (equalsIndex <= 0) { | ||
| continue; | ||
| } | ||
|
|
||
| const key = trimmed.slice(0, equalsIndex).trim(); | ||
| if (key.length > 0) { | ||
| keys.add(key); | ||
| } | ||
| } | ||
|
|
||
| return keys; | ||
| } | ||
|
|
||
| async function main() { | ||
| const projectRoot = process.cwd(); | ||
| const examplePath = path.join(projectRoot, ".env.example"); | ||
| const envPath = path.join(projectRoot, ".env"); | ||
|
|
||
| if (!fs.existsSync(examplePath)) { | ||
| console.error("Could not find .env.example at the project root."); | ||
| console.error("Make sure you are running this script from the repository root."); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| const expectedKeys = await loadKeys(examplePath); | ||
|
|
||
| if (expectedKeys.size === 0) { | ||
| console.log("No keys found in .env.example (nothing to check)."); | ||
| return; | ||
| } | ||
|
|
||
| if (!fs.existsSync(envPath)) { | ||
| console.log("No .env file found at the project root."); | ||
| console.log("Create one by copying .env.example and filling in the values."); | ||
| console.log(""); | ||
| console.log("Example:"); | ||
| console.log(" cp .env.example .env"); | ||
| return; | ||
| } | ||
|
|
||
| const actualKeys = await loadKeys(envPath); | ||
|
|
||
| const missing: string[] = []; | ||
| const extra: string[] = []; | ||
|
|
||
| for (const key of expectedKeys) { | ||
| if (!actualKeys.has(key)) { | ||
| missing.push(key); | ||
| } | ||
| } | ||
|
|
||
| for (const key of actualKeys) { | ||
| if (!expectedKeys.has(key)) { | ||
| extra.push(key); | ||
| } | ||
| } | ||
|
|
||
| console.log("=== .env consistency check ==="); | ||
| console.log(`Expected keys (from .env.example): ${expectedKeys.size}`); | ||
| console.log(`Actual keys (from .env) : ${actualKeys.size}`); | ||
| console.log(""); | ||
|
|
||
| if (missing.length === 0 && extra.length === 0) { | ||
| console.log("✅ All keys from .env.example are present in .env, and there are no extra keys."); | ||
| return; | ||
| } | ||
|
|
||
| if (missing.length > 0) { | ||
| console.log("⚠ Missing keys in .env (present in .env.example but not in .env):"); | ||
| for (const key of missing) { | ||
| console.log(` - ${key}`); | ||
| } | ||
| console.log(""); | ||
| } | ||
|
|
||
| if (extra.length > 0) { | ||
| console.log("ℹ Extra keys in .env (not listed in .env.example):"); | ||
| for (const key of extra) { | ||
| console.log(` - ${key}`); | ||
| } | ||
| console.log(""); | ||
| } | ||
|
|
||
| console.log("If you just created your .env from .env.example,"); | ||
| console.log("check that all required keys are present and spelled correctly."); | ||
| } | ||
|
|
||
| main().catch((error) => { | ||
| console.error("Unexpected error while checking .env:", error); | ||
| process.exit(1); | ||
| }); | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: Markdown code fence accidentally included in TypeScript file
The file contains
```tson line 1, which is markdown syntax for a code fence. This appears to have been accidentally included when copying code from a markdown document. The TypeScript file will fail to parse because```tsis not valid TypeScript syntax.