@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/src/cli.js
CHANGED
|
@@ -1,298 +1,303 @@
|
|
|
1
|
-
// @ts-check
|
|
2
|
-
import { appendFileSync, existsSync, readFileSync } from "node:fs";
|
|
3
|
-
import { relative, resolve, sep } from "node:path";
|
|
4
|
-
import { parseArgs } from "node:util";
|
|
5
|
-
import { check, fixText } from "./check.js";
|
|
6
|
-
import { parseEnv } from "./parse.js";
|
|
7
|
-
import { scanProject } from "./scan.js";
|
|
8
|
-
|
|
9
|
-
export const VERSION = "0.
|
|
10
|
-
|
|
11
|
-
const HELP = `envsonar ${VERSION} — find env vars your code reads but .env.example forgot. Never prints a value.
|
|
12
|
-
|
|
13
|
-
Usage
|
|
14
|
-
envsonar [dir] [options]
|
|
15
|
-
|
|
16
|
-
Checks
|
|
17
|
-
✗ undocumented read in code, missing from .env.example
|
|
18
|
-
✗ missing in .env.example, not set in .env (only when .env exists)
|
|
19
|
-
! unused in .env.example, read by nothing: not your code, not a framework, SDK or image
|
|
20
|
-
! defaulted read with a fallback, missing from .env.example (shown with --optional)
|
|
21
|
-
! empty / extra / duplicate keys in .env
|
|
22
|
-
|
|
23
|
-
Understands .NET IConfiguration and appsettings.json, pydantic-settings, Go env tags,
|
|
24
|
-
envalid and zod env schemas, Prisma, SvelteKit $env, Docker Compose, and the variables
|
|
25
|
-
SDKs read on their own (AWS, OpenAI, Anthropic, Sentry, Auth.js, postgres image, …).
|
|
26
|
-
|
|
27
|
-
Options
|
|
28
|
-
--fix add undocumented keys to .env.example (creates it if needed)
|
|
29
|
-
--optional also report (and --fix) keys that have a default in code
|
|
30
|
-
--list print every env var the code reads, with where
|
|
31
|
-
--example <path> default: .env.example
|
|
32
|
-
--env <path> default: .env — checked only if it exists, unless set explicitly
|
|
33
|
-
--no-code skip scanning source files, only compare the env files
|
|
34
|
-
--ignore <dir> skip a directory (repeatable)
|
|
35
|
-
--ignore-key <KEY> skip a key; PREFIX_* works too (repeatable)
|
|
36
|
-
--
|
|
37
|
-
--
|
|
38
|
-
--
|
|
39
|
-
--
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
* @typedef {{
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
* @
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
if (opt.
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
if (!
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
const
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
const
|
|
99
|
-
const
|
|
100
|
-
const
|
|
101
|
-
const
|
|
102
|
-
|
|
103
|
-
const
|
|
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
|
-
const
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
const
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
for (const [
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
return
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
}
|
|
1
|
+
// @ts-check
|
|
2
|
+
import { appendFileSync, existsSync, readFileSync } from "node:fs";
|
|
3
|
+
import { relative, resolve, sep } from "node:path";
|
|
4
|
+
import { parseArgs } from "node:util";
|
|
5
|
+
import { check, fixText } from "./check.js";
|
|
6
|
+
import { parseEnv } from "./parse.js";
|
|
7
|
+
import { scanProject } from "./scan.js";
|
|
8
|
+
|
|
9
|
+
export const VERSION = "0.2.0";
|
|
10
|
+
|
|
11
|
+
const HELP = `envsonar ${VERSION} — find env vars your code reads but .env.example forgot. Never prints a value.
|
|
12
|
+
|
|
13
|
+
Usage
|
|
14
|
+
envsonar [dir] [options]
|
|
15
|
+
|
|
16
|
+
Checks
|
|
17
|
+
✗ undocumented read in code, missing from .env.example
|
|
18
|
+
✗ missing in .env.example, not set in .env (only when .env exists)
|
|
19
|
+
! unused in .env.example, read by nothing: not your code, not a framework, SDK or image
|
|
20
|
+
! defaulted read with a fallback, missing from .env.example (shown with --optional)
|
|
21
|
+
! empty / extra / duplicate keys in .env
|
|
22
|
+
|
|
23
|
+
Understands .NET IConfiguration and appsettings.json, pydantic-settings, Go env tags,
|
|
24
|
+
envalid and zod env schemas, Prisma, SvelteKit $env, Docker Compose, and the variables
|
|
25
|
+
SDKs read on their own (AWS, OpenAI, Anthropic, Sentry, Auth.js, postgres image, …).
|
|
26
|
+
|
|
27
|
+
Options
|
|
28
|
+
--fix add undocumented keys to .env.example (creates it if needed)
|
|
29
|
+
--optional also report (and --fix) keys that have a default in code
|
|
30
|
+
--list print every env var the code reads, with where
|
|
31
|
+
--example <path> default: .env.example
|
|
32
|
+
--env <path> default: .env — checked only if it exists, unless set explicitly
|
|
33
|
+
--no-code skip scanning source files, only compare the env files
|
|
34
|
+
--ignore <dir> skip a directory (repeatable)
|
|
35
|
+
--ignore-key <KEY> skip a key; PREFIX_* works too (repeatable)
|
|
36
|
+
--include-tests also scan test files and dirs (test/, *.test.ts, test_*.py, …)
|
|
37
|
+
--strict warnings fail too
|
|
38
|
+
--json machine-readable report
|
|
39
|
+
--format <pretty|github> github prints workflow annotations (auto on GitHub Actions)
|
|
40
|
+
--no-color, -h/--help, -v/--version
|
|
41
|
+
|
|
42
|
+
Config: an "envsonar" object in package.json or .envsonarrc.json with the same names
|
|
43
|
+
(example, env, ignore, ignoreKeys, includeTests, strict, optional).
|
|
44
|
+
|
|
45
|
+
Exit codes: 0 clean · 1 problems found · 2 bad usage or unreadable file`;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* @typedef {{ out: (s: string) => void, err: (s: string) => void, cwd: string, color: boolean, githubActions?: boolean, githubOutput?: (s: string) => void }} Io
|
|
49
|
+
* githubOutput — appends step outputs (`errors=…`) on GitHub Actions
|
|
50
|
+
* @typedef {{ example?: string, env?: string, ignore?: string[], ignoreKeys?: string[], includeTests?: boolean, strict?: boolean, optional?: boolean }} Config
|
|
51
|
+
*/
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Runs the CLI and returns the exit code; all output goes through `io`, so tests can capture it.
|
|
55
|
+
*
|
|
56
|
+
* @param {string[]} argv
|
|
57
|
+
* @param {Io} io
|
|
58
|
+
* @returns {number}
|
|
59
|
+
*/
|
|
60
|
+
export function run(argv, io) {
|
|
61
|
+
let parsed;
|
|
62
|
+
try {
|
|
63
|
+
parsed = parseArgs({
|
|
64
|
+
args: argv,
|
|
65
|
+
allowPositionals: true,
|
|
66
|
+
options: {
|
|
67
|
+
example: { type: "string" },
|
|
68
|
+
env: { type: "string" },
|
|
69
|
+
ignore: { type: "string", multiple: true },
|
|
70
|
+
"ignore-key": { type: "string", multiple: true },
|
|
71
|
+
"no-code": { type: "boolean", default: false },
|
|
72
|
+
"include-tests": { type: "boolean" },
|
|
73
|
+
fix: { type: "boolean", default: false },
|
|
74
|
+
optional: { type: "boolean" },
|
|
75
|
+
list: { type: "boolean", default: false },
|
|
76
|
+
strict: { type: "boolean" },
|
|
77
|
+
json: { type: "boolean", default: false },
|
|
78
|
+
format: { type: "string" },
|
|
79
|
+
"no-color": { type: "boolean", default: false },
|
|
80
|
+
help: { type: "boolean", short: "h", default: false },
|
|
81
|
+
version: { type: "boolean", short: "v", default: false },
|
|
82
|
+
},
|
|
83
|
+
});
|
|
84
|
+
} catch (e) {
|
|
85
|
+
return usage(io, e instanceof Error ? e.message : String(e));
|
|
86
|
+
}
|
|
87
|
+
const { values: opt, positionals } = parsed;
|
|
88
|
+
if (opt.help) return io.out(HELP), 0;
|
|
89
|
+
if (opt.version) return io.out(VERSION), 0;
|
|
90
|
+
if (positionals.length > 1) return usage(io, `expected one directory, got "${positionals.join(" ")}"`);
|
|
91
|
+
if (opt.format && !["pretty", "github"].includes(opt.format)) return usage(io, `unknown --format "${opt.format}"`);
|
|
92
|
+
|
|
93
|
+
const root = resolve(io.cwd, positionals[0] ?? ".");
|
|
94
|
+
if (!existsSync(root)) return fail(io, `directory not found: ${positionals[0]}`);
|
|
95
|
+
const config = loadConfig(root, io);
|
|
96
|
+
if (config === null) return 2;
|
|
97
|
+
|
|
98
|
+
const exampleName = opt.example ?? config.example ?? ".env.example";
|
|
99
|
+
const envName = opt.env ?? config.env ?? ".env";
|
|
100
|
+
const envRequired = Boolean(opt.env);
|
|
101
|
+
const strict = opt.strict ?? config.strict ?? false;
|
|
102
|
+
const showDefaulted = opt.optional ?? config.optional ?? false;
|
|
103
|
+
const paint = painter(io.color && !opt["no-color"] && !opt.json);
|
|
104
|
+
const display = (/** @type {string} */ p) => relative(io.cwd, resolve(root, p)).split(sep).join("/") || p;
|
|
105
|
+
|
|
106
|
+
const scan = opt["no-code"]
|
|
107
|
+
? null
|
|
108
|
+
: scanProject(root, {
|
|
109
|
+
ignoreDirs: [...(config.ignore ?? []), ...(opt.ignore ?? [])],
|
|
110
|
+
ignoreKeys: [...(config.ignoreKeys ?? []), ...(opt["ignore-key"] ?? [])],
|
|
111
|
+
includeTests: opt["include-tests"] ?? config.includeTests ?? false,
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
if (opt.list) {
|
|
115
|
+
if (!scan) return usage(io, "--list needs the code scan, drop --no-code");
|
|
116
|
+
return list(scan, opt.json, paint, io, display);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const examplePath = resolve(root, exampleName);
|
|
120
|
+
let exampleText = readOptional(examplePath, io);
|
|
121
|
+
if (exampleText === undefined) return 2;
|
|
122
|
+
if (exampleText === null && !opt.fix) {
|
|
123
|
+
return fail(io, `${display(exampleName)} not found — run "envsonar --fix" to create it from your code`);
|
|
124
|
+
}
|
|
125
|
+
const example = parseEnv(exampleText ?? "");
|
|
126
|
+
|
|
127
|
+
const envText = readOptional(resolve(root, envName), io);
|
|
128
|
+
if (envText === undefined) return 2;
|
|
129
|
+
if (envText === null && envRequired) return fail(io, `${display(envName)} not found`);
|
|
130
|
+
const env = envText === null ? null : parseEnv(envText);
|
|
131
|
+
|
|
132
|
+
// scan paths are relative to the scanned dir; reports use paths relative to where envsonar runs, like the env files
|
|
133
|
+
const runCheck = (/** @type {import("./parse.js").ParsedEnv} */ ex) =>
|
|
134
|
+
check({ example: ex, exampleFile: display(exampleName), env, envFile: display(envName), scan, strict }).map((f) =>
|
|
135
|
+
f.usages ? { ...f, file: display(f.file), usages: f.usages.map((u) => ({ ...u, file: display(u.file) })) } : f,
|
|
136
|
+
);
|
|
137
|
+
let all = runCheck(example);
|
|
138
|
+
const shown = (/** @type {import("./check.js").Finding[]} */ list) => (showDefaulted ? list : list.filter((f) => f.code !== "defaulted"));
|
|
139
|
+
let findings = shown(all);
|
|
140
|
+
|
|
141
|
+
if (opt.fix) {
|
|
142
|
+
const added = findings.filter((f) => f.code === "undocumented" || f.code === "defaulted");
|
|
143
|
+
if (added.length) {
|
|
144
|
+
appendFileSync(examplePath, fixText(added, !exampleText?.trim()));
|
|
145
|
+
io.out(`${paint.green("✓")} added ${plural(added.length, "key")} to ${display(exampleName)}: ${added.map((f) => f.key).join(", ")}`);
|
|
146
|
+
exampleText = readFileSync(examplePath, "utf8");
|
|
147
|
+
all = runCheck(parseEnv(exampleText));
|
|
148
|
+
findings = shown(all);
|
|
149
|
+
} else {
|
|
150
|
+
io.out(`${paint.green("✓")} ${display(exampleName)} already documents every key the code reads`);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const errors = findings.filter((f) => f.severity === "error").length;
|
|
155
|
+
const warnings = findings.length - errors;
|
|
156
|
+
const files = scan ? scan.files : 0;
|
|
157
|
+
const hiddenDefaulted = all.length - findings.length;
|
|
158
|
+
const frameworks = scan ? scan.implicit.map((i) => i.source) : [];
|
|
159
|
+
io.githubOutput?.(`errors=${errors}\nwarnings=${warnings}`);
|
|
160
|
+
|
|
161
|
+
if (opt.json) {
|
|
162
|
+
const dynamic = (scan?.dynamic ?? []).map((d) => ({ ...d, file: display(d.file) }));
|
|
163
|
+
const report = { ok: errors === 0, errors, warnings, scannedFiles: files, frameworks, hiddenDefaulted, findings, dynamic };
|
|
164
|
+
io.out(JSON.stringify(report, null, 2));
|
|
165
|
+
return errors ? 1 : 0;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const format = opt.format ?? (io.githubActions ? "github" : "pretty");
|
|
169
|
+
if (format === "github") {
|
|
170
|
+
for (const f of findings) {
|
|
171
|
+
const level = f.severity === "error" ? "error" : "warning";
|
|
172
|
+
const where = `file=${f.file}${f.line ? `,line=${f.line}` : ""}`;
|
|
173
|
+
io.out(`::${level} ${where},title=envsonar ${f.code}::${f.key ? `${f.key} ` : ""}${f.message}`);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
const header = scan
|
|
178
|
+
? paint.dim(`envsonar · ${files} files scanned · ${scan.usages.size} keys in code · ${example.entries.size} in ${display(exampleName)}${env ? ` · ${display(envName)} checked` : ""}`)
|
|
179
|
+
: paint.dim(`envsonar · ${display(exampleName)}${env ? ` vs ${display(envName)}` : ""}`);
|
|
180
|
+
io.out(header);
|
|
181
|
+
if (frameworks.length) io.out(paint.dim(`also read by: ${frameworks.join(", ")}`));
|
|
182
|
+
const notes = () => {
|
|
183
|
+
if (hiddenDefaulted) io.out(paint.dim(`${plural(hiddenDefaulted, "key")} with a default in code not in ${display(exampleName)} — show with --optional`));
|
|
184
|
+
if (scan?.dynamic.length) {
|
|
185
|
+
const where = scan.dynamic.slice(0, 3).map((d) => `${display(d.file)}:${d.line}`).join(", ");
|
|
186
|
+
io.out(paint.dim(`${plural(scan.dynamic.length, "dynamic read")} can't be checked: ${where}${scan.dynamic.length > 3 ? ", …" : ""}`));
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
if (findings.length === 0) {
|
|
191
|
+
io.out(`${paint.green("✓")} all in sync`);
|
|
192
|
+
notes();
|
|
193
|
+
return 0;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
io.out("");
|
|
197
|
+
const width = Math.max(...findings.map((f) => (f.key ?? "").length), 3);
|
|
198
|
+
const msgWidth = Math.max(...findings.map((f) => f.message.length));
|
|
199
|
+
for (const f of findings) {
|
|
200
|
+
const mark = f.severity === "error" ? paint.red("✗") : paint.yellow("!");
|
|
201
|
+
const more = f.usages && f.usages.length > 1 ? paint.dim(` (+${f.usages.length - 1})`) : "";
|
|
202
|
+
const where = paint.dim(`${f.file}${f.line ? `:${f.line}` : ""}`) + more;
|
|
203
|
+
io.out(` ${mark} ${paint.bold((f.key ?? "").padEnd(width))} ${f.message.padEnd(msgWidth)} ${where}`);
|
|
204
|
+
}
|
|
205
|
+
const summary = [errors && paint.red(plural(errors, "error")), warnings && paint.yellow(plural(warnings, "warning"))].filter(Boolean).join(", ");
|
|
206
|
+
const undocumented = findings.filter((f) => f.code === "undocumented").length;
|
|
207
|
+
const hint = undocumented ? paint.dim(` · run envsonar --fix to add ${plural(undocumented, "key")} to ${display(exampleName)}`) : "";
|
|
208
|
+
io.out(`\n${summary}${hint}`);
|
|
209
|
+
notes();
|
|
210
|
+
return errors ? 1 : 0;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* @param {import("./scan.js").ScanResult} scan
|
|
215
|
+
* @param {boolean} json
|
|
216
|
+
* @param {ReturnType<typeof painter>} paint
|
|
217
|
+
* @param {Io} io
|
|
218
|
+
* @param {(p: string) => string} display
|
|
219
|
+
*/
|
|
220
|
+
function list(scan, json, paint, io, display) {
|
|
221
|
+
const keys = [...scan.usages].sort(([a], [b]) => a.localeCompare(b));
|
|
222
|
+
const frameworks = scan.implicit.map((i) => i.source);
|
|
223
|
+
if (json) {
|
|
224
|
+
const report = { keys: Object.fromEntries(keys), sections: Object.fromEntries(scan.sections), dynamic: scan.dynamic, frameworks };
|
|
225
|
+
io.out(JSON.stringify(report, null, 2));
|
|
226
|
+
return 0;
|
|
227
|
+
}
|
|
228
|
+
io.out(paint.dim(`envsonar · ${scan.files} files scanned · ${keys.length} keys`));
|
|
229
|
+
if (frameworks.length) io.out(paint.dim(`also read by: ${frameworks.join(", ")}`));
|
|
230
|
+
const width = Math.max(...keys.map(([k]) => k.length), 3);
|
|
231
|
+
for (const [key, uses] of keys) {
|
|
232
|
+
const more = uses.length > 1 ? paint.dim(` (+${uses.length - 1})`) : "";
|
|
233
|
+
const tag = uses.every((u) => u.optional) ? paint.dim(" default") : "";
|
|
234
|
+
io.out(` ${paint.bold(key.padEnd(width))} ${paint.dim(`${display(uses[0].file)}:${uses[0].line}`)}${more}${tag}`);
|
|
235
|
+
}
|
|
236
|
+
for (const [prefix, uses] of scan.sections) io.out(` ${paint.bold(`${prefix}*`.padEnd(width))} ${paint.dim(`${display(uses[0].file)}:${uses[0].line} section`)}`);
|
|
237
|
+
for (const d of scan.dynamic) io.out(` ${paint.yellow("?")} ${paint.dim(`${d.expr} ${display(d.file)}:${d.line} dynamic, not checked`)}`);
|
|
238
|
+
return 0;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* @param {string} root
|
|
243
|
+
* @param {Io} io
|
|
244
|
+
* @returns {Config | null}
|
|
245
|
+
*/
|
|
246
|
+
function loadConfig(root, io) {
|
|
247
|
+
for (const [file, pick] of /** @type {const} */ ([
|
|
248
|
+
[".envsonarrc.json", (/** @type {any} */ j) => j],
|
|
249
|
+
["package.json", (/** @type {any} */ j) => j.envsonar],
|
|
250
|
+
])) {
|
|
251
|
+
const text = readOptional(resolve(root, file), io);
|
|
252
|
+
if (text === undefined) return null;
|
|
253
|
+
if (text === null) continue;
|
|
254
|
+
try {
|
|
255
|
+
const config = pick(JSON.parse(text));
|
|
256
|
+
if (config === undefined) continue;
|
|
257
|
+
if (typeof config !== "object" || config === null || Array.isArray(config)) throw new Error("expected an object");
|
|
258
|
+
return /** @type {Config} */ (config);
|
|
259
|
+
} catch (e) {
|
|
260
|
+
io.err(`envsonar: invalid config in ${file}: ${e instanceof Error ? e.message : String(e)}`);
|
|
261
|
+
return null;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
return {};
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* @param {string} path
|
|
269
|
+
* @param {Io} io
|
|
270
|
+
* @returns {string | null | undefined} null when the file doesn't exist, undefined when it can't be read
|
|
271
|
+
*/
|
|
272
|
+
function readOptional(path, io) {
|
|
273
|
+
try {
|
|
274
|
+
return readFileSync(path, "utf8");
|
|
275
|
+
} catch (e) {
|
|
276
|
+
const code = /** @type {NodeJS.ErrnoException} */ (e).code;
|
|
277
|
+
if (code === "ENOENT") return null;
|
|
278
|
+
io.err(`envsonar: cannot read ${path} (${code ?? "unknown error"})`);
|
|
279
|
+
return undefined;
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/** @param {Io} io @param {string} message */
|
|
284
|
+
function usage(io, message) {
|
|
285
|
+
io.err(`envsonar: ${message}\nRun "envsonar --help" for usage.`);
|
|
286
|
+
return 2;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/** @param {Io} io @param {string} message */
|
|
290
|
+
function fail(io, message) {
|
|
291
|
+
io.err(`envsonar: ${message}`);
|
|
292
|
+
return 2;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/** @param {number} n @param {string} word */
|
|
296
|
+
const plural = (n, word) => `${n} ${word}${n === 1 ? "" : "s"}`;
|
|
297
|
+
|
|
298
|
+
/** @param {boolean} on */
|
|
299
|
+
function painter(on) {
|
|
300
|
+
/** @param {number} a @param {number} b */
|
|
301
|
+
const wrap = (a, b) => (/** @type {string} */ s) => (on ? `\x1b[${a}m${s}\x1b[${b}m` : s);
|
|
302
|
+
return { red: wrap(31, 39), green: wrap(32, 39), yellow: wrap(33, 39), dim: wrap(2, 22), bold: wrap(1, 22) };
|
|
303
|
+
}
|