Skip to content

Commit 204287d

Browse files
authored
Merge pull request #65 from Chrilleweb/cmn/dev
added warning on .env missing on gitignore
2 parents 3e28720 + f2e2074 commit 204287d

4 files changed

Lines changed: 42 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ This project follows [Keep a Changelog](https://keepachangelog.com/) and [Semant
1313
-
1414

1515
## [2.2.7] - 2025-09-27
16+
### Added
17+
- Added warning on .env not ignored by .gitignore on default.
18+
1619
### Fixed
1720
- Fixed `--strict` error output to console when no warnings are found.
1821

src/services/scanOutputToConsole.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import chalk from 'chalk';
2+
import { warnIfEnvNotIgnored, isEnvIgnoredByGit } from './git.js';
23
import type {
34
ScanUsageOptions,
45
ScanResult,
@@ -229,12 +230,23 @@ export function outputToConsole(
229230
console.log();
230231
}
231232

233+
let envNotIgnored = false;
234+
if (!opts.json) {
235+
warnIfEnvNotIgnored({ cwd: opts.cwd, envFile: '.env' });
236+
237+
const ignored = isEnvIgnoredByGit({ cwd: opts.cwd, envFile: '.env' });
238+
if (ignored === false || ignored === null) {
239+
envNotIgnored = true;
240+
}
241+
}
242+
232243
if (opts.strict) {
233244
const hasWarnings =
234245
scanResult.unused.length > 0 ||
235246
(scanResult.duplicates?.env?.length ?? 0) > 0 ||
236247
(scanResult.duplicates?.example?.length ?? 0) > 0 ||
237-
(scanResult.secrets?.length ?? 0) > 0;
248+
(scanResult.secrets?.length ?? 0) > 0 ||
249+
envNotIgnored;
238250

239251
if (hasWarnings) {
240252
exitWithError = true;
@@ -247,6 +259,7 @@ export function outputToConsole(
247259
warnings.push('duplicate keys in example');
248260
if ((scanResult.secrets?.length ?? 0) > 0)
249261
warnings.push('potential secrets');
262+
if (envNotIgnored) warnings.push('.env not ignored by git');
250263

251264
console.log(
252265
chalk.red(`💥 Strict mode: Error on warnings → ${warnings.join(', ')}`),

test/e2e/cli.autoscan.e2e.test.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,30 @@ describe('no-flag autoscan', () => {
3838
const res = runCli(cwd, ['--compare']);
3939
expect(res.status).toBe(1);
4040
expect(res.stdout).toContain('Comparing .env ↔ .env.example');
41-
expect(res.stdout).toContain('Comparing .env.staging ↔ .env.example.staging');
41+
expect(res.stdout).toContain(
42+
'Comparing .env.staging ↔ .env.example.staging',
43+
);
4244
expect(res.stdout).toContain('Missing keys');
4345
});
46+
it('will warn about .env not ignored by .gitignore', () => {
47+
const cwd = tmpDir();
48+
49+
fs.mkdirSync(path.join(cwd, '.git'));
50+
fs.writeFileSync(path.join(cwd, '.env'), 'API_KEY=test\n');
51+
52+
fs.writeFileSync(path.join(cwd, '.gitignore'), 'node_modules\n');
53+
54+
fs.mkdirSync(path.join(cwd, 'src'), { recursive: true });
55+
fs.writeFileSync(
56+
path.join(cwd, 'src', 'index.ts'),
57+
`const apiKey = process.env.API_KEY;`.trimStart(),
58+
);
59+
60+
const res = runCli(cwd, []);
61+
console.log('stdout:', res.stdout);
62+
console.log('stderr:', res.stderr);
63+
64+
expect(res.status).toBe(0);
65+
expect(res.stdout).toContain('.env is not ignored by Git');
66+
});
4467
});

test/e2e/cli.strict.e2e.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ describe('--strict mode', () => {
6161
it('succeeds when there are no warnings', () => {
6262
const cwd = tmpDir();
6363
fs.writeFileSync(path.join(cwd, '.env'), '');
64+
fs.writeFileSync(path.join(cwd, '.gitignore'), '.env');
6465

6566
const res = runCli(cwd, ['--strict']);
6667
expect(res.status).toBe(0);

0 commit comments

Comments
 (0)