@subramanyaks/env-drift 1.1.0 → 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 +13 -0
- package/README.md +21 -5
- package/dist/cli.js +590 -35
- package/dist/index.cjs +524 -31
- package/dist/index.d.cts +81 -1
- package/dist/index.d.ts +81 -1
- package/dist/index.js +513 -29
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -4,28 +4,31 @@ import { resolve } from "path";
|
|
|
4
4
|
// src/envParser.ts
|
|
5
5
|
import { existsSync, readFileSync, writeFileSync } from "fs";
|
|
6
6
|
var ENV_LINE_REGEX = /^(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.*)$/;
|
|
7
|
+
function parseDoubleQuotedValue(raw) {
|
|
8
|
+
let result = "";
|
|
9
|
+
for (let i = 1; i < raw.length; i++) {
|
|
10
|
+
const ch = raw[i];
|
|
11
|
+
if (ch === "\\" && i + 1 < raw.length) {
|
|
12
|
+
const next = raw[i + 1];
|
|
13
|
+
if (next === "n") result += "\n";
|
|
14
|
+
else if (next === "r") result += "\r";
|
|
15
|
+
else if (next === "t") result += " ";
|
|
16
|
+
else result += next;
|
|
17
|
+
i++;
|
|
18
|
+
} else if (ch === '"') {
|
|
19
|
+
return { value: result, closed: true };
|
|
20
|
+
} else {
|
|
21
|
+
result += ch;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return { value: result, closed: false };
|
|
25
|
+
}
|
|
7
26
|
function parseValue(raw) {
|
|
8
27
|
const trimmed = raw.trim();
|
|
9
28
|
if (!trimmed) return "";
|
|
10
29
|
const quote = trimmed[0];
|
|
11
30
|
if (quote === '"') {
|
|
12
|
-
|
|
13
|
-
for (let i = 1; i < trimmed.length; i++) {
|
|
14
|
-
const ch = trimmed[i];
|
|
15
|
-
if (ch === "\\" && i + 1 < trimmed.length) {
|
|
16
|
-
const next = trimmed[i + 1];
|
|
17
|
-
if (next === "n") result += "\n";
|
|
18
|
-
else if (next === "r") result += "\r";
|
|
19
|
-
else if (next === "t") result += " ";
|
|
20
|
-
else result += next;
|
|
21
|
-
i++;
|
|
22
|
-
} else if (ch === '"') {
|
|
23
|
-
return result;
|
|
24
|
-
} else {
|
|
25
|
-
result += ch;
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
return result;
|
|
31
|
+
return parseDoubleQuotedValue(trimmed).value;
|
|
29
32
|
}
|
|
30
33
|
if (quote === "'") {
|
|
31
34
|
const closingIdx = trimmed.indexOf("'", 1);
|
|
@@ -56,14 +59,28 @@ function parseEnvContent(content) {
|
|
|
56
59
|
const key = match[1];
|
|
57
60
|
const rawValue = match[2] ?? "";
|
|
58
61
|
if (!key) continue;
|
|
62
|
+
const startLine = i + 1;
|
|
59
63
|
if (seen.has(key)) {
|
|
60
64
|
duplicateSet.add(key);
|
|
61
65
|
} else {
|
|
62
66
|
seen.add(key);
|
|
63
67
|
keys.push(key);
|
|
64
68
|
}
|
|
65
|
-
|
|
66
|
-
|
|
69
|
+
const trimmedVal = rawValue.trimStart();
|
|
70
|
+
if (trimmedVal.startsWith('"')) {
|
|
71
|
+
let combined = trimmedVal;
|
|
72
|
+
let parsed = parseDoubleQuotedValue(combined);
|
|
73
|
+
while (!parsed.closed && i + 1 < lines.length) {
|
|
74
|
+
i++;
|
|
75
|
+
combined += `
|
|
76
|
+
${lines[i]}`;
|
|
77
|
+
parsed = parseDoubleQuotedValue(combined);
|
|
78
|
+
}
|
|
79
|
+
values[key] = parsed.value;
|
|
80
|
+
} else {
|
|
81
|
+
values[key] = parseValue(rawValue);
|
|
82
|
+
}
|
|
83
|
+
keyLines[key] = startLine;
|
|
67
84
|
}
|
|
68
85
|
return {
|
|
69
86
|
keys,
|
|
@@ -148,12 +165,109 @@ var DEFAULT_EXTENSIONS = /* @__PURE__ */ new Set([
|
|
|
148
165
|
".svelte",
|
|
149
166
|
".astro"
|
|
150
167
|
]);
|
|
168
|
+
var REGEX_PRECEDING_KEYWORDS = /* @__PURE__ */ new Set([
|
|
169
|
+
"return",
|
|
170
|
+
"case",
|
|
171
|
+
"throw",
|
|
172
|
+
"in",
|
|
173
|
+
"of",
|
|
174
|
+
"typeof",
|
|
175
|
+
"void",
|
|
176
|
+
"delete",
|
|
177
|
+
"await",
|
|
178
|
+
"yield",
|
|
179
|
+
"else",
|
|
180
|
+
"do"
|
|
181
|
+
]);
|
|
151
182
|
function stripCommentsPreservingLines(source) {
|
|
152
|
-
const
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
183
|
+
const out = [];
|
|
184
|
+
const len = source.length;
|
|
185
|
+
let i = 0;
|
|
186
|
+
let prevSignificant = "";
|
|
187
|
+
let prevWord = "";
|
|
188
|
+
while (i < len) {
|
|
189
|
+
const ch = source[i];
|
|
190
|
+
const next = i + 1 < len ? source[i + 1] : "";
|
|
191
|
+
if (ch === '"' || ch === "'" || ch === "`") {
|
|
192
|
+
const quote = ch;
|
|
193
|
+
out.push(ch);
|
|
194
|
+
i++;
|
|
195
|
+
while (i < len) {
|
|
196
|
+
const c = source[i];
|
|
197
|
+
out.push(c);
|
|
198
|
+
i++;
|
|
199
|
+
if (c === "\\" && i < len) {
|
|
200
|
+
out.push(source[i]);
|
|
201
|
+
i++;
|
|
202
|
+
} else if (c === quote) {
|
|
203
|
+
break;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
prevSignificant = quote;
|
|
207
|
+
prevWord = "";
|
|
208
|
+
continue;
|
|
209
|
+
}
|
|
210
|
+
if (ch === "/" && next === "/") {
|
|
211
|
+
out.push(" ", " ");
|
|
212
|
+
i += 2;
|
|
213
|
+
while (i < len && source[i] !== "\n" && source[i] !== "\r") {
|
|
214
|
+
out.push(" ");
|
|
215
|
+
i++;
|
|
216
|
+
}
|
|
217
|
+
continue;
|
|
218
|
+
}
|
|
219
|
+
if (ch === "/" && next === "*") {
|
|
220
|
+
out.push(" ", " ");
|
|
221
|
+
i += 2;
|
|
222
|
+
while (i < len) {
|
|
223
|
+
if (source[i] === "*" && i + 1 < len && source[i + 1] === "/") {
|
|
224
|
+
out.push(" ", " ");
|
|
225
|
+
i += 2;
|
|
226
|
+
break;
|
|
227
|
+
}
|
|
228
|
+
const c = source[i];
|
|
229
|
+
out.push(c === "\n" || c === "\r" ? c : " ");
|
|
230
|
+
i++;
|
|
231
|
+
}
|
|
232
|
+
continue;
|
|
233
|
+
}
|
|
234
|
+
if (ch === "/" && (prevSignificant === "" || "=([{,;:!&|?+-*%^~<>".includes(prevSignificant) || REGEX_PRECEDING_KEYWORDS.has(prevWord))) {
|
|
235
|
+
out.push(ch);
|
|
236
|
+
i++;
|
|
237
|
+
let inCharClass = false;
|
|
238
|
+
while (i < len) {
|
|
239
|
+
const c = source[i];
|
|
240
|
+
out.push(c);
|
|
241
|
+
i++;
|
|
242
|
+
if (c === "\\" && i < len) {
|
|
243
|
+
out.push(source[i]);
|
|
244
|
+
i++;
|
|
245
|
+
} else if (c === "[") {
|
|
246
|
+
inCharClass = true;
|
|
247
|
+
} else if (c === "]") {
|
|
248
|
+
inCharClass = false;
|
|
249
|
+
} else if (c === "/" && !inCharClass) {
|
|
250
|
+
break;
|
|
251
|
+
} else if (c === "\n" || c === "\r") {
|
|
252
|
+
break;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
prevSignificant = "/";
|
|
256
|
+
prevWord = "";
|
|
257
|
+
continue;
|
|
258
|
+
}
|
|
259
|
+
out.push(ch);
|
|
260
|
+
if (!/\s/.test(ch)) {
|
|
261
|
+
if (/[A-Za-z0-9_$]/.test(ch)) {
|
|
262
|
+
prevWord = /[A-Za-z0-9_$]/.test(prevSignificant) ? prevWord + ch : ch;
|
|
263
|
+
} else {
|
|
264
|
+
prevWord = "";
|
|
265
|
+
}
|
|
266
|
+
prevSignificant = ch;
|
|
267
|
+
}
|
|
268
|
+
i++;
|
|
269
|
+
}
|
|
270
|
+
return out.join("");
|
|
157
271
|
}
|
|
158
272
|
function getLineNumber(source, index) {
|
|
159
273
|
let line = 1;
|
|
@@ -498,6 +612,9 @@ function normalizeConfig(raw) {
|
|
|
498
612
|
if (publicPrefixes) config.publicPrefixes = publicPrefixes;
|
|
499
613
|
const sensitiveKeywords = toStringArray(obj.sensitiveKeywords);
|
|
500
614
|
if (sensitiveKeywords) config.sensitiveKeywords = sensitiveKeywords;
|
|
615
|
+
if (typeof obj.strict === "boolean") {
|
|
616
|
+
config.strict = obj.strict;
|
|
617
|
+
}
|
|
501
618
|
return config;
|
|
502
619
|
}
|
|
503
620
|
function loadConfig(rootDir) {
|
|
@@ -524,9 +641,114 @@ function loadConfig(rootDir) {
|
|
|
524
641
|
return {};
|
|
525
642
|
}
|
|
526
643
|
|
|
527
|
-
// src/
|
|
644
|
+
// src/diff.ts
|
|
645
|
+
import { resolve as resolve3 } from "path";
|
|
646
|
+
function diffEnvFiles(fileA, fileB, options = {}) {
|
|
647
|
+
const rootDir = resolve3(options.rootDir ?? process.cwd());
|
|
648
|
+
const ignoreSet = /* @__PURE__ */ new Set([...BUILTIN_ENV_VARS, ...options.ignore ?? []]);
|
|
649
|
+
const parsedA = parseEnvFile(resolve3(rootDir, fileA));
|
|
650
|
+
const parsedB = parseEnvFile(resolve3(rootDir, fileB));
|
|
651
|
+
const keysA = parsedA.keys.filter((k) => !ignoreSet.has(k));
|
|
652
|
+
const keysB = parsedB.keys.filter((k) => !ignoreSet.has(k));
|
|
653
|
+
const setA = new Set(keysA);
|
|
654
|
+
const setB = new Set(keysB);
|
|
655
|
+
const onlyInA = keysA.filter((k) => !setB.has(k)).sort();
|
|
656
|
+
const onlyInB = keysB.filter((k) => !setA.has(k)).sort();
|
|
657
|
+
const inBoth = keysA.filter((k) => setB.has(k)).sort();
|
|
658
|
+
const emptyInA = keysA.filter((k) => (parsedA.values[k] ?? "").trim() === "").sort();
|
|
659
|
+
const emptyInB = keysB.filter((k) => (parsedB.values[k] ?? "").trim() === "").sort();
|
|
660
|
+
return {
|
|
661
|
+
fileA,
|
|
662
|
+
fileB,
|
|
663
|
+
fileAExists: parsedA.exists,
|
|
664
|
+
fileBExists: parsedB.exists,
|
|
665
|
+
onlyInA,
|
|
666
|
+
onlyInB,
|
|
667
|
+
inBoth,
|
|
668
|
+
emptyInA,
|
|
669
|
+
emptyInB
|
|
670
|
+
};
|
|
671
|
+
}
|
|
672
|
+
function hasDiffIssues(diff, options = {}) {
|
|
673
|
+
if (!diff.fileAExists || !diff.fileBExists) return true;
|
|
674
|
+
if (diff.onlyInA.length > 0 || diff.onlyInB.length > 0) return true;
|
|
675
|
+
if (options.strict && (diff.emptyInA.length > 0 || diff.emptyInB.length > 0)) {
|
|
676
|
+
return true;
|
|
677
|
+
}
|
|
678
|
+
return false;
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
// src/schemagen.ts
|
|
528
682
|
import { mkdirSync, writeFileSync as writeFileSync2 } from "fs";
|
|
529
683
|
import { dirname } from "path";
|
|
684
|
+
function inferZodRule(key) {
|
|
685
|
+
if (key === "NODE_ENV") {
|
|
686
|
+
return 'z.enum(["development", "production", "test"]).default("development")';
|
|
687
|
+
}
|
|
688
|
+
if (/(?:^|_)(?:URL|URI|ENDPOINT)$/i.test(key)) {
|
|
689
|
+
return "z.string().url()";
|
|
690
|
+
}
|
|
691
|
+
if (/^(?:PORT|.*_PORT|.*_TIMEOUT|.*_MS|.*_SECONDS)$/i.test(key)) {
|
|
692
|
+
return "z.coerce.number()";
|
|
693
|
+
}
|
|
694
|
+
if (/^(?:ENABLE|DISABLE|IS|HAS|USE)_|_(?:ENABLED|DISABLED)$/i.test(key)) {
|
|
695
|
+
return "z.coerce.boolean()";
|
|
696
|
+
}
|
|
697
|
+
return "z.string().min(1)";
|
|
698
|
+
}
|
|
699
|
+
function generateZodSchema(result) {
|
|
700
|
+
const keys = /* @__PURE__ */ new Set();
|
|
701
|
+
let hasProcessOrServer = false;
|
|
702
|
+
let hasImportMeta = false;
|
|
703
|
+
for (const [key, refs] of Object.entries(result.usedInCode)) {
|
|
704
|
+
if (BUILTIN_ENV_VARS.has(key)) continue;
|
|
705
|
+
keys.add(key);
|
|
706
|
+
for (const ref of refs) {
|
|
707
|
+
if (ref.source === "import.meta.env") {
|
|
708
|
+
hasImportMeta = true;
|
|
709
|
+
} else {
|
|
710
|
+
hasProcessOrServer = true;
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
for (const key of result.unusedInExample) {
|
|
715
|
+
if (!BUILTIN_ENV_VARS.has(key)) {
|
|
716
|
+
keys.add(key);
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
const sortedKeys = Array.from(keys).sort();
|
|
720
|
+
const sourceExpr = hasImportMeta && !hasProcessOrServer ? "import.meta.env" : "process.env";
|
|
721
|
+
const lines = [
|
|
722
|
+
"// Auto-generated by env-drift. Customize validators as needed.",
|
|
723
|
+
'import { z } from "zod";',
|
|
724
|
+
"",
|
|
725
|
+
"export const envSchema = z.object({"
|
|
726
|
+
];
|
|
727
|
+
for (const key of sortedKeys) {
|
|
728
|
+
lines.push(` ${key}: ${inferZodRule(key)},`);
|
|
729
|
+
}
|
|
730
|
+
lines.push("});");
|
|
731
|
+
lines.push("");
|
|
732
|
+
lines.push("export type Env = z.infer<typeof envSchema>;");
|
|
733
|
+
lines.push("");
|
|
734
|
+
lines.push(`export const env = envSchema.parse(${sourceExpr});`);
|
|
735
|
+
lines.push("");
|
|
736
|
+
return lines.join("\n");
|
|
737
|
+
}
|
|
738
|
+
function writeZodSchema(outputPath, result) {
|
|
739
|
+
const content = generateZodSchema(result);
|
|
740
|
+
mkdirSync(dirname(outputPath), { recursive: true });
|
|
741
|
+
writeFileSync2(outputPath, content, "utf-8");
|
|
742
|
+
const uniqueKeys = /* @__PURE__ */ new Set([
|
|
743
|
+
...Object.keys(result.usedInCode).filter((k) => !BUILTIN_ENV_VARS.has(k)),
|
|
744
|
+
...result.unusedInExample.filter((k) => !BUILTIN_ENV_VARS.has(k))
|
|
745
|
+
]);
|
|
746
|
+
return { outputPath, count: uniqueKeys.size };
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
// src/typegen.ts
|
|
750
|
+
import { mkdirSync as mkdirSync2, writeFileSync as writeFileSync3 } from "fs";
|
|
751
|
+
import { dirname as dirname2 } from "path";
|
|
530
752
|
function generateEnvTypes(result) {
|
|
531
753
|
const processKeys = /* @__PURE__ */ new Set();
|
|
532
754
|
const importMetaKeys = /* @__PURE__ */ new Set();
|
|
@@ -581,8 +803,8 @@ function generateEnvTypes(result) {
|
|
|
581
803
|
}
|
|
582
804
|
function writeEnvTypes(outputPath, result) {
|
|
583
805
|
const content = generateEnvTypes(result);
|
|
584
|
-
|
|
585
|
-
|
|
806
|
+
mkdirSync2(dirname2(outputPath), { recursive: true });
|
|
807
|
+
writeFileSync3(outputPath, content, "utf-8");
|
|
586
808
|
const uniqueKeys = /* @__PURE__ */ new Set([
|
|
587
809
|
...Object.keys(result.usedInCode).filter((k) => !BUILTIN_ENV_VARS.has(k)),
|
|
588
810
|
...result.unusedInExample.filter((k) => !BUILTIN_ENV_VARS.has(k))
|
|
@@ -591,6 +813,7 @@ function writeEnvTypes(outputPath, result) {
|
|
|
591
813
|
}
|
|
592
814
|
|
|
593
815
|
// src/reporter.ts
|
|
816
|
+
import { relative as relative2 } from "path";
|
|
594
817
|
import pc from "picocolors";
|
|
595
818
|
function formatRefs(refs) {
|
|
596
819
|
if (!refs || refs.length === 0) return "";
|
|
@@ -600,7 +823,12 @@ function formatRefs(refs) {
|
|
|
600
823
|
}
|
|
601
824
|
function hasBlockingIssues(result, options = {}) {
|
|
602
825
|
const checkMissingInEnv = options.requireEnvFile ? true : result.envFileExists;
|
|
603
|
-
|
|
826
|
+
const baseBlocking = checkMissingInEnv && result.missingInEnv.length > 0 || result.missingInExample.length > 0 || result.secretExposures.length > 0 || result.exampleSecretLeaks.length > 0;
|
|
827
|
+
if (baseBlocking) return true;
|
|
828
|
+
if (options.strict) {
|
|
829
|
+
return result.emptyInEnv.length > 0 || result.unusedInEnv.length > 0 || result.unusedInExample.length > 0 || result.duplicateKeys.env.length > 0 || result.duplicateKeys.example.length > 0;
|
|
830
|
+
}
|
|
831
|
+
return false;
|
|
604
832
|
}
|
|
605
833
|
function formatAuditReport(result, options = {}) {
|
|
606
834
|
const envFile = options.envFile ?? ".env";
|
|
@@ -828,6 +1056,253 @@ function formatGitHubAnnotations(result, options = {}) {
|
|
|
828
1056
|
}
|
|
829
1057
|
return lines.join("\n");
|
|
830
1058
|
}
|
|
1059
|
+
function formatDiffReport(diff) {
|
|
1060
|
+
const lines = [];
|
|
1061
|
+
lines.push("");
|
|
1062
|
+
lines.push(
|
|
1063
|
+
`${pc.bold(pc.cyan("env-drift diff"))} ${pc.dim(`\u2014 ${diff.fileA} \u2194 ${diff.fileB}`)}`
|
|
1064
|
+
);
|
|
1065
|
+
lines.push("");
|
|
1066
|
+
if (!diff.fileAExists) {
|
|
1067
|
+
lines.push(pc.red(`\u2716 File not found: ${diff.fileA}`));
|
|
1068
|
+
}
|
|
1069
|
+
if (!diff.fileBExists) {
|
|
1070
|
+
lines.push(pc.red(`\u2716 File not found: ${diff.fileB}`));
|
|
1071
|
+
}
|
|
1072
|
+
if (!diff.fileAExists || !diff.fileBExists) {
|
|
1073
|
+
lines.push("");
|
|
1074
|
+
return lines.join("\n");
|
|
1075
|
+
}
|
|
1076
|
+
const totalIssues = diff.onlyInA.length + diff.onlyInB.length + diff.emptyInA.length + diff.emptyInB.length;
|
|
1077
|
+
if (totalIssues === 0) {
|
|
1078
|
+
lines.push(
|
|
1079
|
+
pc.green(
|
|
1080
|
+
`\u2714 No drift between ${diff.fileA} and ${diff.fileB} (${diff.inBoth.length} shared key${diff.inBoth.length === 1 ? "" : "s"}).`
|
|
1081
|
+
)
|
|
1082
|
+
);
|
|
1083
|
+
lines.push("");
|
|
1084
|
+
return lines.join("\n");
|
|
1085
|
+
}
|
|
1086
|
+
if (diff.onlyInA.length > 0) {
|
|
1087
|
+
lines.push(
|
|
1088
|
+
pc.bold(pc.red(`\u2716 Only in ${diff.fileA} (missing in ${diff.fileB}) (${diff.onlyInA.length})`))
|
|
1089
|
+
);
|
|
1090
|
+
for (const key of diff.onlyInA) {
|
|
1091
|
+
lines.push(` ${pc.red("\u2022")} ${key}`);
|
|
1092
|
+
}
|
|
1093
|
+
lines.push("");
|
|
1094
|
+
}
|
|
1095
|
+
if (diff.onlyInB.length > 0) {
|
|
1096
|
+
lines.push(
|
|
1097
|
+
pc.bold(pc.red(`\u2716 Only in ${diff.fileB} (missing in ${diff.fileA}) (${diff.onlyInB.length})`))
|
|
1098
|
+
);
|
|
1099
|
+
for (const key of diff.onlyInB) {
|
|
1100
|
+
lines.push(` ${pc.red("\u2022")} ${key}`);
|
|
1101
|
+
}
|
|
1102
|
+
lines.push("");
|
|
1103
|
+
}
|
|
1104
|
+
if (diff.emptyInA.length > 0) {
|
|
1105
|
+
lines.push(pc.bold(pc.yellow(`\u26A0 Empty in ${diff.fileA} (${diff.emptyInA.length})`)));
|
|
1106
|
+
for (const key of diff.emptyInA) {
|
|
1107
|
+
lines.push(` ${pc.yellow("\u2022")} ${key}`);
|
|
1108
|
+
}
|
|
1109
|
+
lines.push("");
|
|
1110
|
+
}
|
|
1111
|
+
if (diff.emptyInB.length > 0) {
|
|
1112
|
+
lines.push(pc.bold(pc.yellow(`\u26A0 Empty in ${diff.fileB} (${diff.emptyInB.length})`)));
|
|
1113
|
+
for (const key of diff.emptyInB) {
|
|
1114
|
+
lines.push(` ${pc.yellow("\u2022")} ${key}`);
|
|
1115
|
+
}
|
|
1116
|
+
lines.push("");
|
|
1117
|
+
}
|
|
1118
|
+
lines.push(pc.dim(`Shared keys in both files: ${diff.inBoth.length}`));
|
|
1119
|
+
lines.push("");
|
|
1120
|
+
return lines.join("\n");
|
|
1121
|
+
}
|
|
1122
|
+
function formatWorkspaceReport(wsResult, options = {}) {
|
|
1123
|
+
const lines = [];
|
|
1124
|
+
lines.push("");
|
|
1125
|
+
lines.push(
|
|
1126
|
+
`${pc.bold(pc.cyan("env-drift workspaces"))} ${pc.dim(
|
|
1127
|
+
`\u2014 audited ${wsResult.workspaces.length} workspace${wsResult.workspaces.length === 1 ? "" : "s"}`
|
|
1128
|
+
)}`
|
|
1129
|
+
);
|
|
1130
|
+
if (wsResult.workspaces.length === 0) {
|
|
1131
|
+
lines.push("");
|
|
1132
|
+
lines.push(pc.yellow("\u26A0 No workspaces found in package.json or pnpm-workspace.yaml."));
|
|
1133
|
+
lines.push("");
|
|
1134
|
+
return lines.join("\n");
|
|
1135
|
+
}
|
|
1136
|
+
for (const ws of wsResult.workspaces) {
|
|
1137
|
+
const rel = relative2(wsResult.rootDir, ws.path).replace(/\\/g, "/");
|
|
1138
|
+
lines.push("");
|
|
1139
|
+
lines.push(pc.bold(pc.white(`\u25B8 ${ws.name}`)) + pc.dim(` (${rel})`));
|
|
1140
|
+
lines.push(formatAuditReport(ws.result, options).trimEnd());
|
|
1141
|
+
}
|
|
1142
|
+
lines.push("");
|
|
1143
|
+
return lines.join("\n");
|
|
1144
|
+
}
|
|
1145
|
+
|
|
1146
|
+
// src/workspaces.ts
|
|
1147
|
+
import { existsSync as existsSync3, readdirSync as readdirSync2, readFileSync as readFileSync4, statSync as statSync2 } from "fs";
|
|
1148
|
+
import { join as join2, relative as relative3, resolve as resolve4 } from "path";
|
|
1149
|
+
function readWorkspacePatterns(rootDir) {
|
|
1150
|
+
const pkgPath = resolve4(rootDir, "package.json");
|
|
1151
|
+
if (existsSync3(pkgPath)) {
|
|
1152
|
+
try {
|
|
1153
|
+
const parsed = JSON.parse(readFileSync4(pkgPath, "utf-8"));
|
|
1154
|
+
if (Array.isArray(parsed.workspaces)) {
|
|
1155
|
+
return parsed.workspaces.filter((p) => typeof p === "string");
|
|
1156
|
+
}
|
|
1157
|
+
if (parsed.workspaces && typeof parsed.workspaces === "object" && Array.isArray(parsed.workspaces.packages)) {
|
|
1158
|
+
return parsed.workspaces.packages.filter((p) => typeof p === "string");
|
|
1159
|
+
}
|
|
1160
|
+
} catch {
|
|
1161
|
+
}
|
|
1162
|
+
}
|
|
1163
|
+
const pnpmPath = resolve4(rootDir, "pnpm-workspace.yaml");
|
|
1164
|
+
if (existsSync3(pnpmPath)) {
|
|
1165
|
+
const content = readFileSync4(pnpmPath, "utf-8");
|
|
1166
|
+
const patterns = [];
|
|
1167
|
+
let inPackages = false;
|
|
1168
|
+
for (const rawLine of content.split(/\r?\n/)) {
|
|
1169
|
+
const line = rawLine.trim();
|
|
1170
|
+
if (!line || line.startsWith("#")) continue;
|
|
1171
|
+
if (/^packages\s*:/.test(line)) {
|
|
1172
|
+
inPackages = true;
|
|
1173
|
+
continue;
|
|
1174
|
+
}
|
|
1175
|
+
if (inPackages) {
|
|
1176
|
+
if (line.startsWith("-")) {
|
|
1177
|
+
const item = line.slice(1).trim().replace(/^["']|["']$/g, "");
|
|
1178
|
+
if (item) patterns.push(item);
|
|
1179
|
+
} else if (/^[A-Za-z0-9_-]+\s*:/.test(line)) {
|
|
1180
|
+
inPackages = false;
|
|
1181
|
+
}
|
|
1182
|
+
}
|
|
1183
|
+
}
|
|
1184
|
+
if (patterns.length > 0) return patterns;
|
|
1185
|
+
}
|
|
1186
|
+
return [];
|
|
1187
|
+
}
|
|
1188
|
+
function expandPattern(rootDir, pattern) {
|
|
1189
|
+
const cleaned = pattern.replace(/\\/g, "/").replace(/\/+$/, "");
|
|
1190
|
+
const segments = cleaned.split("/").filter(Boolean);
|
|
1191
|
+
let currentDirs = [rootDir];
|
|
1192
|
+
for (const seg of segments) {
|
|
1193
|
+
const nextDirs = [];
|
|
1194
|
+
if (seg === "*" || seg === "**") {
|
|
1195
|
+
for (const dir of currentDirs) {
|
|
1196
|
+
if (!existsSync3(dir)) continue;
|
|
1197
|
+
for (const entry of readdirSync2(dir, { withFileTypes: true })) {
|
|
1198
|
+
if (entry.isDirectory() && !entry.name.startsWith(".") && entry.name !== "node_modules") {
|
|
1199
|
+
nextDirs.push(join2(dir, entry.name));
|
|
1200
|
+
}
|
|
1201
|
+
}
|
|
1202
|
+
}
|
|
1203
|
+
} else if (seg.includes("*")) {
|
|
1204
|
+
const regex = new RegExp(
|
|
1205
|
+
"^" + seg.replace(/[.+?^${}()|[\]\\]/g, "\\$&").replace(/\*/g, ".*") + "$"
|
|
1206
|
+
);
|
|
1207
|
+
for (const dir of currentDirs) {
|
|
1208
|
+
if (!existsSync3(dir)) continue;
|
|
1209
|
+
for (const entry of readdirSync2(dir, { withFileTypes: true })) {
|
|
1210
|
+
if (entry.isDirectory() && regex.test(entry.name)) {
|
|
1211
|
+
nextDirs.push(join2(dir, entry.name));
|
|
1212
|
+
}
|
|
1213
|
+
}
|
|
1214
|
+
}
|
|
1215
|
+
} else {
|
|
1216
|
+
for (const dir of currentDirs) {
|
|
1217
|
+
const candidate = join2(dir, seg);
|
|
1218
|
+
if (existsSync3(candidate) && statSync2(candidate).isDirectory()) {
|
|
1219
|
+
nextDirs.push(candidate);
|
|
1220
|
+
}
|
|
1221
|
+
}
|
|
1222
|
+
}
|
|
1223
|
+
currentDirs = nextDirs;
|
|
1224
|
+
}
|
|
1225
|
+
return currentDirs;
|
|
1226
|
+
}
|
|
1227
|
+
function discoverWorkspaces(rootDir) {
|
|
1228
|
+
const resolvedRoot = resolve4(rootDir);
|
|
1229
|
+
const patterns = readWorkspacePatterns(resolvedRoot);
|
|
1230
|
+
if (patterns.length === 0) return [];
|
|
1231
|
+
const included = /* @__PURE__ */ new Set();
|
|
1232
|
+
const excluded = /* @__PURE__ */ new Set();
|
|
1233
|
+
for (const pattern of patterns) {
|
|
1234
|
+
if (pattern.startsWith("!")) {
|
|
1235
|
+
for (const dir of expandPattern(resolvedRoot, pattern.slice(1))) {
|
|
1236
|
+
excluded.add(resolve4(dir));
|
|
1237
|
+
}
|
|
1238
|
+
} else {
|
|
1239
|
+
for (const dir of expandPattern(resolvedRoot, pattern)) {
|
|
1240
|
+
included.add(resolve4(dir));
|
|
1241
|
+
}
|
|
1242
|
+
}
|
|
1243
|
+
}
|
|
1244
|
+
const results = [];
|
|
1245
|
+
for (const dir of included) {
|
|
1246
|
+
if (excluded.has(dir)) continue;
|
|
1247
|
+
const relPath = relative3(resolvedRoot, dir).replace(/\\/g, "/");
|
|
1248
|
+
let name = relPath;
|
|
1249
|
+
const pkgPath = join2(dir, "package.json");
|
|
1250
|
+
if (existsSync3(pkgPath)) {
|
|
1251
|
+
try {
|
|
1252
|
+
const pkg = JSON.parse(readFileSync4(pkgPath, "utf-8"));
|
|
1253
|
+
if (typeof pkg.name === "string" && pkg.name.trim()) {
|
|
1254
|
+
name = pkg.name.trim();
|
|
1255
|
+
}
|
|
1256
|
+
} catch {
|
|
1257
|
+
}
|
|
1258
|
+
}
|
|
1259
|
+
results.push({ name, path: dir });
|
|
1260
|
+
}
|
|
1261
|
+
results.sort((a, b) => a.path.localeCompare(b.path));
|
|
1262
|
+
return results;
|
|
1263
|
+
}
|
|
1264
|
+
function auditWorkspaces(rootDir, options = {}) {
|
|
1265
|
+
const resolvedRoot = resolve4(rootDir);
|
|
1266
|
+
const discovered = discoverWorkspaces(resolvedRoot);
|
|
1267
|
+
const workspaces = [];
|
|
1268
|
+
for (const ws of discovered) {
|
|
1269
|
+
const wsConfig = loadConfig(ws.path);
|
|
1270
|
+
const mergedIgnore = [
|
|
1271
|
+
...options.ignore ?? [],
|
|
1272
|
+
...wsConfig.ignore ?? []
|
|
1273
|
+
];
|
|
1274
|
+
const mergedIgnoreDirs = [
|
|
1275
|
+
...options.ignoreDirs ?? [],
|
|
1276
|
+
...wsConfig.ignoreDirs ?? []
|
|
1277
|
+
];
|
|
1278
|
+
const mergedPrefixes = [
|
|
1279
|
+
...options.publicPrefixes ?? [],
|
|
1280
|
+
...wsConfig.publicPrefixes ?? []
|
|
1281
|
+
];
|
|
1282
|
+
const mergedKeywords = [
|
|
1283
|
+
...options.sensitiveKeywords ?? [],
|
|
1284
|
+
...wsConfig.sensitiveKeywords ?? []
|
|
1285
|
+
];
|
|
1286
|
+
const result = auditEnv({
|
|
1287
|
+
rootDir: ws.path,
|
|
1288
|
+
envFile: options.envFile ?? wsConfig.envFile,
|
|
1289
|
+
exampleFile: options.exampleFile ?? wsConfig.exampleFile,
|
|
1290
|
+
ignore: mergedIgnore.length > 0 ? mergedIgnore : void 0,
|
|
1291
|
+
ignoreDirs: mergedIgnoreDirs.length > 0 ? mergedIgnoreDirs : void 0,
|
|
1292
|
+
publicPrefixes: mergedPrefixes.length > 0 ? mergedPrefixes : void 0,
|
|
1293
|
+
sensitiveKeywords: mergedKeywords.length > 0 ? mergedKeywords : void 0
|
|
1294
|
+
});
|
|
1295
|
+
workspaces.push({
|
|
1296
|
+
name: ws.name,
|
|
1297
|
+
path: ws.path,
|
|
1298
|
+
result
|
|
1299
|
+
});
|
|
1300
|
+
}
|
|
1301
|
+
return {
|
|
1302
|
+
rootDir: resolvedRoot,
|
|
1303
|
+
workspaces
|
|
1304
|
+
};
|
|
1305
|
+
}
|
|
831
1306
|
export {
|
|
832
1307
|
BUILTIN_ENV_VARS,
|
|
833
1308
|
DEFAULT_EXTENSIONS,
|
|
@@ -835,17 +1310,26 @@ export {
|
|
|
835
1310
|
PUBLIC_ENV_PREFIXES,
|
|
836
1311
|
SENSITIVE_KEYWORDS,
|
|
837
1312
|
auditEnv,
|
|
1313
|
+
auditWorkspaces,
|
|
838
1314
|
detectExampleSecretLeak,
|
|
839
1315
|
detectSecretExposure,
|
|
1316
|
+
diffEnvFiles,
|
|
1317
|
+
discoverWorkspaces,
|
|
840
1318
|
extractEnvReferences,
|
|
841
1319
|
formatAuditReport,
|
|
1320
|
+
formatDiffReport,
|
|
842
1321
|
formatGitHubAnnotations,
|
|
1322
|
+
formatWorkspaceReport,
|
|
843
1323
|
generateEnvTypes,
|
|
1324
|
+
generateZodSchema,
|
|
844
1325
|
hasBlockingIssues,
|
|
1326
|
+
hasDiffIssues,
|
|
1327
|
+
inferZodRule,
|
|
845
1328
|
loadConfig,
|
|
846
1329
|
parseEnvContent,
|
|
847
1330
|
parseEnvFile,
|
|
848
1331
|
scanFiles,
|
|
849
1332
|
syncEnvExample,
|
|
850
|
-
writeEnvTypes
|
|
1333
|
+
writeEnvTypes,
|
|
1334
|
+
writeZodSchema
|
|
851
1335
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@subramanyaks/env-drift",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "Zero-config CLI and library to detect environment variable drift, missing keys, dead vars, and accidental public secret exposure.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"publishConfig": {
|