env-detector 1.0.5 ā 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/LICENSE +20 -1
- package/README.md +24 -10
- package/bin/env-scan.js +97 -26
- package/package.json +1 -1
package/LICENSE
CHANGED
|
@@ -1,2 +1,21 @@
|
|
|
1
1
|
MIT License
|
|
2
|
-
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Jenil Gajjar
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
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,15 +10,48 @@ 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
|
+
const helpMode = args.includes("--help") || args.includes("-h");
|
|
21
|
+
|
|
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
|
+
}
|
|
20
38
|
|
|
21
|
-
|
|
39
|
+
if (helpMode || (unknownFlag && !helpMode)) {
|
|
40
|
+
console.log(`
|
|
41
|
+
Usage: env-detector [options]
|
|
42
|
+
|
|
43
|
+
Options:
|
|
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
|
|
52
|
+
`);
|
|
53
|
+
process.exit(unknownFlag ? 1 : 0);
|
|
54
|
+
}
|
|
22
55
|
|
|
23
56
|
if (versionMode) {
|
|
24
57
|
const pkg = require("../package.json");
|
|
@@ -26,6 +59,28 @@ if (versionMode) {
|
|
|
26
59
|
process.exit(0);
|
|
27
60
|
}
|
|
28
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
|
+
|
|
29
84
|
// security
|
|
30
85
|
if (securityMode) {
|
|
31
86
|
const issues = scanSecurity(cwd);
|
|
@@ -82,13 +137,43 @@ if (checkMode) {
|
|
|
82
137
|
}
|
|
83
138
|
|
|
84
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
|
+
|
|
85
171
|
// create env
|
|
86
172
|
if (!fs.existsSync(envPath)) {
|
|
87
173
|
fs.writeFileSync(envPath, "");
|
|
88
174
|
}
|
|
89
175
|
|
|
90
176
|
|
|
91
|
-
// grouped generation
|
|
92
177
|
// grouped generation
|
|
93
178
|
if (Object.keys(result.grouped).length && !askMode) {
|
|
94
179
|
|
|
@@ -140,6 +225,8 @@ let content = fs.existsSync(envPath)
|
|
|
140
225
|
const envMap = {};
|
|
141
226
|
|
|
142
227
|
content.split("\n").forEach(line => {
|
|
228
|
+
const trimmed = line.trim();
|
|
229
|
+
if (!trimmed || trimmed.startsWith("#")) return;
|
|
143
230
|
const [k, ...rest] = line.split("=");
|
|
144
231
|
if (!k) return;
|
|
145
232
|
envMap[k.trim()] = rest.join("=");
|
|
@@ -178,7 +265,7 @@ result.missing.forEach(key => {
|
|
|
178
265
|
|
|
179
266
|
// fix unused
|
|
180
267
|
if (fixMode) {
|
|
181
|
-
|
|
268
|
+
varsToDelete.forEach(key => delete envMap[key]);
|
|
182
269
|
}
|
|
183
270
|
|
|
184
271
|
|
|
@@ -189,20 +276,4 @@ const newContent = Object.entries(envMap)
|
|
|
189
276
|
fs.writeFileSync(envPath, newContent + "\n");
|
|
190
277
|
|
|
191
278
|
|
|
192
|
-
// strict
|
|
193
|
-
if (strictMode) {
|
|
194
|
-
if (
|
|
195
|
-
result.missing.length ||
|
|
196
|
-
result.empty.length ||
|
|
197
|
-
result.unused.length
|
|
198
|
-
) {
|
|
199
|
-
console.log("ā strict mode failed\n");
|
|
200
|
-
process.exit(1);
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
console.log("ā strict mode passed\n");
|
|
204
|
-
process.exit(0);
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
|
|
208
279
|
console.log("ā env scan complete\n");
|