Skip to content

Commit bb633f2

Browse files
authored
fix: lint-staged support (#332)
1 parent 250f61e commit bb633f2

4 files changed

Lines changed: 125 additions & 81 deletions

File tree

.changeset/brown-chicken-look.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'dotenv-diff': patch
3+
'@repo/eslint-config': patch
4+
'@repo/tsconfig': patch
5+
---
6+
7+
Added support for lint-staged

docs/git_hooks_ci.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ Running `dotenv-diff` before each commit helps catch missing, unused, and misuse
1010

1111
A common setup is Husky + lint-staged, where `dotenv-diff` runs automatically on commit.
1212

13+
### Example lint-staged config
14+
15+
```json
16+
{
17+
"*.{js,ts,tsx,svelte}": [
18+
"dotenv-diff --example .env.example"
19+
]
20+
}
21+
```
22+
1323
## Running dotenv-diff in GitHub Actions
1424

1525
Use `dotenv-diff` in CI to validate environment variable consistency on pull requests.

packages/cli/src/cli/program.ts

Lines changed: 88 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -5,85 +5,92 @@ import { Command } from 'commander';
55
* @returns The configured commander program instance
66
*/
77
export function createProgram() {
8-
return new Command()
9-
.name('dotenv-diff')
10-
.description('Compare .env and .env.example files')
11-
.option('--check-values', 'Compare actual values if example has values')
12-
.option('--ci', 'Run non-interactively and never create files')
13-
.option('-y, --yes', 'Run non-interactively and answer Yes to prompts')
14-
.option('--env <file>', 'Path to a specific .env file')
15-
.option('--example <file>', 'Path to a specific .env.example file')
16-
.option(
17-
'--allow-duplicates',
18-
'Do not warn about duplicate keys in .env* files',
19-
)
20-
.option('--ignore <keys>', 'Comma-separated list of keys to ignore')
21-
.option('--ignore-regex <pattern>', 'Regex pattern to ignore matching keys')
22-
.option(
23-
'--fix',
24-
'Automatically fix common issues: remove duplicates, add missing keys',
25-
)
26-
.option('--json', 'Output results in JSON format')
27-
.option('--color', 'Enable colored output')
28-
.option('--no-color', 'Disable colored output')
29-
.option(
30-
'--only <list>',
31-
'Comma-separated categories to only run (missing,extra,empty,duplicate,gitignore)',
32-
)
33-
.option('--scan-usage', 'Scan codebase for environment variable usage')
34-
.option('--compare', 'Compare .env and .env.example files')
35-
.option(
36-
'--include-files <patterns>',
37-
'Comma-separated file patterns to ADD to default scan patterns (extends default)',
38-
)
39-
.option(
40-
'--files <patterns>',
41-
'Comma-separated file patterns to scan (completely replaces default patterns)',
42-
)
43-
.option(
44-
'--exclude-files <patterns>',
45-
'Comma-separated file patterns to exclude from scan',
46-
)
47-
.option(
48-
'--show-unused',
49-
'List variables that are defined in .env but not used in code',
50-
)
51-
.option(
52-
'--no-show-unused',
53-
'Do not list variables that are defined in .env but not used in code',
54-
)
55-
.option('--show-stats', 'Show statistics')
56-
.option('--no-show-stats', 'Do not show statistics')
57-
.option('--strict', 'Enable fail on warnings')
58-
.option(
59-
'--secrets',
60-
'Enable secret detection during scan (enabled by default)',
61-
)
62-
.option(
63-
'--no-secrets',
64-
'Disable secret detection during scan (enabled by default)',
65-
)
66-
.option(
67-
'--ignore-urls <list>',
68-
'Comma-separated URLs to ignore in secret scan',
69-
)
70-
.option(
71-
'--uppercase-keys',
72-
'Enable uppercase key validation (enabled by default)',
73-
)
74-
.option('--no-uppercase-keys', 'Disable uppercase key validation')
75-
.option(
76-
'--expire-warnings',
77-
'Enable expiration date warnings for environment variables (enabled by default)',
78-
)
79-
.option('--no-expire-warnings', 'Disable expiration date warnings')
80-
.option(
81-
'--inconsistent-naming-warnings',
82-
'Enable inconsistent naming pattern warnings (enabled by default)',
83-
)
84-
.option(
85-
'--no-inconsistent-naming-warnings',
86-
'Disable inconsistent naming pattern warnings',
87-
)
88-
.option('--init', 'Create a sample dotenv-diff.config.json file');
8+
return (
9+
new Command()
10+
.name('dotenv-diff')
11+
.description('Compare .env and .env.example files')
12+
// Ignore extra positional args
13+
.allowExcessArguments(true)
14+
.option('--check-values', 'Compare actual values if example has values')
15+
.option('--ci', 'Run non-interactively and never create files')
16+
.option('-y, --yes', 'Run non-interactively and answer Yes to prompts')
17+
.option('--env <file>', 'Path to a specific .env file')
18+
.option('--example <file>', 'Path to a specific .env.example file')
19+
.option(
20+
'--allow-duplicates',
21+
'Do not warn about duplicate keys in .env* files',
22+
)
23+
.option('--ignore <keys>', 'Comma-separated list of keys to ignore')
24+
.option(
25+
'--ignore-regex <pattern>',
26+
'Regex pattern to ignore matching keys',
27+
)
28+
.option(
29+
'--fix',
30+
'Automatically fix common issues: remove duplicates, add missing keys',
31+
)
32+
.option('--json', 'Output results in JSON format')
33+
.option('--color', 'Enable colored output')
34+
.option('--no-color', 'Disable colored output')
35+
.option(
36+
'--only <list>',
37+
'Comma-separated categories to only run (missing,extra,empty,duplicate,gitignore)',
38+
)
39+
.option('--scan-usage', 'Scan codebase for environment variable usage')
40+
.option('--compare', 'Compare .env and .env.example files')
41+
.option(
42+
'--include-files <patterns>',
43+
'Comma-separated file patterns to ADD to default scan patterns (extends default)',
44+
)
45+
.option(
46+
'--files <patterns>',
47+
'Comma-separated file patterns to scan (completely replaces default patterns)',
48+
)
49+
.option(
50+
'--exclude-files <patterns>',
51+
'Comma-separated file patterns to exclude from scan',
52+
)
53+
.option(
54+
'--show-unused',
55+
'List variables that are defined in .env but not used in code',
56+
)
57+
.option(
58+
'--no-show-unused',
59+
'Do not list variables that are defined in .env but not used in code',
60+
)
61+
.option('--show-stats', 'Show statistics')
62+
.option('--no-show-stats', 'Do not show statistics')
63+
.option('--strict', 'Enable fail on warnings')
64+
.option(
65+
'--secrets',
66+
'Enable secret detection during scan (enabled by default)',
67+
)
68+
.option(
69+
'--no-secrets',
70+
'Disable secret detection during scan (enabled by default)',
71+
)
72+
.option(
73+
'--ignore-urls <list>',
74+
'Comma-separated URLs to ignore in secret scan',
75+
)
76+
.option(
77+
'--uppercase-keys',
78+
'Enable uppercase key validation (enabled by default)',
79+
)
80+
.option('--no-uppercase-keys', 'Disable uppercase key validation')
81+
.option(
82+
'--expire-warnings',
83+
'Enable expiration date warnings for environment variables (enabled by default)',
84+
)
85+
.option('--no-expire-warnings', 'Disable expiration date warnings')
86+
.option(
87+
'--inconsistent-naming-warnings',
88+
'Enable inconsistent naming pattern warnings (enabled by default)',
89+
)
90+
.option(
91+
'--no-inconsistent-naming-warnings',
92+
'Disable inconsistent naming pattern warnings',
93+
)
94+
.option('--init', 'Create a sample dotenv-diff.config.json file')
95+
);
8996
}

packages/cli/test/unit/cli/program.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,24 @@ describe('createProgram', () => {
3434
expect(opts.color).toBe(false);
3535
expect(opts.strict).toBe(true);
3636
});
37+
38+
it('ignores extra positional arguments', () => {
39+
const program = createProgram();
40+
41+
expect(() => {
42+
program.parse([
43+
'node',
44+
'dotenv-diff',
45+
'--compare',
46+
'--example',
47+
'.env.example',
48+
'file1.ts',
49+
'file2.ts',
50+
]);
51+
}).not.toThrow();
52+
53+
const opts = program.opts();
54+
expect(opts.compare).toBe(true);
55+
expect(opts.example).toBe('.env.example');
56+
});
3757
});

0 commit comments

Comments
 (0)