dotenv-diff 1.0.0 → 1.0.2

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 CHANGED
@@ -1,43 +1,52 @@
1
1
  #!/usr/bin/env node
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');
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('❌ Error: .env file is missing in the project root.'));
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('❌ Error: .env.example file is missing in the project root.'));
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('🔍 Comparing .env and .env.example...'));
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 && diff.extra.length === 0 && emptyKeys.length === 0) {
27
- console.log(chalk.green('✅ All keys match! Your .env file is valid.'));
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('\n❌ Missing keys in .env:'));
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('\n⚠️ Extra keys in .env (not defined in .env.example):'));
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('\n⚠️ The following keys in .env have no value (empty):'));
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);
@@ -0,0 +1,15 @@
1
+ export function diffEnv(current, example) {
2
+ const currentKeys = Object.keys(current);
3
+ const exampleKeys = Object.keys(example);
4
+ const missing = exampleKeys.filter((key) => !currentKeys.includes(key));
5
+ const extra = currentKeys.filter((key) => !exampleKeys.includes(key));
6
+ const valueMismatches = exampleKeys
7
+ .filter((key) => currentKeys.includes(key))
8
+ .filter((key) => current[key] !== example[key]) // her kan du evt. bruge .toLowerCase() hvis det skal være case-insensitive
9
+ .map((key) => ({
10
+ key,
11
+ expected: example[key],
12
+ actual: current[key],
13
+ }));
14
+ return { missing, extra, valueMismatches };
15
+ }
@@ -0,0 +1,16 @@
1
+ import fs from 'fs';
2
+ export function parseEnvFile(path) {
3
+ const content = fs.readFileSync(path, 'utf-8');
4
+ const lines = content.split('\n');
5
+ const result = {};
6
+ for (const line of lines) {
7
+ const trimmed = line.trim();
8
+ if (!trimmed || trimmed.startsWith('#'))
9
+ continue;
10
+ const [key, ...rest] = trimmed.split('=');
11
+ if (!key)
12
+ continue;
13
+ result[key.trim()] = rest.join('=').trim();
14
+ }
15
+ return result;
16
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dotenv-diff",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "type": "module",
5
5
  "description": "A small CLI and library to find differences between .env and .env.example files.",
6
6
  "bin": {
@@ -10,6 +10,8 @@
10
10
  "types": "dist/index.d.ts",
11
11
  "files": [
12
12
  "dist/cli.js",
13
+ "dist/lib/diffEnv.js",
14
+ "dist/lib/parseEnv.js",
13
15
  "dist/index.js",
14
16
  "dist/index.d.ts",
15
17
  "README.md"