@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/dist/index.cjs CHANGED
@@ -36,19 +36,28 @@ __export(src_exports, {
36
36
  PUBLIC_ENV_PREFIXES: () => PUBLIC_ENV_PREFIXES,
37
37
  SENSITIVE_KEYWORDS: () => SENSITIVE_KEYWORDS,
38
38
  auditEnv: () => auditEnv,
39
+ auditWorkspaces: () => auditWorkspaces,
39
40
  detectExampleSecretLeak: () => detectExampleSecretLeak,
40
41
  detectSecretExposure: () => detectSecretExposure,
42
+ diffEnvFiles: () => diffEnvFiles,
43
+ discoverWorkspaces: () => discoverWorkspaces,
41
44
  extractEnvReferences: () => extractEnvReferences,
42
45
  formatAuditReport: () => formatAuditReport,
46
+ formatDiffReport: () => formatDiffReport,
43
47
  formatGitHubAnnotations: () => formatGitHubAnnotations,
48
+ formatWorkspaceReport: () => formatWorkspaceReport,
44
49
  generateEnvTypes: () => generateEnvTypes,
50
+ generateZodSchema: () => generateZodSchema,
45
51
  hasBlockingIssues: () => hasBlockingIssues,
52
+ hasDiffIssues: () => hasDiffIssues,
53
+ inferZodRule: () => inferZodRule,
46
54
  loadConfig: () => loadConfig,
47
55
  parseEnvContent: () => parseEnvContent,
48
56
  parseEnvFile: () => parseEnvFile,
49
57
  scanFiles: () => scanFiles,
50
58
  syncEnvExample: () => syncEnvExample,
51
- writeEnvTypes: () => writeEnvTypes
59
+ writeEnvTypes: () => writeEnvTypes,
60
+ writeZodSchema: () => writeZodSchema
52
61
  });
53
62
  module.exports = __toCommonJS(src_exports);
54
63
 
@@ -58,28 +67,31 @@ var import_node_path2 = require("path");
58
67
  // src/envParser.ts
59
68
  var import_node_fs = require("fs");
60
69
  var ENV_LINE_REGEX = /^(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.*)$/;
70
+ function parseDoubleQuotedValue(raw) {
71
+ let result = "";
72
+ for (let i = 1; i < raw.length; i++) {
73
+ const ch = raw[i];
74
+ if (ch === "\\" && i + 1 < raw.length) {
75
+ const next = raw[i + 1];
76
+ if (next === "n") result += "\n";
77
+ else if (next === "r") result += "\r";
78
+ else if (next === "t") result += " ";
79
+ else result += next;
80
+ i++;
81
+ } else if (ch === '"') {
82
+ return { value: result, closed: true };
83
+ } else {
84
+ result += ch;
85
+ }
86
+ }
87
+ return { value: result, closed: false };
88
+ }
61
89
  function parseValue(raw) {
62
90
  const trimmed = raw.trim();
63
91
  if (!trimmed) return "";
64
92
  const quote = trimmed[0];
65
93
  if (quote === '"') {
66
- let result = "";
67
- for (let i = 1; i < trimmed.length; i++) {
68
- const ch = trimmed[i];
69
- if (ch === "\\" && i + 1 < trimmed.length) {
70
- const next = trimmed[i + 1];
71
- if (next === "n") result += "\n";
72
- else if (next === "r") result += "\r";
73
- else if (next === "t") result += " ";
74
- else result += next;
75
- i++;
76
- } else if (ch === '"') {
77
- return result;
78
- } else {
79
- result += ch;
80
- }
81
- }
82
- return result;
94
+ return parseDoubleQuotedValue(trimmed).value;
83
95
  }
84
96
  if (quote === "'") {
85
97
  const closingIdx = trimmed.indexOf("'", 1);
@@ -110,14 +122,28 @@ function parseEnvContent(content) {
110
122
  const key = match[1];
111
123
  const rawValue = match[2] ?? "";
112
124
  if (!key) continue;
125
+ const startLine = i + 1;
113
126
  if (seen.has(key)) {
114
127
  duplicateSet.add(key);
115
128
  } else {
116
129
  seen.add(key);
117
130
  keys.push(key);
118
131
  }
119
- values[key] = parseValue(rawValue);
120
- keyLines[key] = i + 1;
132
+ const trimmedVal = rawValue.trimStart();
133
+ if (trimmedVal.startsWith('"')) {
134
+ let combined = trimmedVal;
135
+ let parsed = parseDoubleQuotedValue(combined);
136
+ while (!parsed.closed && i + 1 < lines.length) {
137
+ i++;
138
+ combined += `
139
+ ${lines[i]}`;
140
+ parsed = parseDoubleQuotedValue(combined);
141
+ }
142
+ values[key] = parsed.value;
143
+ } else {
144
+ values[key] = parseValue(rawValue);
145
+ }
146
+ keyLines[key] = startLine;
121
147
  }
122
148
  return {
123
149
  keys,
@@ -202,12 +228,109 @@ var DEFAULT_EXTENSIONS = /* @__PURE__ */ new Set([
202
228
  ".svelte",
203
229
  ".astro"
204
230
  ]);
231
+ var REGEX_PRECEDING_KEYWORDS = /* @__PURE__ */ new Set([
232
+ "return",
233
+ "case",
234
+ "throw",
235
+ "in",
236
+ "of",
237
+ "typeof",
238
+ "void",
239
+ "delete",
240
+ "await",
241
+ "yield",
242
+ "else",
243
+ "do"
244
+ ]);
205
245
  function stripCommentsPreservingLines(source) {
206
- const tokenRegex = /("(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|`(?:\\.|[^`\\])*`)|\/\*[\s\S]*?\*\/|\/\/[^\r\n]*/g;
207
- return source.replace(tokenRegex, (match, stringLiteral) => {
208
- if (stringLiteral) return match;
209
- return match.replace(/[^\r\n]/g, " ");
210
- });
246
+ const out = [];
247
+ const len = source.length;
248
+ let i = 0;
249
+ let prevSignificant = "";
250
+ let prevWord = "";
251
+ while (i < len) {
252
+ const ch = source[i];
253
+ const next = i + 1 < len ? source[i + 1] : "";
254
+ if (ch === '"' || ch === "'" || ch === "`") {
255
+ const quote = ch;
256
+ out.push(ch);
257
+ i++;
258
+ while (i < len) {
259
+ const c = source[i];
260
+ out.push(c);
261
+ i++;
262
+ if (c === "\\" && i < len) {
263
+ out.push(source[i]);
264
+ i++;
265
+ } else if (c === quote) {
266
+ break;
267
+ }
268
+ }
269
+ prevSignificant = quote;
270
+ prevWord = "";
271
+ continue;
272
+ }
273
+ if (ch === "/" && next === "/") {
274
+ out.push(" ", " ");
275
+ i += 2;
276
+ while (i < len && source[i] !== "\n" && source[i] !== "\r") {
277
+ out.push(" ");
278
+ i++;
279
+ }
280
+ continue;
281
+ }
282
+ if (ch === "/" && next === "*") {
283
+ out.push(" ", " ");
284
+ i += 2;
285
+ while (i < len) {
286
+ if (source[i] === "*" && i + 1 < len && source[i + 1] === "/") {
287
+ out.push(" ", " ");
288
+ i += 2;
289
+ break;
290
+ }
291
+ const c = source[i];
292
+ out.push(c === "\n" || c === "\r" ? c : " ");
293
+ i++;
294
+ }
295
+ continue;
296
+ }
297
+ if (ch === "/" && (prevSignificant === "" || "=([{,;:!&|?+-*%^~<>".includes(prevSignificant) || REGEX_PRECEDING_KEYWORDS.has(prevWord))) {
298
+ out.push(ch);
299
+ i++;
300
+ let inCharClass = false;
301
+ while (i < len) {
302
+ const c = source[i];
303
+ out.push(c);
304
+ i++;
305
+ if (c === "\\" && i < len) {
306
+ out.push(source[i]);
307
+ i++;
308
+ } else if (c === "[") {
309
+ inCharClass = true;
310
+ } else if (c === "]") {
311
+ inCharClass = false;
312
+ } else if (c === "/" && !inCharClass) {
313
+ break;
314
+ } else if (c === "\n" || c === "\r") {
315
+ break;
316
+ }
317
+ }
318
+ prevSignificant = "/";
319
+ prevWord = "";
320
+ continue;
321
+ }
322
+ out.push(ch);
323
+ if (!/\s/.test(ch)) {
324
+ if (/[A-Za-z0-9_$]/.test(ch)) {
325
+ prevWord = /[A-Za-z0-9_$]/.test(prevSignificant) ? prevWord + ch : ch;
326
+ } else {
327
+ prevWord = "";
328
+ }
329
+ prevSignificant = ch;
330
+ }
331
+ i++;
332
+ }
333
+ return out.join("");
211
334
  }
212
335
  function getLineNumber(source, index) {
213
336
  let line = 1;
@@ -552,6 +675,9 @@ function normalizeConfig(raw) {
552
675
  if (publicPrefixes) config.publicPrefixes = publicPrefixes;
553
676
  const sensitiveKeywords = toStringArray(obj.sensitiveKeywords);
554
677
  if (sensitiveKeywords) config.sensitiveKeywords = sensitiveKeywords;
678
+ if (typeof obj.strict === "boolean") {
679
+ config.strict = obj.strict;
680
+ }
555
681
  return config;
556
682
  }
557
683
  function loadConfig(rootDir) {
@@ -578,9 +704,114 @@ function loadConfig(rootDir) {
578
704
  return {};
579
705
  }
580
706
 
581
- // src/typegen.ts
582
- var import_node_fs4 = require("fs");
707
+ // src/diff.ts
583
708
  var import_node_path4 = require("path");
709
+ function diffEnvFiles(fileA, fileB, options = {}) {
710
+ const rootDir = (0, import_node_path4.resolve)(options.rootDir ?? process.cwd());
711
+ const ignoreSet = /* @__PURE__ */ new Set([...BUILTIN_ENV_VARS, ...options.ignore ?? []]);
712
+ const parsedA = parseEnvFile((0, import_node_path4.resolve)(rootDir, fileA));
713
+ const parsedB = parseEnvFile((0, import_node_path4.resolve)(rootDir, fileB));
714
+ const keysA = parsedA.keys.filter((k) => !ignoreSet.has(k));
715
+ const keysB = parsedB.keys.filter((k) => !ignoreSet.has(k));
716
+ const setA = new Set(keysA);
717
+ const setB = new Set(keysB);
718
+ const onlyInA = keysA.filter((k) => !setB.has(k)).sort();
719
+ const onlyInB = keysB.filter((k) => !setA.has(k)).sort();
720
+ const inBoth = keysA.filter((k) => setB.has(k)).sort();
721
+ const emptyInA = keysA.filter((k) => (parsedA.values[k] ?? "").trim() === "").sort();
722
+ const emptyInB = keysB.filter((k) => (parsedB.values[k] ?? "").trim() === "").sort();
723
+ return {
724
+ fileA,
725
+ fileB,
726
+ fileAExists: parsedA.exists,
727
+ fileBExists: parsedB.exists,
728
+ onlyInA,
729
+ onlyInB,
730
+ inBoth,
731
+ emptyInA,
732
+ emptyInB
733
+ };
734
+ }
735
+ function hasDiffIssues(diff, options = {}) {
736
+ if (!diff.fileAExists || !diff.fileBExists) return true;
737
+ if (diff.onlyInA.length > 0 || diff.onlyInB.length > 0) return true;
738
+ if (options.strict && (diff.emptyInA.length > 0 || diff.emptyInB.length > 0)) {
739
+ return true;
740
+ }
741
+ return false;
742
+ }
743
+
744
+ // src/schemagen.ts
745
+ var import_node_fs4 = require("fs");
746
+ var import_node_path5 = require("path");
747
+ function inferZodRule(key) {
748
+ if (key === "NODE_ENV") {
749
+ return 'z.enum(["development", "production", "test"]).default("development")';
750
+ }
751
+ if (/(?:^|_)(?:URL|URI|ENDPOINT)$/i.test(key)) {
752
+ return "z.string().url()";
753
+ }
754
+ if (/^(?:PORT|.*_PORT|.*_TIMEOUT|.*_MS|.*_SECONDS)$/i.test(key)) {
755
+ return "z.coerce.number()";
756
+ }
757
+ if (/^(?:ENABLE|DISABLE|IS|HAS|USE)_|_(?:ENABLED|DISABLED)$/i.test(key)) {
758
+ return "z.coerce.boolean()";
759
+ }
760
+ return "z.string().min(1)";
761
+ }
762
+ function generateZodSchema(result) {
763
+ const keys = /* @__PURE__ */ new Set();
764
+ let hasProcessOrServer = false;
765
+ let hasImportMeta = false;
766
+ for (const [key, refs] of Object.entries(result.usedInCode)) {
767
+ if (BUILTIN_ENV_VARS.has(key)) continue;
768
+ keys.add(key);
769
+ for (const ref of refs) {
770
+ if (ref.source === "import.meta.env") {
771
+ hasImportMeta = true;
772
+ } else {
773
+ hasProcessOrServer = true;
774
+ }
775
+ }
776
+ }
777
+ for (const key of result.unusedInExample) {
778
+ if (!BUILTIN_ENV_VARS.has(key)) {
779
+ keys.add(key);
780
+ }
781
+ }
782
+ const sortedKeys = Array.from(keys).sort();
783
+ const sourceExpr = hasImportMeta && !hasProcessOrServer ? "import.meta.env" : "process.env";
784
+ const lines = [
785
+ "// Auto-generated by env-drift. Customize validators as needed.",
786
+ 'import { z } from "zod";',
787
+ "",
788
+ "export const envSchema = z.object({"
789
+ ];
790
+ for (const key of sortedKeys) {
791
+ lines.push(` ${key}: ${inferZodRule(key)},`);
792
+ }
793
+ lines.push("});");
794
+ lines.push("");
795
+ lines.push("export type Env = z.infer<typeof envSchema>;");
796
+ lines.push("");
797
+ lines.push(`export const env = envSchema.parse(${sourceExpr});`);
798
+ lines.push("");
799
+ return lines.join("\n");
800
+ }
801
+ function writeZodSchema(outputPath, result) {
802
+ const content = generateZodSchema(result);
803
+ (0, import_node_fs4.mkdirSync)((0, import_node_path5.dirname)(outputPath), { recursive: true });
804
+ (0, import_node_fs4.writeFileSync)(outputPath, content, "utf-8");
805
+ const uniqueKeys = /* @__PURE__ */ new Set([
806
+ ...Object.keys(result.usedInCode).filter((k) => !BUILTIN_ENV_VARS.has(k)),
807
+ ...result.unusedInExample.filter((k) => !BUILTIN_ENV_VARS.has(k))
808
+ ]);
809
+ return { outputPath, count: uniqueKeys.size };
810
+ }
811
+
812
+ // src/typegen.ts
813
+ var import_node_fs5 = require("fs");
814
+ var import_node_path6 = require("path");
584
815
  function generateEnvTypes(result) {
585
816
  const processKeys = /* @__PURE__ */ new Set();
586
817
  const importMetaKeys = /* @__PURE__ */ new Set();
@@ -635,8 +866,8 @@ function generateEnvTypes(result) {
635
866
  }
636
867
  function writeEnvTypes(outputPath, result) {
637
868
  const content = generateEnvTypes(result);
638
- (0, import_node_fs4.mkdirSync)((0, import_node_path4.dirname)(outputPath), { recursive: true });
639
- (0, import_node_fs4.writeFileSync)(outputPath, content, "utf-8");
869
+ (0, import_node_fs5.mkdirSync)((0, import_node_path6.dirname)(outputPath), { recursive: true });
870
+ (0, import_node_fs5.writeFileSync)(outputPath, content, "utf-8");
640
871
  const uniqueKeys = /* @__PURE__ */ new Set([
641
872
  ...Object.keys(result.usedInCode).filter((k) => !BUILTIN_ENV_VARS.has(k)),
642
873
  ...result.unusedInExample.filter((k) => !BUILTIN_ENV_VARS.has(k))
@@ -645,6 +876,7 @@ function writeEnvTypes(outputPath, result) {
645
876
  }
646
877
 
647
878
  // src/reporter.ts
879
+ var import_node_path7 = require("path");
648
880
  var import_picocolors = __toESM(require("picocolors"), 1);
649
881
  function formatRefs(refs) {
650
882
  if (!refs || refs.length === 0) return "";
@@ -654,7 +886,12 @@ function formatRefs(refs) {
654
886
  }
655
887
  function hasBlockingIssues(result, options = {}) {
656
888
  const checkMissingInEnv = options.requireEnvFile ? true : result.envFileExists;
657
- return checkMissingInEnv && result.missingInEnv.length > 0 || result.missingInExample.length > 0 || result.secretExposures.length > 0 || result.exampleSecretLeaks.length > 0;
889
+ const baseBlocking = checkMissingInEnv && result.missingInEnv.length > 0 || result.missingInExample.length > 0 || result.secretExposures.length > 0 || result.exampleSecretLeaks.length > 0;
890
+ if (baseBlocking) return true;
891
+ if (options.strict) {
892
+ return result.emptyInEnv.length > 0 || result.unusedInEnv.length > 0 || result.unusedInExample.length > 0 || result.duplicateKeys.env.length > 0 || result.duplicateKeys.example.length > 0;
893
+ }
894
+ return false;
658
895
  }
659
896
  function formatAuditReport(result, options = {}) {
660
897
  const envFile = options.envFile ?? ".env";
@@ -882,6 +1119,253 @@ function formatGitHubAnnotations(result, options = {}) {
882
1119
  }
883
1120
  return lines.join("\n");
884
1121
  }
1122
+ function formatDiffReport(diff) {
1123
+ const lines = [];
1124
+ lines.push("");
1125
+ lines.push(
1126
+ `${import_picocolors.default.bold(import_picocolors.default.cyan("env-drift diff"))} ${import_picocolors.default.dim(`\u2014 ${diff.fileA} \u2194 ${diff.fileB}`)}`
1127
+ );
1128
+ lines.push("");
1129
+ if (!diff.fileAExists) {
1130
+ lines.push(import_picocolors.default.red(`\u2716 File not found: ${diff.fileA}`));
1131
+ }
1132
+ if (!diff.fileBExists) {
1133
+ lines.push(import_picocolors.default.red(`\u2716 File not found: ${diff.fileB}`));
1134
+ }
1135
+ if (!diff.fileAExists || !diff.fileBExists) {
1136
+ lines.push("");
1137
+ return lines.join("\n");
1138
+ }
1139
+ const totalIssues = diff.onlyInA.length + diff.onlyInB.length + diff.emptyInA.length + diff.emptyInB.length;
1140
+ if (totalIssues === 0) {
1141
+ lines.push(
1142
+ import_picocolors.default.green(
1143
+ `\u2714 No drift between ${diff.fileA} and ${diff.fileB} (${diff.inBoth.length} shared key${diff.inBoth.length === 1 ? "" : "s"}).`
1144
+ )
1145
+ );
1146
+ lines.push("");
1147
+ return lines.join("\n");
1148
+ }
1149
+ if (diff.onlyInA.length > 0) {
1150
+ lines.push(
1151
+ import_picocolors.default.bold(import_picocolors.default.red(`\u2716 Only in ${diff.fileA} (missing in ${diff.fileB}) (${diff.onlyInA.length})`))
1152
+ );
1153
+ for (const key of diff.onlyInA) {
1154
+ lines.push(` ${import_picocolors.default.red("\u2022")} ${key}`);
1155
+ }
1156
+ lines.push("");
1157
+ }
1158
+ if (diff.onlyInB.length > 0) {
1159
+ lines.push(
1160
+ import_picocolors.default.bold(import_picocolors.default.red(`\u2716 Only in ${diff.fileB} (missing in ${diff.fileA}) (${diff.onlyInB.length})`))
1161
+ );
1162
+ for (const key of diff.onlyInB) {
1163
+ lines.push(` ${import_picocolors.default.red("\u2022")} ${key}`);
1164
+ }
1165
+ lines.push("");
1166
+ }
1167
+ if (diff.emptyInA.length > 0) {
1168
+ lines.push(import_picocolors.default.bold(import_picocolors.default.yellow(`\u26A0 Empty in ${diff.fileA} (${diff.emptyInA.length})`)));
1169
+ for (const key of diff.emptyInA) {
1170
+ lines.push(` ${import_picocolors.default.yellow("\u2022")} ${key}`);
1171
+ }
1172
+ lines.push("");
1173
+ }
1174
+ if (diff.emptyInB.length > 0) {
1175
+ lines.push(import_picocolors.default.bold(import_picocolors.default.yellow(`\u26A0 Empty in ${diff.fileB} (${diff.emptyInB.length})`)));
1176
+ for (const key of diff.emptyInB) {
1177
+ lines.push(` ${import_picocolors.default.yellow("\u2022")} ${key}`);
1178
+ }
1179
+ lines.push("");
1180
+ }
1181
+ lines.push(import_picocolors.default.dim(`Shared keys in both files: ${diff.inBoth.length}`));
1182
+ lines.push("");
1183
+ return lines.join("\n");
1184
+ }
1185
+ function formatWorkspaceReport(wsResult, options = {}) {
1186
+ const lines = [];
1187
+ lines.push("");
1188
+ lines.push(
1189
+ `${import_picocolors.default.bold(import_picocolors.default.cyan("env-drift workspaces"))} ${import_picocolors.default.dim(
1190
+ `\u2014 audited ${wsResult.workspaces.length} workspace${wsResult.workspaces.length === 1 ? "" : "s"}`
1191
+ )}`
1192
+ );
1193
+ if (wsResult.workspaces.length === 0) {
1194
+ lines.push("");
1195
+ lines.push(import_picocolors.default.yellow("\u26A0 No workspaces found in package.json or pnpm-workspace.yaml."));
1196
+ lines.push("");
1197
+ return lines.join("\n");
1198
+ }
1199
+ for (const ws of wsResult.workspaces) {
1200
+ const rel = (0, import_node_path7.relative)(wsResult.rootDir, ws.path).replace(/\\/g, "/");
1201
+ lines.push("");
1202
+ lines.push(import_picocolors.default.bold(import_picocolors.default.white(`\u25B8 ${ws.name}`)) + import_picocolors.default.dim(` (${rel})`));
1203
+ lines.push(formatAuditReport(ws.result, options).trimEnd());
1204
+ }
1205
+ lines.push("");
1206
+ return lines.join("\n");
1207
+ }
1208
+
1209
+ // src/workspaces.ts
1210
+ var import_node_fs6 = require("fs");
1211
+ var import_node_path8 = require("path");
1212
+ function readWorkspacePatterns(rootDir) {
1213
+ const pkgPath = (0, import_node_path8.resolve)(rootDir, "package.json");
1214
+ if ((0, import_node_fs6.existsSync)(pkgPath)) {
1215
+ try {
1216
+ const parsed = JSON.parse((0, import_node_fs6.readFileSync)(pkgPath, "utf-8"));
1217
+ if (Array.isArray(parsed.workspaces)) {
1218
+ return parsed.workspaces.filter((p) => typeof p === "string");
1219
+ }
1220
+ if (parsed.workspaces && typeof parsed.workspaces === "object" && Array.isArray(parsed.workspaces.packages)) {
1221
+ return parsed.workspaces.packages.filter((p) => typeof p === "string");
1222
+ }
1223
+ } catch {
1224
+ }
1225
+ }
1226
+ const pnpmPath = (0, import_node_path8.resolve)(rootDir, "pnpm-workspace.yaml");
1227
+ if ((0, import_node_fs6.existsSync)(pnpmPath)) {
1228
+ const content = (0, import_node_fs6.readFileSync)(pnpmPath, "utf-8");
1229
+ const patterns = [];
1230
+ let inPackages = false;
1231
+ for (const rawLine of content.split(/\r?\n/)) {
1232
+ const line = rawLine.trim();
1233
+ if (!line || line.startsWith("#")) continue;
1234
+ if (/^packages\s*:/.test(line)) {
1235
+ inPackages = true;
1236
+ continue;
1237
+ }
1238
+ if (inPackages) {
1239
+ if (line.startsWith("-")) {
1240
+ const item = line.slice(1).trim().replace(/^["']|["']$/g, "");
1241
+ if (item) patterns.push(item);
1242
+ } else if (/^[A-Za-z0-9_-]+\s*:/.test(line)) {
1243
+ inPackages = false;
1244
+ }
1245
+ }
1246
+ }
1247
+ if (patterns.length > 0) return patterns;
1248
+ }
1249
+ return [];
1250
+ }
1251
+ function expandPattern(rootDir, pattern) {
1252
+ const cleaned = pattern.replace(/\\/g, "/").replace(/\/+$/, "");
1253
+ const segments = cleaned.split("/").filter(Boolean);
1254
+ let currentDirs = [rootDir];
1255
+ for (const seg of segments) {
1256
+ const nextDirs = [];
1257
+ if (seg === "*" || seg === "**") {
1258
+ for (const dir of currentDirs) {
1259
+ if (!(0, import_node_fs6.existsSync)(dir)) continue;
1260
+ for (const entry of (0, import_node_fs6.readdirSync)(dir, { withFileTypes: true })) {
1261
+ if (entry.isDirectory() && !entry.name.startsWith(".") && entry.name !== "node_modules") {
1262
+ nextDirs.push((0, import_node_path8.join)(dir, entry.name));
1263
+ }
1264
+ }
1265
+ }
1266
+ } else if (seg.includes("*")) {
1267
+ const regex = new RegExp(
1268
+ "^" + seg.replace(/[.+?^${}()|[\]\\]/g, "\\$&").replace(/\*/g, ".*") + "$"
1269
+ );
1270
+ for (const dir of currentDirs) {
1271
+ if (!(0, import_node_fs6.existsSync)(dir)) continue;
1272
+ for (const entry of (0, import_node_fs6.readdirSync)(dir, { withFileTypes: true })) {
1273
+ if (entry.isDirectory() && regex.test(entry.name)) {
1274
+ nextDirs.push((0, import_node_path8.join)(dir, entry.name));
1275
+ }
1276
+ }
1277
+ }
1278
+ } else {
1279
+ for (const dir of currentDirs) {
1280
+ const candidate = (0, import_node_path8.join)(dir, seg);
1281
+ if ((0, import_node_fs6.existsSync)(candidate) && (0, import_node_fs6.statSync)(candidate).isDirectory()) {
1282
+ nextDirs.push(candidate);
1283
+ }
1284
+ }
1285
+ }
1286
+ currentDirs = nextDirs;
1287
+ }
1288
+ return currentDirs;
1289
+ }
1290
+ function discoverWorkspaces(rootDir) {
1291
+ const resolvedRoot = (0, import_node_path8.resolve)(rootDir);
1292
+ const patterns = readWorkspacePatterns(resolvedRoot);
1293
+ if (patterns.length === 0) return [];
1294
+ const included = /* @__PURE__ */ new Set();
1295
+ const excluded = /* @__PURE__ */ new Set();
1296
+ for (const pattern of patterns) {
1297
+ if (pattern.startsWith("!")) {
1298
+ for (const dir of expandPattern(resolvedRoot, pattern.slice(1))) {
1299
+ excluded.add((0, import_node_path8.resolve)(dir));
1300
+ }
1301
+ } else {
1302
+ for (const dir of expandPattern(resolvedRoot, pattern)) {
1303
+ included.add((0, import_node_path8.resolve)(dir));
1304
+ }
1305
+ }
1306
+ }
1307
+ const results = [];
1308
+ for (const dir of included) {
1309
+ if (excluded.has(dir)) continue;
1310
+ const relPath = (0, import_node_path8.relative)(resolvedRoot, dir).replace(/\\/g, "/");
1311
+ let name = relPath;
1312
+ const pkgPath = (0, import_node_path8.join)(dir, "package.json");
1313
+ if ((0, import_node_fs6.existsSync)(pkgPath)) {
1314
+ try {
1315
+ const pkg = JSON.parse((0, import_node_fs6.readFileSync)(pkgPath, "utf-8"));
1316
+ if (typeof pkg.name === "string" && pkg.name.trim()) {
1317
+ name = pkg.name.trim();
1318
+ }
1319
+ } catch {
1320
+ }
1321
+ }
1322
+ results.push({ name, path: dir });
1323
+ }
1324
+ results.sort((a, b) => a.path.localeCompare(b.path));
1325
+ return results;
1326
+ }
1327
+ function auditWorkspaces(rootDir, options = {}) {
1328
+ const resolvedRoot = (0, import_node_path8.resolve)(rootDir);
1329
+ const discovered = discoverWorkspaces(resolvedRoot);
1330
+ const workspaces = [];
1331
+ for (const ws of discovered) {
1332
+ const wsConfig = loadConfig(ws.path);
1333
+ const mergedIgnore = [
1334
+ ...options.ignore ?? [],
1335
+ ...wsConfig.ignore ?? []
1336
+ ];
1337
+ const mergedIgnoreDirs = [
1338
+ ...options.ignoreDirs ?? [],
1339
+ ...wsConfig.ignoreDirs ?? []
1340
+ ];
1341
+ const mergedPrefixes = [
1342
+ ...options.publicPrefixes ?? [],
1343
+ ...wsConfig.publicPrefixes ?? []
1344
+ ];
1345
+ const mergedKeywords = [
1346
+ ...options.sensitiveKeywords ?? [],
1347
+ ...wsConfig.sensitiveKeywords ?? []
1348
+ ];
1349
+ const result = auditEnv({
1350
+ rootDir: ws.path,
1351
+ envFile: options.envFile ?? wsConfig.envFile,
1352
+ exampleFile: options.exampleFile ?? wsConfig.exampleFile,
1353
+ ignore: mergedIgnore.length > 0 ? mergedIgnore : void 0,
1354
+ ignoreDirs: mergedIgnoreDirs.length > 0 ? mergedIgnoreDirs : void 0,
1355
+ publicPrefixes: mergedPrefixes.length > 0 ? mergedPrefixes : void 0,
1356
+ sensitiveKeywords: mergedKeywords.length > 0 ? mergedKeywords : void 0
1357
+ });
1358
+ workspaces.push({
1359
+ name: ws.name,
1360
+ path: ws.path,
1361
+ result
1362
+ });
1363
+ }
1364
+ return {
1365
+ rootDir: resolvedRoot,
1366
+ workspaces
1367
+ };
1368
+ }
885
1369
  // Annotate the CommonJS export names for ESM import in node:
886
1370
  0 && (module.exports = {
887
1371
  BUILTIN_ENV_VARS,
@@ -890,17 +1374,26 @@ function formatGitHubAnnotations(result, options = {}) {
890
1374
  PUBLIC_ENV_PREFIXES,
891
1375
  SENSITIVE_KEYWORDS,
892
1376
  auditEnv,
1377
+ auditWorkspaces,
893
1378
  detectExampleSecretLeak,
894
1379
  detectSecretExposure,
1380
+ diffEnvFiles,
1381
+ discoverWorkspaces,
895
1382
  extractEnvReferences,
896
1383
  formatAuditReport,
1384
+ formatDiffReport,
897
1385
  formatGitHubAnnotations,
1386
+ formatWorkspaceReport,
898
1387
  generateEnvTypes,
1388
+ generateZodSchema,
899
1389
  hasBlockingIssues,
1390
+ hasDiffIssues,
1391
+ inferZodRule,
900
1392
  loadConfig,
901
1393
  parseEnvContent,
902
1394
  parseEnvFile,
903
1395
  scanFiles,
904
1396
  syncEnvExample,
905
- writeEnvTypes
1397
+ writeEnvTypes,
1398
+ writeZodSchema
906
1399
  });