@jutaru-dev/envsonar 0.0.0-stage → 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/LICENSE +21 -0
- package/README.md +187 -2
- package/bin/envsonar.js +13 -0
- package/package.json +55 -4
- package/src/check.js +157 -0
- package/src/cli.js +303 -0
- package/src/index.js +5 -0
- package/src/parse.js +99 -0
- package/src/profiles.js +80 -0
- package/src/scan.js +796 -0
- 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
ADDED
|
@@ -0,0 +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.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
|
+
}
|
package/src/index.js
ADDED
package/src/parse.js
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @typedef {{ value: string, line: number, endLine: number }} Entry
|
|
5
|
+
* @typedef {{ line: number, message: string }} ParseIssue
|
|
6
|
+
* @typedef {{
|
|
7
|
+
* entries: Map<string, Entry>,
|
|
8
|
+
* duplicates: { key: string, lines: number[] }[],
|
|
9
|
+
* errors: ParseIssue[],
|
|
10
|
+
* }} ParsedEnv
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
const KEY = /^[A-Za-z_][A-Za-z0-9_.-]*$/;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Parses dotenv text. Supports comments, `export KEY=...`, single/double/backtick quotes,
|
|
17
|
+
* multi-line quoted values and inline `#` comments after unquoted values.
|
|
18
|
+
* Never throws: malformed lines are reported in `errors`.
|
|
19
|
+
*
|
|
20
|
+
* @param {string} text
|
|
21
|
+
* @returns {ParsedEnv}
|
|
22
|
+
*/
|
|
23
|
+
export function parseEnv(text) {
|
|
24
|
+
const lines = text.replace(/^\uFEFF/, "").split(/\r?\n/);
|
|
25
|
+
/** @type {Map<string, Entry>} */
|
|
26
|
+
const entries = new Map();
|
|
27
|
+
/** @type {Map<string, number[]>} */
|
|
28
|
+
const seen = new Map();
|
|
29
|
+
/** @type {ParseIssue[]} */
|
|
30
|
+
const errors = [];
|
|
31
|
+
|
|
32
|
+
for (let i = 0; i < lines.length; i++) {
|
|
33
|
+
const lineNo = i + 1;
|
|
34
|
+
const raw = lines[i].trim();
|
|
35
|
+
if (raw === "" || raw.startsWith("#")) continue;
|
|
36
|
+
|
|
37
|
+
const body = raw.startsWith("export ") ? raw.slice(7).trimStart() : raw;
|
|
38
|
+
const eq = body.indexOf("=");
|
|
39
|
+
if (eq === -1) {
|
|
40
|
+
errors.push({ line: lineNo, message: "expected KEY=VALUE" });
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
const key = body.slice(0, eq).trim();
|
|
44
|
+
if (!KEY.test(key)) {
|
|
45
|
+
errors.push({ line: lineNo, message: "invalid key name" });
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
let rest = body.slice(eq + 1).trimStart();
|
|
50
|
+
let value;
|
|
51
|
+
const quote = rest[0];
|
|
52
|
+
if (quote === '"' || quote === "'" || quote === "`") {
|
|
53
|
+
// a quoted value may continue over the following lines until its closing quote
|
|
54
|
+
let collected = rest.slice(1);
|
|
55
|
+
let close = findClosing(collected, quote);
|
|
56
|
+
let j = i;
|
|
57
|
+
while (close === -1 && j + 1 < lines.length) {
|
|
58
|
+
j++;
|
|
59
|
+
collected += "\n" + lines[j];
|
|
60
|
+
close = findClosing(collected, quote);
|
|
61
|
+
}
|
|
62
|
+
if (close === -1) {
|
|
63
|
+
errors.push({ line: lineNo, message: "unterminated quoted value" });
|
|
64
|
+
i = j;
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
value = collected.slice(0, close);
|
|
68
|
+
// one pass, so `\\n` stays a backslash followed by "n" instead of turning into a newline
|
|
69
|
+
if (quote === '"') value = value.replace(/\\([nr"\\])/g, (_, c) => (c === "n" ? "\n" : c === "r" ? "\r" : c));
|
|
70
|
+
i = j;
|
|
71
|
+
} else {
|
|
72
|
+
const hash = rest.startsWith("#") ? 0 : rest.search(/\s#/);
|
|
73
|
+
value = (hash === -1 ? rest : rest.slice(0, hash)).trim();
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const prev = seen.get(key);
|
|
77
|
+
if (prev) prev.push(lineNo);
|
|
78
|
+
else seen.set(key, [lineNo]);
|
|
79
|
+
entries.set(key, { value, line: lineNo, endLine: i + 1 });
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const duplicates = [...seen].filter(([, l]) => l.length > 1).map(([key, l]) => ({ key, lines: l }));
|
|
83
|
+
return { entries, duplicates, errors };
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* @param {string} s
|
|
88
|
+
* @param {string} quote
|
|
89
|
+
*/
|
|
90
|
+
function findClosing(s, quote) {
|
|
91
|
+
for (let k = 0; k < s.length; k++) {
|
|
92
|
+
if (s[k] === "\\" && quote === '"') {
|
|
93
|
+
k++;
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
if (s[k] === quote) return k;
|
|
97
|
+
}
|
|
98
|
+
return -1;
|
|
99
|
+
}
|
package/src/profiles.js
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Variables that SDKs, frameworks and container images read on their own, without a line of your code
|
|
5
|
+
* naming them. A key from this list in .env.example is never reported as unused once the matching
|
|
6
|
+
* dependency (or compose image) is found.
|
|
7
|
+
*
|
|
8
|
+
* markers: `npm:<pkg>`, `py:<pkg>`, `composer:<pkg>`, `nuget:<pkg>`, `image:<name>`, `dotnet`, `compose`;
|
|
9
|
+
* a trailing `*` matches a prefix.
|
|
10
|
+
*
|
|
11
|
+
* @type {{ source: string, markers: string[], keys: string[] }[]}
|
|
12
|
+
*/
|
|
13
|
+
export const PROFILES = [
|
|
14
|
+
{ source: "Next.js", markers: ["npm:next"], keys: ["PORT", "HOSTNAME", "NEXT_TELEMETRY_DISABLED"] },
|
|
15
|
+
{ source: "Nuxt", markers: ["npm:nuxt"], keys: ["NUXT_*", "NITRO_*", "PORT", "HOST"] },
|
|
16
|
+
{ source: "Auth.js", markers: ["npm:next-auth", "npm:@auth/*"], keys: ["NEXTAUTH_*", "AUTH_*"] },
|
|
17
|
+
{ source: "Prisma", markers: ["npm:prisma", "npm:@prisma/client"], keys: ["PRISMA_*"] },
|
|
18
|
+
{
|
|
19
|
+
source: "OpenAI SDK",
|
|
20
|
+
markers: ["npm:openai", "npm:@ai-sdk/openai", "py:openai", "py:langchain-openai", "composer:openai-php/*"],
|
|
21
|
+
keys: ["OPENAI_API_KEY", "OPENAI_BASE_URL", "OPENAI_ORG_ID", "OPENAI_PROJECT_ID"],
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
source: "Anthropic SDK",
|
|
25
|
+
markers: ["npm:@anthropic-ai/sdk", "npm:@ai-sdk/anthropic", "py:anthropic", "py:langchain-anthropic"],
|
|
26
|
+
keys: ["ANTHROPIC_API_KEY", "ANTHROPIC_AUTH_TOKEN", "ANTHROPIC_BASE_URL"],
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
source: "Google AI SDK",
|
|
30
|
+
markers: ["npm:@ai-sdk/google", "npm:@google/genai", "py:google-genai"],
|
|
31
|
+
keys: ["GOOGLE_GENERATIVE_AI_API_KEY", "GEMINI_API_KEY", "GOOGLE_API_KEY"],
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
source: "AWS SDK",
|
|
35
|
+
markers: ["npm:aws-sdk", "npm:@aws-sdk/*", "py:boto3", "py:botocore", "composer:aws/aws-sdk-php", "nuget:AWSSDK.*"],
|
|
36
|
+
keys: ["AWS_*"],
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
source: "Google Cloud",
|
|
40
|
+
markers: ["npm:firebase-admin", "npm:@google-cloud/*", "py:google-cloud-*", "py:firebase-admin", "nuget:Google.Cloud.*"],
|
|
41
|
+
keys: ["GOOGLE_APPLICATION_CREDENTIALS", "GOOGLE_CLOUD_PROJECT", "GCLOUD_PROJECT", "FIREBASE_CONFIG"],
|
|
42
|
+
},
|
|
43
|
+
{ source: "Sentry", markers: ["npm:@sentry/*", "py:sentry-sdk", "composer:sentry/*", "nuget:Sentry*"], keys: ["SENTRY_*"] },
|
|
44
|
+
{
|
|
45
|
+
source: "libpq",
|
|
46
|
+
markers: ["npm:pg", "npm:postgres", "py:psycopg", "py:psycopg2", "py:psycopg2-binary", "py:asyncpg"],
|
|
47
|
+
keys: ["PGHOST", "PGPORT", "PGUSER", "PGPASSWORD", "PGDATABASE", "PGSSLMODE", "PGAPPNAME"],
|
|
48
|
+
},
|
|
49
|
+
{ source: "Vercel Postgres", markers: ["npm:@vercel/postgres"], keys: ["POSTGRES_*"] },
|
|
50
|
+
{ source: "Vercel KV", markers: ["npm:@vercel/kv"], keys: ["KV_*"] },
|
|
51
|
+
{ source: "Vercel Blob", markers: ["npm:@vercel/blob"], keys: ["BLOB_READ_WRITE_TOKEN"] },
|
|
52
|
+
{ source: "Upstash Redis", markers: ["npm:@upstash/redis"], keys: ["UPSTASH_REDIS_REST_URL", "UPSTASH_REDIS_REST_TOKEN"] },
|
|
53
|
+
{ source: "dotenvx", markers: ["npm:@dotenvx/dotenvx"], keys: ["DOTENV_*"] },
|
|
54
|
+
{ source: "Django", markers: ["py:django"], keys: ["DJANGO_SETTINGS_MODULE"] },
|
|
55
|
+
{
|
|
56
|
+
source: "Laravel",
|
|
57
|
+
markers: ["composer:laravel/framework"],
|
|
58
|
+
keys: [
|
|
59
|
+
"APP_*", "LOG_*", "DB_*", "SESSION_*", "BROADCAST_*", "FILESYSTEM_*", "QUEUE_*", "CACHE_*", "MEMCACHED_*",
|
|
60
|
+
"REDIS_*", "MAIL_*", "PUSHER_*", "VITE_*", "BCRYPT_ROUNDS", "PHP_CLI_SERVER_WORKERS",
|
|
61
|
+
],
|
|
62
|
+
},
|
|
63
|
+
{ source: "ASP.NET Core", markers: ["dotnet"], keys: ["ASPNETCORE_*", "DOTNET_*", "Logging__*", "Kestrel__*", "AllowedHosts", "Serilog__*"] },
|
|
64
|
+
{ source: "Docker Compose", markers: ["compose"], keys: ["COMPOSE_*", "DOCKER_*"] },
|
|
65
|
+
{ source: "postgres image", markers: ["image:postgres", "image:postgis", "image:postgresql"], keys: ["POSTGRES_*", "PGDATA"] },
|
|
66
|
+
{ source: "mysql image", markers: ["image:mysql"], keys: ["MYSQL_*"] },
|
|
67
|
+
{ source: "mariadb image", markers: ["image:mariadb"], keys: ["MARIADB_*", "MYSQL_*"] },
|
|
68
|
+
{ source: "mongo image", markers: ["image:mongo"], keys: ["MONGO_INITDB_*"] },
|
|
69
|
+
{ source: "redis image", markers: ["image:redis", "image:valkey"], keys: ["REDIS_PASSWORD", "REDIS_ARGS"] },
|
|
70
|
+
{ source: "rabbitmq image", markers: ["image:rabbitmq"], keys: ["RABBITMQ_*"] },
|
|
71
|
+
{ source: "minio image", markers: ["image:minio"], keys: ["MINIO_*"] },
|
|
72
|
+
{ source: "pgadmin image", markers: ["image:pgadmin4"], keys: ["PGADMIN_*"] },
|
|
73
|
+
{ source: "keycloak image", markers: ["image:keycloak"], keys: ["KC_*", "KEYCLOAK_*"] },
|
|
74
|
+
{ source: "clickhouse image", markers: ["image:clickhouse-server"], keys: ["CLICKHOUSE_*"] },
|
|
75
|
+
{ source: "elasticsearch image", markers: ["image:elasticsearch"], keys: ["ELASTIC_*", "ES_JAVA_OPTS"] },
|
|
76
|
+
{ source: "mssql image", markers: ["image:mssql-server"], keys: ["MSSQL_*", "ACCEPT_EULA", "SA_PASSWORD"] },
|
|
77
|
+
{ source: "grafana image", markers: ["image:grafana"], keys: ["GF_*"] },
|
|
78
|
+
{ source: "mailpit image", markers: ["image:mailpit"], keys: ["MP_*"] },
|
|
79
|
+
{ source: "n8n image", markers: ["image:n8n"], keys: ["N8N_*"] },
|
|
80
|
+
];
|