@subramanyaks/env-drift 1.0.1 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md ADDED
@@ -0,0 +1,48 @@
1
+ # Changelog
2
+
3
+ All notable changes to `@subramanyaks/env-drift` are documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [1.2.0] - 2026-12-02
9
+
10
+ ### Added
11
+ - **Multi-environment drift diffing (`--diff`):** Compare keys across two env files (e.g. `env-drift --diff .env.staging .env.production`) to surface keys only in one file and empty values without ever printing secret values. Supports `--ci`, `--strict`, and `--json`.
12
+ - **Zod runtime validation schema generator (`--schema` / `--schema-out`):** Generates a starter `env.schema.ts` with inferred Zod validators (`z.string().url()` for `*_URL`, `z.coerce.number()` for `PORT`/`*_PORT`/`*_TIMEOUT`, `z.coerce.boolean()` for boolean flags, and `z.string().min(1)` by default).
13
+ - **Monorepo / workspace auditing (`--workspaces`):** Automatically discovers workspaces from root `package.json` (`workspaces`) or `pnpm-workspace.yaml` and audits each package with a unified summary and combined `--ci` exit code.
14
+ - **Strict CI mode (`--strict`):** Extends `--ci` (and `"strict": true` in `.envdriftrc.json`) to also fail on empty `.env` values, unused keys, and duplicate keys.
15
+ - **New public API exports:** Exported `diffEnvFiles`, `hasDiffIssues`, `formatDiffReport`, `generateZodSchema`, `inferZodRule`, `writeZodSchema`, `discoverWorkspaces`, `auditWorkspaces`, `formatWorkspaceReport`, `EnvDiffResult`, `WorkspaceAuditItem`, and `WorkspaceAuditResult`.
16
+
17
+ ### Fixed
18
+ - **Multiline quoted `.env` values:** `parseEnvContent()` now properly parses multiline double-quoted values such as PEM private keys while preserving accurate starting line numbers.
19
+ - **Regex literal safety in scanner:** Replaced regex-based comment stripping with a single-pass lexer in `stripCommentsPreservingLines()` so regex literals containing `//` (e.g. `/https?:\/\//`) are never misread as line comments.
20
+
21
+ ## [1.1.0] - 2026-11-26
22
+
23
+ ### Added
24
+ - **Real secret leak detection in `.env.example`:** Detects committed credentials (Stripe keys, GitHub tokens, AWS Access Key IDs, AI provider keys, Slack tokens, Google API keys, JWTs, PEM private keys, and connection URLs with real passwords) while ignoring empty values and common placeholders. Treated as a blocking issue in `--ci` mode.
25
+ - **Empty value detection in `.env`:** Flags keys referenced in code that exist in `.env` with blank values (`emptyInEnv`), respecting `.env.local` overrides.
26
+ - **Project configuration file support:** Automatically loads optional settings from `.envdriftrc.json` or the `"env-drift"` field in `package.json` (`envFile`, `exampleFile`, `ignore`, `ignoreDirs`, `publicPrefixes`, `sensitiveKeywords`). CLI flags override config values.
27
+ - **GitHub Actions inline PR annotations (`--format github`):** Emits `::error` and `::warning` workflow commands with file and line metadata so findings appear inline on PR diffs. Also added `--format <text|json|github>` (with `--json` kept as an alias).
28
+ - **Broader runtime support & inline ignore comments:** Added detection for `Bun.env` (dot, bracket, destructuring) and `Deno.env.get(...)`, plus `// env-drift-ignore` and `// env-drift-ignore-next-line` comments.
29
+ - **New public API exports:** Exported `loadConfig`, `detectExampleSecretLeak`, `formatGitHubAnnotations`, `EnvDriftConfig`, and `ExampleSecretLeak`.
30
+
31
+ ## [1.0.1] - 2026-11-26
32
+
33
+ ### Fixed
34
+ - **CI mode with gitignored `.env`:** `--ci` and `hasBlockingIssues()` no longer fail on `missingInEnv` when no local `.env` / `.env.local` file exists on disk, while still blocking on `missingInExample` and `secretExposures`. Added `envFileExists` and `exampleFileExists` to `AuditResult`.
35
+ - **Stale report after `--sync`:** Keys newly written to `.env.example` by `--sync` are now cleared from `result.missingInExample` before rendering the report/JSON or evaluating `--ci`.
36
+ - **Dynamic CLI version:** CLI `--version` now reads directly from `package.json` instead of a hardcoded string.
37
+ - **Cross-platform direct execution guard:** Replaced suffix-matching with `realpathSync` + `fileURLToPath(import.meta.url)` so CLI execution works reliably across Windows, macOS, and Linux shims.
38
+ - **Parser & scanner edge cases:** Properly unescapes `\"`, `\n`, `\r`, and `\t` inside double-quoted `.env` values, and detects optional-chained bracket access (`process.env?.["KEY"]`, `import.meta.env?.["KEY"]`).
39
+
40
+ ## [1.0.0] - 2026-11-25
41
+
42
+ ### Added
43
+ - Initial release of `@subramanyaks/env-drift` CLI and programmatic TypeScript/ESM/CJS library.
44
+ - Static codebase scanning for `process.env` and `import.meta.env` (dot, optional-chain, bracket, and destructuring access) while ignoring comments.
45
+ - Drift analysis detecting missing keys in `.env` and `.env.example`, unused/dead keys, duplicate keys, and client-exposed sensitive variable names (`NEXT_PUBLIC_*`, `VITE_*`, `REACT_APP_*`, etc.).
46
+ - Safe `--sync` to append missing keys as empty `KEY=` placeholders to `.env.example`.
47
+ - TypeScript `env.d.ts` declaration generator (`--types` / `--types-out`).
48
+ - CI exit code gating (`--ci`) and structured JSON output (`--json`).
package/README.md CHANGED
@@ -11,12 +11,15 @@ Zero-config CLI and TypeScript library that detects **environment variable drift
11
11
 
12
12
  Runtime validators are great, but they require adding code and only fail *after* your app boots. `env-drift` statically audits your repo in milliseconds with **zero setup**:
13
13
 
14
- - **Missing in `.env`** — Catch `process.env.STRIPE_KEY` or `import.meta.env.VITE_API_URL` before runtime crashes.
14
+ - **Missing in `.env`** — Catch `process.env.STRIPE_KEY`, `import.meta.env.VITE_API_URL`, `Bun.env.API_KEY`, or `Deno.env.get("TOKEN")` before runtime crashes.
15
15
  - **Missing in `.env.example`** — Keep teammate onboarding in sync (with `--sync` to safely append `KEY=` placeholders without ever copying secret values).
16
+ - **Empty Values in `.env`** — Catch keys that exist in `.env` as `STRIPE_KEY=` with no value set.
16
17
  - **Unused / Dead vars** — Spot stale keys left behind in `.env` and `.env.example`.
17
18
  - **Public Secret Exposure** — Flags dangerous client-exposed names like `NEXT_PUBLIC_DB_PASSWORD` or `VITE_JWT_SECRET`.
18
- - **Type Generation** — Auto-generate `env.d.ts` (`NodeJS.ProcessEnv` + `ImportMetaEnv`) with `--types`.
19
- - **CI Ready** — Drop `npx @subramanyaks/env-drift --ci` into GitHub Actions to fail PRs on drift or leaked public secrets.
19
+ - **Real Secrets in `.env.example`** — Detects real API keys, tokens, JWTs, PEM private keys, and DB passwords accidentally committed to `.env.example`.
20
+ - **Type & Zod Schema Generation** — Auto-generate `env.d.ts` (`--types`) and a starter Zod runtime validation schema `env.schema.ts` (`--schema`).
21
+ - **Multi-Env Diff & Monorepos** — Compare `.env.staging` vs `.env.production` with `--diff` (without printing secret values) and audit all monorepo packages with `--workspaces`.
22
+ - **CI & GitHub PR Annotations** — Drop `npx @subramanyaks/env-drift --ci --format github` into GitHub Actions (with optional `--strict`) to annotate PR diffs directly.
20
23
 
21
24
  ## Quick Start
22
25
 
@@ -59,8 +62,14 @@ Tip: Run with --sync to safely append missing keys to .env.example.
59
62
  | `--sync` | Safely append missing keys to `.env.example` as `KEY=` | `false` |
60
63
  | `--types` | Generate `env.d.ts` for detected env variables | `false` |
61
64
  | `--types-out <path>` | Custom output path for `--types` | `env.d.ts` |
62
- | `--ci` | Exit with code `1` if missing vars or public secret leaks are found | `false` |
63
- | `--json` | Output structured JSON (`AuditResult`) | `false` |
65
+ | `--schema` | Generate starter Zod runtime validation schema (`env.schema.ts`) | `false` |
66
+ | `--schema-out <path>` | Custom output path for `--schema` | `env.schema.ts` |
67
+ | `--diff <fileA> <fileB>` | Compare keys across two env files without printing secret values | `false` |
68
+ | `--workspaces` | Audit all monorepo workspaces (`package.json` / `pnpm-workspace.yaml`) | `false` |
69
+ | `--ci` | Exit with code `1` if missing vars or secret leaks are found (skips local `.env` check when `.env` is absent) | `false` |
70
+ | `--strict` | In `--ci` mode, also fail on empty values, unused keys, and duplicate keys | `false` |
71
+ | `--format <type>` | Output format: `text`, `json`, or `github` (GitHub Actions annotations) | `text` |
72
+ | `--json` | Output structured JSON (`AuditResult`) — alias for `--format json` | `false` |
64
73
  | `-v, --version` | Print version | — |
65
74
  | `-h, --help` | Print help message | — |
66
75
 
@@ -73,8 +82,17 @@ npx @subramanyaks/env-drift --sync
73
82
  # Generate env.d.ts for TypeScript autocomplete
74
83
  npx @subramanyaks/env-drift --types
75
84
 
76
- # Run in CI (fails build on missing vars or public secret leaks)
77
- npx @subramanyaks/env-drift --ci
85
+ # Generate a starter Zod runtime validation schema (env.schema.ts)
86
+ npx @subramanyaks/env-drift --schema
87
+
88
+ # Compare staging and production env files (keys only, never prints secrets)
89
+ npx @subramanyaks/env-drift --diff .env.staging .env.production
90
+
91
+ # Audit all packages in a monorepo
92
+ npx @subramanyaks/env-drift --workspaces --ci
93
+
94
+ # Run in CI (fails build on missing vars or secret leaks; add --strict to fail on warnings too)
95
+ npx @subramanyaks/env-drift --ci --strict
78
96
 
79
97
  # Ignore specific platform variables
80
98
  npx @subramanyaks/env-drift --ignore VERCEL_URL,RAILWAY_ENVIRONMENT
@@ -82,13 +100,31 @@ npx @subramanyaks/env-drift --ignore VERCEL_URL,RAILWAY_ENVIRONMENT
82
100
 
83
101
  ## GitHub Actions CI Recipe
84
102
 
85
- Add this step to your `.github/workflows/ci.yml` to block PRs with undocumented env vars or leaked client secrets:
103
+ Add this step to your `.github/workflows/ci.yml` to block PRs with undocumented env vars or leaked secrets and render inline PR annotations:
86
104
 
87
105
  ```yaml
88
106
  - name: Check environment drift
89
- run: npx @subramanyaks/env-drift --ci
107
+ run: npx @subramanyaks/env-drift --ci --format github
90
108
  ```
91
109
 
110
+ ## Configuration File (Optional)
111
+
112
+ You can persist options in `.envdriftrc.json` (or under an `"env-drift"` key in `package.json`):
113
+
114
+ ```json
115
+ {
116
+ "envFile": ".env",
117
+ "exampleFile": ".env.example",
118
+ "ignore": ["VERCEL_URL", "RAILWAY_ENVIRONMENT"],
119
+ "ignoreDirs": ["generated"],
120
+ "publicPrefixes": ["CUSTOM_PUBLIC_"],
121
+ "sensitiveKeywords": ["MASTER_KEY"],
122
+ "strict": false
123
+ }
124
+ ```
125
+
126
+ CLI flags always override config file values.
127
+
92
128
  ## Programmatic API
93
129
 
94
130
  Install as a dev dependency to use in custom scripts:
@@ -113,10 +149,11 @@ if (hasBlockingIssues(result)) {
113
149
 
114
150
  ## What Patterns Are Detected?
115
151
 
116
- - `process.env.API_KEY` and `process.env?.API_KEY`
117
- - `process.env["API_KEY"]` and `process.env['API_KEY']`
152
+ - `process.env.API_KEY`, `process.env?.API_KEY`, `process.env["API_KEY"]`, and `process.env?.["API_KEY"]`
118
153
  - `const { API_KEY, PORT: port = "3000" } = process.env`
119
154
  - `import.meta.env.VITE_API_URL` and destructuring from `import.meta.env`
155
+ - `Bun.env.API_KEY`, `Bun.env["API_KEY"]`, and `Deno.env.get("API_KEY")`
156
+ - Inline ignore comments: `// env-drift-ignore` and `// env-drift-ignore-next-line`
120
157
  - Commented-out code (`//` and `/* ... */`) is automatically ignored.
121
158
  - Standard built-ins (`NODE_ENV`, `CI`, `MODE`, `DEV`, `PROD`, `SSR`, `BASE_URL`) are ignored by default.
122
159