@jutaru-dev/envsonar 0.1.0 → 0.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/README.md +188 -176
- package/bin/envsonar.js +2 -0
- package/package.json +57 -46
- package/src/check.js +157 -146
- package/src/cli.js +303 -298
- package/src/parse.js +99 -98
- package/src/scan.js +272 -33
- package/types/check.d.ts +66 -0
- package/types/cli.d.ts +34 -0
- package/types/index.d.ts +4 -0
- package/types/parse.d.ts +26 -0
- package/types/profiles.d.ts +15 -0
- package/types/scan.d.ts +156 -0
package/README.md
CHANGED
|
@@ -1,176 +1,188 @@
|
|
|
1
|
-
# envsonar
|
|
2
|
-
|
|
3
|
-
[](https://github.com/jutaru-dev/envsonar/actions/workflows/ci.yml)
|
|
4
|
-
[](https://www.npmjs.com/package/@jutaru-dev/envsonar)
|
|
5
|
-

|
|
6
|
-

|
|
7
|
-

|
|
8
|
-
|
|
9
|
-
**Catches the env vars your code reads but `.env.example` forgot — and knows which ones your framework reads for you.**
|
|
10
|
-
|
|
11
|
-
<p align="center">
|
|
12
|
-
<img src="https://raw.githubusercontent.com/jutaru-dev/envsonar/main/assets/demo.svg" alt="envsonar on a monorepo: two keys missing from .env.example, one documented key nobody reads" width="100%">
|
|
13
|
-
</p>
|
|
14
|
-
|
|
15
|
-
Checking `.env.example` against the code isn't a new idea, and the simple version is easy: grep for `process.env.X` and `os.getenv("X")`. The trouble is that real projects mostly don't read config that way. ASP.NET Core turns `config["Jwt:Key"]` into `Jwt__Key`. pydantic-settings derives names from class fields. A zod schema parses `process.env` in one place. The postgres image reads `POSTGRES_PASSWORD` and the OpenAI SDK reads `OPENAI_API_KEY` without a single line of your code naming them.
|
|
16
|
-
|
|
17
|
-
A grep-only check misses the first group and calls the second one "unused". After a week of false alarms people switch it off. envsonar is built around that problem: it understands how each stack reads configuration, so what it reports is worth fixing.
|
|
18
|
-
|
|
19
|
-
## What that means on a real project
|
|
20
|
-
|
|
21
|
-
The demo above is [`examples/demo`](https://github.com/jutaru-dev/envsonar/tree/main/examples/demo): a Next.js app, an ASP.NET Core API, a Python worker and a compose file sharing one `.env.example`.
|
|
22
|
-
|
|
23
|
-
| | a scan that only finds direct reads | envsonar |
|
|
24
|
-
| --- | --- | --- |
|
|
25
|
-
| missing from `.env.example` | `STRIPE_SECRET_KEY`, `REDIS_URL` | `STRIPE_SECRET_KEY`, `WORKER_QUEUE_URL` |
|
|
26
|
-
| documented but "unused" | 8 keys, 7 of them wrong | `LEGACY_FLAG` |
|
|
27
|
-
| why | can't see the zod schema, .NET config, pydantic fields, the SDKs or the postgres image | reads all of them; `REDIS_URL` has a fallback in code, so it's a note, not an error |
|
|
28
|
-
|
|
29
|
-
Before release it was also run on two private production projects, an ASP.NET Core + Next.js + compose app and a Python bot: zero false alarms between code and `.env.example`, and two real undocumented keys found.
|
|
30
|
-
|
|
31
|
-
## Quick start
|
|
32
|
-
|
|
33
|
-
```sh
|
|
34
|
-
npx @jutaru-dev/envsonar # check the current project
|
|
35
|
-
npx @jutaru-dev/envsonar --fix # add what's missing to .env.example (creates it if needed)
|
|
36
|
-
npx @jutaru-dev/envsonar --list # every variable the code reads, and where
|
|
37
|
-
```
|
|
38
|
-
|
|
39
|
-
Node.js 20 or newer. No install step and no config needed. `--fix` writes each key with an empty value and a comment saying where it's read:
|
|
40
|
-
|
|
41
|
-
```sh
|
|
42
|
-
# added by envsonar
|
|
43
|
-
# read in apps/web/src/app/api/checkout/route.ts:3
|
|
44
|
-
STRIPE_SECRET_KEY=
|
|
45
|
-
```
|
|
46
|
-
|
|
47
|
-
## What it checks
|
|
48
|
-
|
|
49
|
-
| | | |
|
|
50
|
-
| --- | --- | --- |
|
|
51
|
-
| ✗ | `undocumented` | read in code, not in `.env.example` |
|
|
52
|
-
| ✗ | `missing` | in `.env.example`, not set in `.env` and the code needs it (only when `.env` exists) |
|
|
53
|
-
| ! | `unused` | in `.env.example`, but nothing reads it: not your code, not a framework, SDK or image |
|
|
54
|
-
| ! | `defaulted` | read with a fallback (`?? "x"`, `getenv("X", 1)`, `${X:-1}`), not in `.env.example`; hidden unless `--optional` |
|
|
55
|
-
| ! | `empty` · `extra` · `duplicate` | in `.env`: empty value, key not documented, key defined twice |
|
|
56
|
-
| ✗ | `syntax` | a line in either file that isn't `KEY=VALUE` |
|
|
57
|
-
|
|
58
|
-
Errors fail the run; `--strict` makes warnings fail too. Reports contain key names, files and line numbers only. **Values are never printed**, so the output is safe in public CI logs.
|
|
59
|
-
|
|
60
|
-
## What it understands
|
|
61
|
-
|
|
62
|
-
| Stack | Reads it picks up |
|
|
63
|
-
| --- | --- |
|
|
64
|
-
| **JS / TS** | `process.env.X`, `process.env["X"]`, `import.meta.env.X`, `Bun.env`, `Deno.env.get`, `const { X, Y = 1 } = process.env`, `const env = process.env; env.X` |
|
|
65
|
-
| **env schemas** | zod objects passed to `parse(process.env)`, `@t3-oss/env` `createEnv`, envalid `cleanEnv
|
|
66
|
-
| **SvelteKit · Vite** | `import { X } from "$env/static/private"`, `$env/dynamic`, `%VITE_X%` in `index.html` |
|
|
67
|
-
| **ASP.NET Core** | `config["Jwt:Key"]` → `Jwt__Key`, `GetValue<T>()`, `GetConnectionString("Db")` → `ConnectionStrings__Db`, `GetSection("
|
|
68
|
-
| **Python** | `os.getenv`, `os.environ[...]`, `.get` / `.setdefault`, aliases such as `env = dict(os.environ)`; **pydantic-settings** fields with `env_prefix` and `alias` |
|
|
69
|
-
| **Go** | `os.Getenv`, `os.LookupEnv`, struct tags `env:"X"` (caarlos0/env) and `envconfig:"X"` |
|
|
70
|
-
| **PHP / Laravel** | `env("X", default)`, `getenv`, `$_ENV`; framework keys Laravel reads from its own config |
|
|
71
|
-
| **Ruby · Rust** | `ENV["X"]`, `ENV.fetch`; `env::var`, `dotenvy::var`, `env!`, `option_env!` |
|
|
72
|
-
| **Prisma** | `env("DATABASE_URL")` in `schema.prisma` and `prisma.config.ts` |
|
|
73
|
-
| **Docker Compose** | `${X}`, `${X:-default}`, `${X:?error}`, nested `${A:-${B}}`, `$X`; `environment:` entries supply the app with values |
|
|
74
|
-
|
|
75
|
-
### Variables nobody's code names
|
|
76
|
-
|
|
77
|
-
Some variables are read by a dependency or a container, not by you. envsonar finds the dependency in `package.json`, `pyproject.toml` / `requirements*.txt`, `composer.json`, `*.csproj` or the compose `image:`, and stops calling its variables unused. The run header says what it found: `also read by: Next.js, OpenAI SDK, postgres image`.
|
|
78
|
-
|
|
79
|
-
Covered now: Next.js, Nuxt, Auth.js, Prisma, OpenAI, Anthropic, Google AI, AWS, Google Cloud / Firebase, Sentry, libpq (`PG*`), Vercel Postgres / KV / Blob, Upstash, dotenvx, Django, Laravel, ASP.NET Core, Docker Compose, and the postgres, mysql, mariadb, mongo, redis, rabbitmq, minio, pgadmin, keycloak, clickhouse, elasticsearch, mssql, grafana, mailpit and n8n images. The whole list is one readable file, [`src/profiles.js`](https://github.com/jutaru-dev/envsonar/blob/main/src/profiles.js); adding a stack is a three-line PR.
|
|
80
|
-
|
|
81
|
-
### What it doesn't guess
|
|
82
|
-
|
|
83
|
-
- **Dynamic reads** such as `process.env[name]` or `os.getenv(key)` can't be resolved statically. envsonar lists them with their locations (`1 dynamic read can't be checked: src/config.ts:12`) instead of silently skipping them.
|
|
84
|
-
-
|
|
85
|
-
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
|
120
|
-
|
|
|
121
|
-
|
|
|
122
|
-
| `--
|
|
123
|
-
| `--
|
|
124
|
-
| `--
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
##
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
1
|
+
# envsonar
|
|
2
|
+
|
|
3
|
+
[](https://github.com/jutaru-dev/envsonar/actions/workflows/ci.yml)
|
|
4
|
+
[](https://www.npmjs.com/package/@jutaru-dev/envsonar)
|
|
5
|
+

|
|
6
|
+

|
|
7
|
+

|
|
8
|
+
|
|
9
|
+
**Catches the env vars your code reads but `.env.example` forgot — and knows which ones your framework reads for you.**
|
|
10
|
+
|
|
11
|
+
<p align="center">
|
|
12
|
+
<img src="https://raw.githubusercontent.com/jutaru-dev/envsonar/main/assets/demo.svg" alt="envsonar on a monorepo: two keys missing from .env.example, one documented key nobody reads" width="100%">
|
|
13
|
+
</p>
|
|
14
|
+
|
|
15
|
+
Checking `.env.example` against the code isn't a new idea, and the simple version is easy: grep for `process.env.X` and `os.getenv("X")`. The trouble is that real projects mostly don't read config that way. ASP.NET Core turns `config["Jwt:Key"]` into `Jwt__Key`. pydantic-settings derives names from class fields. A zod schema parses `process.env` in one place. The postgres image reads `POSTGRES_PASSWORD` and the OpenAI SDK reads `OPENAI_API_KEY` without a single line of your code naming them.
|
|
16
|
+
|
|
17
|
+
A grep-only check misses the first group and calls the second one "unused". After a week of false alarms people switch it off. envsonar is built around that problem: it understands how each stack reads configuration, so what it reports is worth fixing.
|
|
18
|
+
|
|
19
|
+
## What that means on a real project
|
|
20
|
+
|
|
21
|
+
The demo above is [`examples/demo`](https://github.com/jutaru-dev/envsonar/tree/main/examples/demo): a Next.js app, an ASP.NET Core API, a Python worker and a compose file sharing one `.env.example`.
|
|
22
|
+
|
|
23
|
+
| | a scan that only finds direct reads | envsonar |
|
|
24
|
+
| --- | --- | --- |
|
|
25
|
+
| missing from `.env.example` | `STRIPE_SECRET_KEY`, `REDIS_URL` | `STRIPE_SECRET_KEY`, `WORKER_QUEUE_URL` |
|
|
26
|
+
| documented but "unused" | 8 keys, 7 of them wrong | `LEGACY_FLAG` |
|
|
27
|
+
| why | can't see the zod schema, .NET config, pydantic fields, the SDKs or the postgres image | reads all of them; `REDIS_URL` has a fallback in code, so it's a note, not an error |
|
|
28
|
+
|
|
29
|
+
Before release it was also run on two private production projects, an ASP.NET Core + Next.js + compose app and a Python bot: zero false alarms between code and `.env.example`, and two real undocumented keys found.
|
|
30
|
+
|
|
31
|
+
## Quick start
|
|
32
|
+
|
|
33
|
+
```sh
|
|
34
|
+
npx @jutaru-dev/envsonar # check the current project
|
|
35
|
+
npx @jutaru-dev/envsonar --fix # add what's missing to .env.example (creates it if needed)
|
|
36
|
+
npx @jutaru-dev/envsonar --list # every variable the code reads, and where
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Node.js 20 or newer. No install step and no config needed. `--fix` writes each key with an empty value and a comment saying where it's read:
|
|
40
|
+
|
|
41
|
+
```sh
|
|
42
|
+
# added by envsonar
|
|
43
|
+
# read in apps/web/src/app/api/checkout/route.ts:3
|
|
44
|
+
STRIPE_SECRET_KEY=
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## What it checks
|
|
48
|
+
|
|
49
|
+
| | | |
|
|
50
|
+
| --- | --- | --- |
|
|
51
|
+
| ✗ | `undocumented` | read in code, not in `.env.example` |
|
|
52
|
+
| ✗ | `missing` | in `.env.example`, not set in `.env` and the code needs it (only when `.env` exists) |
|
|
53
|
+
| ! | `unused` | in `.env.example`, but nothing reads it: not your code, not a framework, SDK or image |
|
|
54
|
+
| ! | `defaulted` | read with a fallback (`?? "x"`, `getenv("X", 1)`, `${X:-1}`), not in `.env.example`; hidden unless `--optional` |
|
|
55
|
+
| ! | `empty` · `extra` · `duplicate` | in `.env`: empty value, key not documented, key defined twice |
|
|
56
|
+
| ✗ | `syntax` | a line in either file that isn't `KEY=VALUE` |
|
|
57
|
+
|
|
58
|
+
Errors fail the run; `--strict` makes warnings fail too. Reports contain key names, files and line numbers only. **Values are never printed**, so the output is safe in public CI logs.
|
|
59
|
+
|
|
60
|
+
## What it understands
|
|
61
|
+
|
|
62
|
+
| Stack | Reads it picks up |
|
|
63
|
+
| --- | --- |
|
|
64
|
+
| **JS / TS** | `process.env.X`, `process.env["X"]`, `import.meta.env.X`, `Bun.env`, `Deno.env.get`, `const { X, Y = 1 } = process.env`, `const env = process.env; env.X` |
|
|
65
|
+
| **env schemas** | zod objects passed to `parse(process.env)`, `@t3-oss/env` `createEnv`, envalid `cleanEnv`, also when a formatter wraps them over several lines; `.default()` / `.optional()` count as fallbacks, `runtimeEnv` doesn't override them |
|
|
66
|
+
| **SvelteKit · Vite** | `import { X } from "$env/static/private"`, `$env/dynamic`, `%VITE_X%` in `index.html` |
|
|
67
|
+
| **ASP.NET Core** | `config["Jwt:Key"]` → `Jwt__Key`, `GetValue<T>()`, `GetConnectionString("Db")` → `ConnectionStrings__Db`; options classes bound with `Configure<JwtOptions>(GetSection("Jwt"))`, `GetSection("Jwt").Get<T>()` or `BindConfiguration("Jwt")` → one key per property, required unless it has a real default; keys with a value in `appsettings.json` don't need a `.env` entry (`appsettings.Development.json` doesn't count, production won't have it); names match case-insensitively, as .NET does |
|
|
68
|
+
| **Python** | `os.getenv`, `os.environ[...]`, `.get` / `.setdefault`, aliases such as `env = dict(os.environ)`; **pydantic-settings** fields with `env_prefix` and `alias` |
|
|
69
|
+
| **Go** | `os.Getenv`, `os.LookupEnv`, struct tags `env:"X"` (caarlos0/env) and `envconfig:"X"` |
|
|
70
|
+
| **PHP / Laravel** | `env("X", default)`, `getenv`, `$_ENV`; framework keys Laravel reads from its own config |
|
|
71
|
+
| **Ruby · Rust** | `ENV["X"]`, `ENV.fetch`; `env::var`, `dotenvy::var`, `env!`, `option_env!` |
|
|
72
|
+
| **Prisma** | `env("DATABASE_URL")` in `schema.prisma` and `prisma.config.ts` |
|
|
73
|
+
| **Docker Compose** | `${X}`, `${X:-default}`, `${X:?error}`, nested `${A:-${B}}`, `$X`; `environment:` entries supply the app with values |
|
|
74
|
+
|
|
75
|
+
### Variables nobody's code names
|
|
76
|
+
|
|
77
|
+
Some variables are read by a dependency or a container, not by you. envsonar finds the dependency in `package.json`, `pyproject.toml` / `requirements*.txt`, `composer.json`, `*.csproj` or the compose `image:`, and stops calling its variables unused. The run header says what it found: `also read by: Next.js, OpenAI SDK, postgres image`.
|
|
78
|
+
|
|
79
|
+
Covered now: Next.js, Nuxt, Auth.js, Prisma, OpenAI, Anthropic, Google AI, AWS, Google Cloud / Firebase, Sentry, libpq (`PG*`), Vercel Postgres / KV / Blob, Upstash, dotenvx, Django, Laravel, ASP.NET Core, Docker Compose, and the postgres, mysql, mariadb, mongo, redis, rabbitmq, minio, pgadmin, keycloak, clickhouse, elasticsearch, mssql, grafana, mailpit and n8n images. The whole list is one readable file, [`src/profiles.js`](https://github.com/jutaru-dev/envsonar/blob/main/src/profiles.js); adding a stack is a three-line PR.
|
|
80
|
+
|
|
81
|
+
### What it doesn't guess
|
|
82
|
+
|
|
83
|
+
- **Dynamic reads** such as `process.env[name]` or `os.getenv(key)` can't be resolved statically. envsonar lists them with their locations (`1 dynamic read can't be checked: src/config.ts:12`) instead of silently skipping them.
|
|
84
|
+
- Comments and docstrings are skipped, and so are writes such as `process.env.X = "1"` in a setup file. Tests (`test/`, `__tests__/`, `*.test.ts`, `test_*.py`, `*_test.go`, `*.Tests` projects) aren't scanned: they set and mock variables the app itself doesn't need. `--include-tests` scans them too.
|
|
85
|
+
- It reads source with patterns, not a full parser per language. A read hidden behind your own wrapper (`getConfig("X")`) isn't seen. Add the key to `.env.example`, or skip it with `--ignore-key`.
|
|
86
|
+
- Kubernetes manifests, Terraform and CI workflow files aren't scanned yet.
|
|
87
|
+
|
|
88
|
+
## In CI
|
|
89
|
+
|
|
90
|
+
As a GitHub Action, no Node setup or install step needed:
|
|
91
|
+
|
|
92
|
+
```yaml
|
|
93
|
+
# .github/workflows/env.yml
|
|
94
|
+
name: env
|
|
95
|
+
on: [pull_request]
|
|
96
|
+
jobs:
|
|
97
|
+
envsonar:
|
|
98
|
+
runs-on: ubuntu-latest
|
|
99
|
+
steps:
|
|
100
|
+
- uses: actions/checkout@v7
|
|
101
|
+
- uses: jutaru-dev/envsonar@v0
|
|
102
|
+
# with:
|
|
103
|
+
# path: apps/api # project directory, default "."
|
|
104
|
+
# args: --strict # any CLI flags
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
The action runs the check once and exposes `errors` and `warnings` counts as step outputs. Any other CI, or a pinned npm version:
|
|
108
|
+
|
|
109
|
+
```yaml
|
|
110
|
+
- run: npx @jutaru-dev/envsonar@0.2.0
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
On GitHub Actions the findings also become annotations on the lines that caused them: the `STRIPE_SECRET_KEY` error shows up right on `route.ts:3` in the pull request diff. Elsewhere use `--format github` to force it, or `--json` for your own tooling.
|
|
114
|
+
|
|
115
|
+
`.env` is usually not in CI, so only the code ↔ `.env.example` checks run there. To check a committed env file too, pass it: `--env .env.ci`.
|
|
116
|
+
|
|
117
|
+
## Options
|
|
118
|
+
|
|
119
|
+
| | |
|
|
120
|
+
| --- | --- |
|
|
121
|
+
| `envsonar [dir]` | project to check (default: current directory); for a monorepo, run it per app: `envsonar apps/api` |
|
|
122
|
+
| `--fix` | add undocumented keys to `.env.example`, creating the file if needed |
|
|
123
|
+
| `--optional` | also report (and `--fix`) keys that have a fallback in code |
|
|
124
|
+
| `--list` | print every variable the code reads, bound config sections and dynamic reads |
|
|
125
|
+
| `--example <path>` | default `.env.example` |
|
|
126
|
+
| `--env <path>` | default `.env`; checked only if it exists, unless you pass this flag |
|
|
127
|
+
| `--no-code` | don't scan source, only compare `.env` with `.env.example` |
|
|
128
|
+
| `--ignore <dir>` | skip a directory (repeatable). `node_modules`, build output (`bin`/`obj` next to a `.csproj`), virtualenvs, worktrees and plain dirs from `.gitignore` are skipped already |
|
|
129
|
+
| `--include-tests` | also scan test files and directories |
|
|
130
|
+
| `--ignore-key <KEY>` | skip a key; `PREFIX_*` works (repeatable). OS and CI variables (`PATH`, `CI`, `GITHUB_*`, …) are skipped already |
|
|
131
|
+
| `--strict` | warnings fail the run too |
|
|
132
|
+
| `--json` | machine-readable report |
|
|
133
|
+
| `--format <pretty\|github>` | `github` prints workflow annotations (automatic on GitHub Actions) |
|
|
134
|
+
| `--no-color` | also respects `NO_COLOR` |
|
|
135
|
+
|
|
136
|
+
The same settings can live in `.envsonarrc.json` or under `"envsonar"` in `package.json`:
|
|
137
|
+
|
|
138
|
+
```json
|
|
139
|
+
{
|
|
140
|
+
"envsonar": {
|
|
141
|
+
"example": "config/.env.example",
|
|
142
|
+
"ignore": ["legacy"],
|
|
143
|
+
"ignoreKeys": ["INTERNAL_*"],
|
|
144
|
+
"includeTests": false,
|
|
145
|
+
"optional": true
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
**Exit codes:** `0` clean · `1` errors found · `2` bad usage, unreadable file, or no `.env.example` (run `--fix` to create one).
|
|
151
|
+
|
|
152
|
+
## From code
|
|
153
|
+
|
|
154
|
+
```js
|
|
155
|
+
import { scanProject, parseEnv, check } from "@jutaru-dev/envsonar";
|
|
156
|
+
import { readFileSync } from "node:fs";
|
|
157
|
+
|
|
158
|
+
const scan = scanProject(".");
|
|
159
|
+
const findings = check({
|
|
160
|
+
example: parseEnv(readFileSync(".env.example", "utf8")),
|
|
161
|
+
exampleFile: ".env.example",
|
|
162
|
+
scan,
|
|
163
|
+
});
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
`findings` is an array of `{ severity, code, key, file, line, message }`. `scan.usages` maps every key to the places it's read. Type declarations ship with the package.
|
|
167
|
+
|
|
168
|
+
## Alternatives
|
|
169
|
+
|
|
170
|
+
There are plenty of tools around `.env` files; pick the one that matches the job.
|
|
171
|
+
|
|
172
|
+
- [dotenv-linter](https://github.com/dotenv-linter/dotenv-linter): style and syntax of `.env` files themselves.
|
|
173
|
+
- [envalid](https://github.com/af/envalid), [@t3-oss/env](https://github.com/t3-oss/t3-env): validate variables at runtime. envsonar reads their schemas, so the two work together.
|
|
174
|
+
- [dotenvx](https://github.com/dotenvx/dotenvx): encrypted `.env` files.
|
|
175
|
+
- [envsleuth](https://pypi.org/project/envsleuth), [dotenv-scan](https://dev.to/flyingsquirrel0419/i-got-tired-of-deploying-broken-configs-so-i-built-dotenv-scan-47lb), [env-check](https://github.com/SemTiOne/env-check): also compare code with `.env.example`. envsleuth has SARIF output.
|
|
176
|
+
|
|
177
|
+
envsonar's focus is a mixed-stack repository and few false alarms: framework-specific config reads, implicit consumers, and defaults told apart from required keys.
|
|
178
|
+
|
|
179
|
+
## Development
|
|
180
|
+
|
|
181
|
+
```sh
|
|
182
|
+
node --test # the whole suite, no install needed
|
|
183
|
+
npm install && npm run typecheck # types; npm pack / publish build types/ first
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
## License
|
|
187
|
+
|
|
188
|
+
MIT © Jutaru
|
package/bin/envsonar.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
// @ts-check
|
|
3
|
+
import { appendFileSync } from "node:fs";
|
|
3
4
|
import { run } from "../src/cli.js";
|
|
4
5
|
|
|
5
6
|
process.exitCode = run(process.argv.slice(2), {
|
|
@@ -8,4 +9,5 @@ process.exitCode = run(process.argv.slice(2), {
|
|
|
8
9
|
cwd: process.cwd(),
|
|
9
10
|
color: Boolean(process.stdout.isTTY) && !("NO_COLOR" in process.env),
|
|
10
11
|
githubActions: process.env.GITHUB_ACTIONS === "true",
|
|
12
|
+
githubOutput: process.env.GITHUB_OUTPUT ? (s) => appendFileSync(String(process.env.GITHUB_OUTPUT), s + "\n") : undefined,
|
|
11
13
|
});
|
package/package.json
CHANGED
|
@@ -1,46 +1,57 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@jutaru-dev/envsonar",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Catches env vars your code reads but .env.example forgot — and knows which ones .NET config, pydantic, Prisma, compose and SDKs read for you. Zero dependencies, never prints a value.",
|
|
5
|
-
"type": "module",
|
|
6
|
-
"bin": {
|
|
7
|
-
"envsonar": "bin/envsonar.js"
|
|
8
|
-
},
|
|
9
|
-
"exports": {
|
|
10
|
-
".":
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
"
|
|
17
|
-
"
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
"
|
|
26
|
-
"
|
|
27
|
-
"
|
|
28
|
-
"
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
"
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
"
|
|
35
|
-
"
|
|
36
|
-
"
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
"
|
|
42
|
-
"
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
"
|
|
46
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "@jutaru-dev/envsonar",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Catches env vars your code reads but .env.example forgot — and knows which ones .NET config, pydantic, Prisma, compose and SDKs read for you. Zero dependencies, never prints a value.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"bin": {
|
|
7
|
+
"envsonar": "bin/envsonar.js"
|
|
8
|
+
},
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./types/index.d.ts",
|
|
12
|
+
"default": "./src/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"types": "./types/index.d.ts",
|
|
16
|
+
"files": [
|
|
17
|
+
"bin",
|
|
18
|
+
"src",
|
|
19
|
+
"types"
|
|
20
|
+
],
|
|
21
|
+
"engines": {
|
|
22
|
+
"node": ">=20"
|
|
23
|
+
},
|
|
24
|
+
"scripts": {
|
|
25
|
+
"test": "node --test",
|
|
26
|
+
"typecheck": "tsc -p jsconfig.json",
|
|
27
|
+
"build:types": "tsc -p tsconfig.types.json",
|
|
28
|
+
"prepack": "npm run build:types"
|
|
29
|
+
},
|
|
30
|
+
"keywords": [
|
|
31
|
+
"dotenv",
|
|
32
|
+
"env",
|
|
33
|
+
"env-example",
|
|
34
|
+
"environment-variables",
|
|
35
|
+
"lint",
|
|
36
|
+
"ci",
|
|
37
|
+
"github-actions",
|
|
38
|
+
"monorepo",
|
|
39
|
+
"aspnetcore",
|
|
40
|
+
"pydantic-settings",
|
|
41
|
+
"prisma",
|
|
42
|
+
"docker-compose",
|
|
43
|
+
"laravel"
|
|
44
|
+
],
|
|
45
|
+
"author": "Jutaru (https://github.com/jutaru-dev)",
|
|
46
|
+
"license": "MIT",
|
|
47
|
+
"repository": {
|
|
48
|
+
"type": "git",
|
|
49
|
+
"url": "git+https://github.com/jutaru-dev/envsonar.git"
|
|
50
|
+
},
|
|
51
|
+
"bugs": "https://github.com/jutaru-dev/envsonar/issues",
|
|
52
|
+
"homepage": "https://github.com/jutaru-dev/envsonar#readme",
|
|
53
|
+
"devDependencies": {
|
|
54
|
+
"@types/node": "^22.0.0",
|
|
55
|
+
"typescript": "5.9.3"
|
|
56
|
+
}
|
|
57
|
+
}
|