dotenv-key-diff 0.0.0-stage → 0.1.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 +56 -2
- package/bin/dotenv-diff.js +95 -0
- package/package.json +27 -4
- package/src/index.d.ts +9 -0
- package/src/index.js +38 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Ali Dakoumi
|
|
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
|
@@ -1,3 +1,57 @@
|
|
|
1
|
-
#
|
|
1
|
+
# dotenv-key-diff
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Compare the variable names in `.env.example` with `.env` to catch missing or stale configuration keys. Values are never displayed.
|
|
4
|
+
|
|
5
|
+
## Use it from the command line
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npx dotenv-key-diff
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
By default, it reads `.env.example` and `.env` from the current directory. You can choose other files and produce JSON for scripts or CI:
|
|
12
|
+
|
|
13
|
+
```sh
|
|
14
|
+
npx dotenv-key-diff --example .env.example --env .env.local
|
|
15
|
+
npx dotenv-key-diff --json
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
Exit codes: `0` when keys match, `1` when keys are missing or extra, and `2` when an option or file is invalid. The tool compares key names only; it does not validate values or load them into your process.
|
|
19
|
+
|
|
20
|
+
## Use it as a library
|
|
21
|
+
|
|
22
|
+
```js
|
|
23
|
+
import { compareEnv, parseEnvKeys } from "dotenv-key-diff"
|
|
24
|
+
|
|
25
|
+
const result = compareEnv(
|
|
26
|
+
"DATABASE_URL=\nPUBLIC_API_URL=\n",
|
|
27
|
+
"DATABASE_URL=postgres://...\n",
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
console.log(result.missing) // ["PUBLIC_API_URL"]
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
`compareEnv(exampleSource, envSource)` returns `missing`, `extra`, and `shared` key arrays. `parseEnvKeys(source)` returns unique valid environment variable names in the order they first appear. Blank lines, comments, and malformed assignments are ignored; `export KEY=value` is supported.
|
|
34
|
+
|
|
35
|
+
## Development
|
|
36
|
+
|
|
37
|
+
Requires Node.js 20 or newer. There are no runtime or development dependencies.
|
|
38
|
+
|
|
39
|
+
```sh
|
|
40
|
+
npm run check
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
This runs the unit suite, checks JavaScript syntax, and verifies that the npm package tarball contains the expected files. There are no runtime or development dependencies.
|
|
44
|
+
|
|
45
|
+
## Interactive playground
|
|
46
|
+
|
|
47
|
+
Run the browser-only comparison playground with:
|
|
48
|
+
|
|
49
|
+
```sh
|
|
50
|
+
npm run preview
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
Open `http://localhost:4173`. The page imports the package source directly, compares only key names, and keeps all input in browser memory. The preview server only serves the playground page and library module; it does not expose the rest of the repository. The playground is included in the GitHub repository, not in the published npm package.
|
|
54
|
+
|
|
55
|
+
## License
|
|
56
|
+
|
|
57
|
+
MIT
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { readFile } from "node:fs/promises"
|
|
3
|
+
import { compareEnv } from "../src/index.js"
|
|
4
|
+
|
|
5
|
+
const usage = `Usage: dotenv-key-diff [options]
|
|
6
|
+
|
|
7
|
+
Compare the keys in .env.example and .env without printing their values.
|
|
8
|
+
|
|
9
|
+
Options:
|
|
10
|
+
--example <path> Template file (default: .env.example)
|
|
11
|
+
--env <path> Local environment file (default: .env)
|
|
12
|
+
--json Print a machine-readable JSON report
|
|
13
|
+
--help Show this help
|
|
14
|
+
|
|
15
|
+
Exit codes:
|
|
16
|
+
0 Keys match
|
|
17
|
+
1 Missing or extra keys found
|
|
18
|
+
2 Invalid arguments or files could not be read`
|
|
19
|
+
|
|
20
|
+
function parseArgs(args) {
|
|
21
|
+
const options = { example: ".env.example", env: ".env", json: false, help: false }
|
|
22
|
+
|
|
23
|
+
for (let index = 0; index < args.length; index += 1) {
|
|
24
|
+
const arg = args[index]
|
|
25
|
+
|
|
26
|
+
if (arg === "--example" || arg === "--env") {
|
|
27
|
+
const value = args[index + 1]
|
|
28
|
+
if (!value || value.startsWith("--")) {
|
|
29
|
+
throw new Error(`Expected a file path after ${arg}`)
|
|
30
|
+
}
|
|
31
|
+
options[arg === "--example" ? "example" : "env"] = value
|
|
32
|
+
index += 1
|
|
33
|
+
} else if (arg === "--json") {
|
|
34
|
+
options.json = true
|
|
35
|
+
} else if (arg === "--help" || arg === "-h") {
|
|
36
|
+
options.help = true
|
|
37
|
+
} else {
|
|
38
|
+
throw new Error(`Unknown option: ${arg}`)
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return options
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
async function main(args) {
|
|
46
|
+
let options
|
|
47
|
+
|
|
48
|
+
try {
|
|
49
|
+
options = parseArgs(args)
|
|
50
|
+
} catch (error) {
|
|
51
|
+
process.stderr.write(`dotenv-key-diff: ${error.message}\n\n${usage}\n`)
|
|
52
|
+
return 2
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (options.help) {
|
|
56
|
+
process.stdout.write(`${usage}\n`)
|
|
57
|
+
return 0
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
let exampleSource
|
|
61
|
+
let envSource
|
|
62
|
+
|
|
63
|
+
try {
|
|
64
|
+
;[exampleSource, envSource] = await Promise.all([
|
|
65
|
+
readFile(options.example, "utf8"),
|
|
66
|
+
readFile(options.env, "utf8"),
|
|
67
|
+
])
|
|
68
|
+
} catch (error) {
|
|
69
|
+
process.stderr.write(`dotenv-key-diff: ${error.message}\n`)
|
|
70
|
+
return 2
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const { missing, extra, shared } = compareEnv(exampleSource, envSource)
|
|
74
|
+
const matches = missing.length === 0 && extra.length === 0
|
|
75
|
+
|
|
76
|
+
if (options.json) {
|
|
77
|
+
process.stdout.write(
|
|
78
|
+
`${JSON.stringify({ example: options.example, env: options.env, matches, missing, extra, shared }, null, 2)}\n`,
|
|
79
|
+
)
|
|
80
|
+
} else if (matches) {
|
|
81
|
+
process.stdout.write("Environment keys match.\n")
|
|
82
|
+
} else {
|
|
83
|
+
if (missing.length) {
|
|
84
|
+
process.stdout.write(`Missing from ${options.env}: ${missing.join(", ")}\n`)
|
|
85
|
+
}
|
|
86
|
+
if (extra.length) {
|
|
87
|
+
process.stdout.write(`Not in ${options.example}: ${extra.join(", ")}\n`)
|
|
88
|
+
}
|
|
89
|
+
process.stdout.write(`${shared.length} shared key${shared.length === 1 ? "" : "s"}.\n`)
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
return matches ? 0 : 1
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
process.exitCode = await main(process.argv.slice(2))
|
package/package.json
CHANGED
|
@@ -1,6 +1,29 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dotenv-key-diff",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Compare .env keys with .env.example without exposing values",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"engines": {
|
|
7
|
+
"node": ">=20"
|
|
8
|
+
},
|
|
9
|
+
"types": "./src/index.d.ts",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./src/index.d.ts",
|
|
13
|
+
"import": "./src/index.js"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"bin": {
|
|
17
|
+
"dotenv-key-diff": "./bin/dotenv-diff.js"
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"bin/",
|
|
21
|
+
"src/"
|
|
22
|
+
],
|
|
23
|
+
"scripts": {
|
|
24
|
+
"test": "node --test",
|
|
25
|
+
"check": "npm test && node --check src/index.js && node --check bin/dotenv-diff.js && node --check scripts/preview-server.js && npm pack --dry-run",
|
|
26
|
+
"preview": "node scripts/preview-server.js"
|
|
27
|
+
},
|
|
28
|
+
"license": "MIT"
|
|
29
|
+
}
|
package/src/index.d.ts
ADDED
package/src/index.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
const ENV_KEY_PATTERN = /^[A-Za-z_][A-Za-z0-9_]*$/
|
|
2
|
+
|
|
3
|
+
export function parseEnvKeys(source) {
|
|
4
|
+
if (typeof source !== "string") {
|
|
5
|
+
throw new TypeError("Expected environment file contents to be a string")
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
const keys = new Set()
|
|
9
|
+
const normalizedSource = source.replace(/^\uFEFF/, "")
|
|
10
|
+
|
|
11
|
+
for (const rawLine of normalizedSource.split(/\r?\n/)) {
|
|
12
|
+
let line = rawLine.trim()
|
|
13
|
+
|
|
14
|
+
if (!line || line.startsWith("#")) continue
|
|
15
|
+
line = line.replace(/^export\s+/, "")
|
|
16
|
+
|
|
17
|
+
const equalsIndex = line.indexOf("=")
|
|
18
|
+
if (equalsIndex === -1) continue
|
|
19
|
+
|
|
20
|
+
const key = line.slice(0, equalsIndex).trim()
|
|
21
|
+
if (ENV_KEY_PATTERN.test(key)) keys.add(key)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return [...keys]
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function compareEnv(exampleSource, envSource) {
|
|
28
|
+
const expectedKeys = parseEnvKeys(exampleSource)
|
|
29
|
+
const actualKeys = parseEnvKeys(envSource)
|
|
30
|
+
const actualSet = new Set(actualKeys)
|
|
31
|
+
const expectedSet = new Set(expectedKeys)
|
|
32
|
+
|
|
33
|
+
return {
|
|
34
|
+
missing: expectedKeys.filter((key) => !actualSet.has(key)),
|
|
35
|
+
extra: actualKeys.filter((key) => !expectedSet.has(key)),
|
|
36
|
+
shared: expectedKeys.filter((key) => actualSet.has(key)),
|
|
37
|
+
}
|
|
38
|
+
}
|