env-detector 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/README.md +24 -10
- package/bin/env-scan.js +89 -35
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -19,16 +19,30 @@ env-detector
|
|
|
19
19
|
|
|
20
20
|
## Commands
|
|
21
21
|
|
|
22
|
-
| Command | Description |
|
|
23
|
-
|
|
24
|
-
| `env-detector` | Generate `.env` |
|
|
25
|
-
| `env-detector --
|
|
26
|
-
| `env-detector --
|
|
27
|
-
| `env-detector --
|
|
28
|
-
| `env-detector --
|
|
29
|
-
| `env-detector --
|
|
30
|
-
| `env-detector --
|
|
31
|
-
| `env-detector --
|
|
22
|
+
| Command | Shorthand | Description |
|
|
23
|
+
|---------|-----------|-------------|
|
|
24
|
+
| `env-detector` | | Generate `.env` |
|
|
25
|
+
| `env-detector --ask` | `-a` | Interactive mode to fill missing or empty values |
|
|
26
|
+
| `env-detector --compare` | `-c` | Show detailed comparison of used, missing, empty, and unused variables |
|
|
27
|
+
| `env-detector --check` | `-k` | Exit with error if variables are missing or empty |
|
|
28
|
+
| `env-detector --fix` | `-f` | **Interactive** cleanup of unused variables |
|
|
29
|
+
| `env-detector --security` | `-s` | Scan for hardcoded secrets in source files and `.env` |
|
|
30
|
+
| `env-detector --strict` | `-t` | Strict mode (CI) with detailed failure reporting |
|
|
31
|
+
| `env-detector --help` | `-h` | Show help message |
|
|
32
|
+
| `env-detector --version` | `-v` | Show version |
|
|
33
|
+
|
|
34
|
+
---
|
|
35
|
+
|
|
36
|
+
## Features
|
|
37
|
+
|
|
38
|
+
### š Interactive Fix
|
|
39
|
+
When running with `--fix` or `-f`, the tool doesn't just delete variables. It lists every unused key it finds and asks for your confirmation (`y/n`) before removing it.
|
|
40
|
+
|
|
41
|
+
### š Detailed Strict Mode
|
|
42
|
+
Ideal for CI/CD pipelines. If `strict` mode fails, it will provide a categorized list of exactly what triggered the failure:
|
|
43
|
+
- **Missing**: Variables used in code but not in `.env`.
|
|
44
|
+
- **Empty**: Variables in `.env` without values.
|
|
45
|
+
- **Unused**: Variables in `.env` not found in code.
|
|
32
46
|
|
|
33
47
|
---
|
|
34
48
|
|
package/bin/env-scan.js
CHANGED
|
@@ -10,32 +10,47 @@ const envPath = path.join(cwd, ".env");
|
|
|
10
10
|
|
|
11
11
|
const args = process.argv.slice(2);
|
|
12
12
|
|
|
13
|
-
const askMode = args.includes("--ask");
|
|
14
|
-
const compareMode = args.includes("--compare");
|
|
15
|
-
const checkMode = args.includes("--check");
|
|
16
|
-
const fixMode = args.includes("--fix");
|
|
17
|
-
const securityMode = args.includes("--security");
|
|
18
|
-
const strictMode = args.includes("--strict");
|
|
19
|
-
const versionMode = args.includes("--version") || args.includes("
|
|
13
|
+
const askMode = args.includes("--ask") || args.includes("-a");
|
|
14
|
+
const compareMode = args.includes("--compare") || args.includes("-c");
|
|
15
|
+
const checkMode = args.includes("--check") || args.includes("-k");
|
|
16
|
+
const fixMode = args.includes("--fix") || args.includes("-f");
|
|
17
|
+
const securityMode = args.includes("--security") || args.includes("-s");
|
|
18
|
+
const strictMode = args.includes("--strict") || args.includes("-t");
|
|
19
|
+
const versionMode = args.includes("--version") || args.includes("-v")
|
|
20
20
|
const helpMode = args.includes("--help") || args.includes("-h");
|
|
21
21
|
|
|
22
|
-
|
|
22
|
+
const validFlags = [
|
|
23
|
+
"--ask", "-a",
|
|
24
|
+
"--compare", "-c",
|
|
25
|
+
"--check", "-k",
|
|
26
|
+
"--fix", "-f",
|
|
27
|
+
"--security", "-s",
|
|
28
|
+
"--strict", "-t",
|
|
29
|
+
"--version", "-v",
|
|
30
|
+
"--help", "-h"
|
|
31
|
+
];
|
|
32
|
+
|
|
33
|
+
const unknownFlag = args.find(arg => arg.startsWith("-") && !validFlags.includes(arg));
|
|
34
|
+
|
|
35
|
+
if (unknownFlag && !helpMode) {
|
|
36
|
+
console.log(`\nError: Unknown flag "${unknownFlag}"`);
|
|
37
|
+
}
|
|
23
38
|
|
|
24
|
-
if (helpMode) {
|
|
39
|
+
if (helpMode || (unknownFlag && !helpMode)) {
|
|
25
40
|
console.log(`
|
|
26
41
|
Usage: env-detector [options]
|
|
27
42
|
|
|
28
43
|
Options:
|
|
29
|
-
--ask Interactive mode to fill missing or empty values
|
|
30
|
-
--compare Show detailed comparison of used, missing, empty, and unused variables
|
|
31
|
-
--check Exit with error if variables are missing or empty
|
|
32
|
-
--fix Remove unused variables from .env
|
|
33
|
-
--security Scan for hardcoded secrets in source files and .env
|
|
34
|
-
--strict Fail if any issues (missing, empty, or unused) are found
|
|
35
|
-
--version Show version information
|
|
36
|
-
--help
|
|
44
|
+
-a, --ask Interactive mode to fill missing or empty values
|
|
45
|
+
-c, --compare Show detailed comparison of used, missing, empty, and unused variables
|
|
46
|
+
-k, --check Exit with error if variables are missing or empty
|
|
47
|
+
-f, --fix Remove unused variables from .env
|
|
48
|
+
-s, --security Scan for hardcoded secrets in source files and .env
|
|
49
|
+
-t, --strict Fail if any issues (missing, empty, or unused) are found
|
|
50
|
+
-v, --version Show version information
|
|
51
|
+
-h, --help Show this help message
|
|
37
52
|
`);
|
|
38
|
-
process.exit(0);
|
|
53
|
+
process.exit(unknownFlag ? 1 : 0);
|
|
39
54
|
}
|
|
40
55
|
|
|
41
56
|
if (versionMode) {
|
|
@@ -44,6 +59,28 @@ if (versionMode) {
|
|
|
44
59
|
process.exit(0);
|
|
45
60
|
}
|
|
46
61
|
|
|
62
|
+
let result = scanProject(cwd);
|
|
63
|
+
|
|
64
|
+
// interactive fix
|
|
65
|
+
const varsToDelete = new Set();
|
|
66
|
+
|
|
67
|
+
if (fixMode && result.unused.length) {
|
|
68
|
+
console.log("\nReview unused variables:");
|
|
69
|
+
result.unused.forEach(key => {
|
|
70
|
+
if (readline.keyInYN(`Delete unused variable "${key}"?`)) {
|
|
71
|
+
varsToDelete.add(key);
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
console.log("");
|
|
75
|
+
|
|
76
|
+
// update result to reflect choices
|
|
77
|
+
const originalUnused = [...result.unused];
|
|
78
|
+
result.unused = originalUnused.filter(k => varsToDelete.has(k));
|
|
79
|
+
// if we chose NOT to delete it, it's effectively "used" for this run's purposes
|
|
80
|
+
const kept = originalUnused.filter(k => !varsToDelete.has(k));
|
|
81
|
+
result.used.push(...kept);
|
|
82
|
+
}
|
|
83
|
+
|
|
47
84
|
// security
|
|
48
85
|
if (securityMode) {
|
|
49
86
|
const issues = scanSecurity(cwd);
|
|
@@ -100,6 +137,37 @@ if (checkMode) {
|
|
|
100
137
|
}
|
|
101
138
|
|
|
102
139
|
|
|
140
|
+
if (strictMode) {
|
|
141
|
+
let failed = false;
|
|
142
|
+
|
|
143
|
+
if (result.missing.length) {
|
|
144
|
+
failed = true;
|
|
145
|
+
console.log("\nMissing variables:");
|
|
146
|
+
result.missing.forEach(v => console.log(` - ${v}`));
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (result.empty.length) {
|
|
150
|
+
failed = true;
|
|
151
|
+
console.log("\nEmpty variables:");
|
|
152
|
+
result.empty.forEach(v => console.log(` - ${v}`));
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
if (result.unused.length) {
|
|
156
|
+
failed = true;
|
|
157
|
+
console.log("\nUnused variables:");
|
|
158
|
+
result.unused.forEach(v => console.log(` - ${v}`));
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
if (failed) {
|
|
162
|
+
console.log("\nā strict mode failed\n");
|
|
163
|
+
process.exit(1);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
console.log("ā strict mode passed\n");
|
|
167
|
+
process.exit(0);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
|
|
103
171
|
// create env
|
|
104
172
|
if (!fs.existsSync(envPath)) {
|
|
105
173
|
fs.writeFileSync(envPath, "");
|
|
@@ -157,6 +225,8 @@ let content = fs.existsSync(envPath)
|
|
|
157
225
|
const envMap = {};
|
|
158
226
|
|
|
159
227
|
content.split("\n").forEach(line => {
|
|
228
|
+
const trimmed = line.trim();
|
|
229
|
+
if (!trimmed || trimmed.startsWith("#")) return;
|
|
160
230
|
const [k, ...rest] = line.split("=");
|
|
161
231
|
if (!k) return;
|
|
162
232
|
envMap[k.trim()] = rest.join("=");
|
|
@@ -195,7 +265,7 @@ result.missing.forEach(key => {
|
|
|
195
265
|
|
|
196
266
|
// fix unused
|
|
197
267
|
if (fixMode) {
|
|
198
|
-
|
|
268
|
+
varsToDelete.forEach(key => delete envMap[key]);
|
|
199
269
|
}
|
|
200
270
|
|
|
201
271
|
|
|
@@ -206,20 +276,4 @@ const newContent = Object.entries(envMap)
|
|
|
206
276
|
fs.writeFileSync(envPath, newContent + "\n");
|
|
207
277
|
|
|
208
278
|
|
|
209
|
-
// strict
|
|
210
|
-
if (strictMode) {
|
|
211
|
-
if (
|
|
212
|
-
result.missing.length ||
|
|
213
|
-
result.empty.length ||
|
|
214
|
-
result.unused.length
|
|
215
|
-
) {
|
|
216
|
-
console.log("ā strict mode failed\n");
|
|
217
|
-
process.exit(1);
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
console.log("ā strict mode passed\n");
|
|
221
|
-
process.exit(0);
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
|
|
225
279
|
console.log("ā env scan complete\n");
|