dotenv-diff 1.0.1 → 1.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/dist/cli.js +25 -16
- package/dist/lib/diffEnv.js +12 -1
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -1,43 +1,52 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import path from
|
|
3
|
-
import fs from
|
|
4
|
-
import chalk from
|
|
5
|
-
import { parseEnvFile } from
|
|
6
|
-
import { diffEnv } from
|
|
7
|
-
const envPath = path.resolve(process.cwd(),
|
|
8
|
-
const examplePath = path.resolve(process.cwd(),
|
|
2
|
+
import path from "path";
|
|
3
|
+
import fs from "fs";
|
|
4
|
+
import chalk from "chalk";
|
|
5
|
+
import { parseEnvFile } from "./lib/parseEnv.js";
|
|
6
|
+
import { diffEnv } from "./lib/diffEnv.js";
|
|
7
|
+
const envPath = path.resolve(process.cwd(), ".env");
|
|
8
|
+
const examplePath = path.resolve(process.cwd(), ".env.example");
|
|
9
9
|
if (!fs.existsSync(envPath)) {
|
|
10
|
-
console.error(chalk.red(
|
|
10
|
+
console.error(chalk.red("❌ Error: .env file is missing in the project root."));
|
|
11
11
|
process.exit(1);
|
|
12
12
|
}
|
|
13
13
|
if (!fs.existsSync(examplePath)) {
|
|
14
|
-
console.error(chalk.red(
|
|
14
|
+
console.error(chalk.red("❌ Error: .env.example file is missing in the project root."));
|
|
15
15
|
process.exit(1);
|
|
16
16
|
}
|
|
17
|
-
console.log(chalk.bold(
|
|
17
|
+
console.log(chalk.bold("🔍 Comparing .env and .env.example..."));
|
|
18
18
|
const current = parseEnvFile(envPath);
|
|
19
19
|
const example = parseEnvFile(examplePath);
|
|
20
20
|
const diff = diffEnv(current, example);
|
|
21
21
|
// Find tomme variabler i .env
|
|
22
22
|
const emptyKeys = Object.entries(current)
|
|
23
|
-
.filter(([key, value]) => value.trim() ===
|
|
23
|
+
.filter(([key, value]) => value.trim() === "")
|
|
24
24
|
.map(([key]) => key);
|
|
25
25
|
let hasWarnings = false;
|
|
26
|
-
if (diff.missing.length === 0 &&
|
|
27
|
-
|
|
26
|
+
if (diff.missing.length === 0 &&
|
|
27
|
+
diff.extra.length === 0 &&
|
|
28
|
+
emptyKeys.length === 0) {
|
|
29
|
+
console.log(chalk.green("✅ All keys match! Your .env file is valid."));
|
|
28
30
|
process.exit(0);
|
|
29
31
|
}
|
|
30
32
|
if (diff.missing.length > 0) {
|
|
31
|
-
console.log(chalk.red(
|
|
33
|
+
console.log(chalk.red("\n❌ Missing keys in .env:"));
|
|
32
34
|
diff.missing.forEach((key) => console.log(chalk.red(` - ${key}`)));
|
|
33
35
|
}
|
|
34
36
|
if (diff.extra.length > 0) {
|
|
35
|
-
console.log(chalk.yellow(
|
|
37
|
+
console.log(chalk.yellow("\n⚠️ Extra keys in .env (not defined in .env.example):"));
|
|
36
38
|
diff.extra.forEach((key) => console.log(chalk.yellow(` - ${key}`)));
|
|
37
39
|
}
|
|
38
40
|
if (emptyKeys.length > 0) {
|
|
39
41
|
hasWarnings = true;
|
|
40
|
-
console.log(chalk.yellow(
|
|
42
|
+
console.log(chalk.yellow("\n⚠️ The following keys in .env have no value (empty):"));
|
|
41
43
|
emptyKeys.forEach((key) => console.log(chalk.yellow(` - ${key}`)));
|
|
42
44
|
}
|
|
45
|
+
if (diff.valueMismatches.length > 0) {
|
|
46
|
+
hasWarnings = true;
|
|
47
|
+
console.log(chalk.yellow("\n⚠️ The following keys have different values:"));
|
|
48
|
+
diff.valueMismatches.forEach(({ key, expected, actual }) => {
|
|
49
|
+
console.log(chalk.yellow(` - ${key}: expected "${expected}", but got "${actual}"`));
|
|
50
|
+
});
|
|
51
|
+
}
|
|
43
52
|
process.exit(diff.missing.length > 0 ? 1 : hasWarnings ? 0 : 0);
|
package/dist/lib/diffEnv.js
CHANGED
|
@@ -3,5 +3,16 @@ export function diffEnv(current, example) {
|
|
|
3
3
|
const exampleKeys = Object.keys(example);
|
|
4
4
|
const missing = exampleKeys.filter((key) => !currentKeys.includes(key));
|
|
5
5
|
const extra = currentKeys.filter((key) => !exampleKeys.includes(key));
|
|
6
|
-
|
|
6
|
+
const valueMismatches = exampleKeys
|
|
7
|
+
.filter((key) => {
|
|
8
|
+
return (currentKeys.includes(key) &&
|
|
9
|
+
example[key].trim() !== "" &&
|
|
10
|
+
current[key] !== example[key]);
|
|
11
|
+
})
|
|
12
|
+
.map((key) => ({
|
|
13
|
+
key,
|
|
14
|
+
expected: example[key],
|
|
15
|
+
actual: current[key],
|
|
16
|
+
}));
|
|
17
|
+
return { missing, extra, valueMismatches };
|
|
7
18
|
}
|