Skip to content

Commit 1fd3eac

Browse files
committed
printHeader to compare
1 parent 2206561 commit 1fd3eac

2 files changed

Lines changed: 49 additions & 25 deletions

File tree

src/commands/compare.ts

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,18 @@ import { diffEnv } from '../core/diffEnv.js';
66
import { warnIfEnvNotIgnored, isEnvIgnoredByGit } from '../services/git.js';
77
import { findDuplicateKeys } from '../services/duplicates.js';
88
import { filterIgnoredKeys } from '../core/filterIgnoredKeys.js';
9-
import type { Category, CompareJsonEntry, ComparisonOptions, FilePair, ComparisonResult } from '../config/types.js';
9+
import type {
10+
Category,
11+
CompareJsonEntry,
12+
ComparisonOptions,
13+
FilePair,
14+
ComparisonResult,
15+
} from '../config/types.js';
1016
import { applyFixes } from '../core/fixEnv.js';
1117
import { printFixTips } from '../ui/compare/printFixTips.js';
1218
import { printStats } from '../ui/compare/printStats.js';
1319
import { printDuplicates } from '../ui/compare/printDuplicates.js';
20+
import { printHeader } from '../ui/compare/printHeader.js';
1421

1522
/**
1623
* Compares multiple pairs of .env and .env.example files.
@@ -42,24 +49,16 @@ export async function compareMany(
4249
const exampleName = path.basename(examplePath);
4350
const entry: CompareJsonEntry = { env: envName, example: exampleName };
4451

45-
if (!fs.existsSync(envPath) || !fs.existsSync(examplePath)) {
46-
if (!opts.json) {
47-
console.log();
48-
console.log(chalk.blue(`🔍 Comparing ${envName}${exampleName}...`));
49-
console.log(
50-
chalk.yellow('⚠️ Skipping: missing matching example file.'),
51-
);
52-
console.log();
53-
}
52+
const skipping = !fs.existsSync(envPath) || !fs.existsSync(examplePath);
53+
54+
if (skipping) {
55+
printHeader(envName, exampleName, opts.json, skipping);
56+
exitWithError = true;
5457
entry.skipped = { reason: 'missing file' };
5558
opts.collect?.(entry);
5659
continue;
57-
}
58-
59-
if (!opts.json) {
60-
console.log();
61-
console.log(chalk.blue(`🔍 Comparing ${envName}${exampleName}...`));
62-
console.log();
60+
} else {
61+
printHeader(envName, exampleName, opts.json, skipping);
6362
}
6463

6564
// Git ignore hint (only when not JSON)
@@ -150,13 +149,20 @@ export async function compareMany(
150149
? filtered.mismatches.length
151150
: 0;
152151

153-
printStats(envName, exampleName, {
152+
printStats(
153+
envName,
154+
exampleName,
155+
{
154156
envCount,
155157
exampleCount,
156158
sharedCount,
157159
duplicateCount,
158160
valueMismatchCount,
159-
}, filtered, opts.json ?? false, opts.showStats);
161+
},
162+
filtered,
163+
opts.json ?? false,
164+
opts.showStats,
165+
);
160166
}
161167

162168
const allOk =
@@ -177,13 +183,7 @@ export async function compareMany(
177183
continue;
178184
}
179185

180-
printDuplicates(
181-
envName,
182-
exampleName,
183-
dupsEnv,
184-
dupsEx,
185-
opts.json ?? false,
186-
);
186+
printDuplicates(envName, exampleName, dupsEnv, dupsEx, opts.json ?? false);
187187

188188
if (filtered.missing.length) {
189189
entry.missing = filtered.missing;

src/ui/compare/printHeader.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import chalk from 'chalk';
2+
3+
/**
4+
* Prints the header for the comparison output.
5+
* @param envName The name of the environment file.
6+
* @param exampleName The name of the example file.
7+
* @param json Whether to output in JSON format.
8+
* @param skipping Whether the comparison is being skipped.
9+
* @returns void
10+
*/
11+
export function printHeader(
12+
envName: string,
13+
exampleName: string,
14+
json: boolean | undefined,
15+
skipping: boolean,
16+
) {
17+
if (json) return;
18+
console.log();
19+
console.log(chalk.blue(`🔍 Comparing ${envName}${exampleName}...`));
20+
if (skipping) {
21+
console.log(chalk.yellow('⚠️ Skipping: missing matching example file.'));
22+
}
23+
console.log();
24+
}

0 commit comments

Comments
 (0)