Skip to content

Commit 83a406a

Browse files
committed
printIssues
1 parent b9de5ea commit 83a406a

2 files changed

Lines changed: 42 additions & 35 deletions

File tree

src/commands/compare.ts

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { printStats } from '../ui/compare/printStats.js';
1919
import { printDuplicates } from '../ui/compare/printDuplicates.js';
2020
import { printHeader } from '../ui/compare/printHeader.js';
2121
import { printAutoFix } from '../ui/compare/printAutoFix.js';
22+
import { printIssues } from '../ui/compare/printIssues.js';
2223

2324
/**
2425
* Compares multiple pairs of .env and .env.example files.
@@ -216,41 +217,7 @@ export async function compareMany(
216217
exitWithError = true;
217218
}
218219

219-
if (!opts.json) {
220-
if (filtered.missing.length && !opts.fix) {
221-
console.log(chalk.red('❌ Missing keys:'));
222-
filtered.missing.forEach((key) => console.log(chalk.red(` - ${key}`)));
223-
console.log();
224-
}
225-
if (filtered.extra.length) {
226-
console.log(chalk.yellow('⚠️ Extra keys (not in example):'));
227-
filtered.extra.forEach((key) =>
228-
console.log(chalk.yellow(` - ${key}`)),
229-
);
230-
console.log();
231-
}
232-
if (filtered.empty.length) {
233-
console.log(chalk.yellow('⚠️ Empty values:'));
234-
filtered.empty.forEach((key) =>
235-
console.log(chalk.yellow(` - ${key}`)),
236-
);
237-
console.log();
238-
}
239-
if (filtered.mismatches.length) {
240-
console.log(chalk.yellow('⚠️ Value mismatches:'));
241-
filtered.mismatches.forEach(({ key, expected, actual }) =>
242-
console.log(
243-
chalk.yellow(
244-
` - ${key}: expected '${expected}', but got '${actual}'`,
245-
),
246-
),
247-
);
248-
console.log();
249-
}
250-
if (gitignoreMsg && !opts.json) {
251-
console.log((gitignoreMsg as string).replace(/^/gm, ' '));
252-
}
253-
}
220+
printIssues(filtered, opts.json);
254221

255222
if (!opts.json && !opts.fix) {
256223
const ignored = isEnvIgnoredByGit({ cwd: opts.cwd, envFile: '.env' });

src/ui/compare/printIssues.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import chalk from 'chalk';
2+
import { type Filtered } from '../../config/types.js';
3+
4+
/**
5+
* Prints the issues found during the comparison.
6+
* @param filtered The filtered comparison results.
7+
* @param json Whether to output in JSON format.
8+
* @returns void
9+
*/
10+
export function printIssues(
11+
filtered: Filtered,
12+
json: boolean | undefined,
13+
) {
14+
if (json) return;
15+
if (filtered.missing.length) {
16+
const header = chalk.red('❌ Missing keys:');
17+
console.log(header);
18+
filtered.missing.forEach((key) => console.log(chalk.red(` - ${key}`)));
19+
console.log();
20+
}
21+
if (filtered.extra.length) {
22+
console.log(chalk.yellow('⚠️ Extra keys (not in example):'));
23+
filtered.extra.forEach((key) => console.log(chalk.yellow(` - ${key}`)));
24+
console.log();
25+
}
26+
if (filtered.empty.length) {
27+
console.log(chalk.yellow('⚠️ Empty values:'));
28+
filtered.empty.forEach((key) => console.log(chalk.yellow(` - ${key}`)));
29+
console.log();
30+
}
31+
if (filtered.mismatches.length) {
32+
console.log(chalk.yellow('⚠️ Value mismatches:'));
33+
filtered.mismatches.forEach(({ key, expected, actual }) =>
34+
console.log(
35+
chalk.yellow(` - ${key}: expected '${expected}', but got '${actual}'`),
36+
),
37+
);
38+
console.log();
39+
}
40+
}

0 commit comments

Comments
 (0)