diff --git a/src/commands/compare.ts b/src/commands/compare.ts index eb61638..a251a34 100644 --- a/src/commands/compare.ts +++ b/src/commands/compare.ts @@ -194,13 +194,12 @@ export async function compareMany( ensureGitignore: hasGitignoreIssue, }); - printAutoFix( - changed, - result, - envName, - opts.json ?? false, - hasGitignoreIssue, - ); + const fixContext = { + ...result, + fixApplied: changed, + }; + + printAutoFix(fixContext, envName, opts.json ?? false); } opts.collect?.(entry); diff --git a/src/commands/scanUsage.ts b/src/commands/scanUsage.ts index e335bc5..7e2346e 100644 --- a/src/commands/scanUsage.ts +++ b/src/commands/scanUsage.ts @@ -102,10 +102,10 @@ export async function scanUsage(opts: ScanUsageOptions): Promise { } else { scanResult = result.scanResult; comparedAgainst = result.comparedAgainst; - fixApplied = result.fixApplied; - removedDuplicates = result.removedDuplicates; - fixedKeys = result.addedEnv; - gitignoreUpdated = result.gitignoreUpdated; + fixApplied = result.fix.fixApplied; + removedDuplicates = result.fix.removedDuplicates; + fixedKeys = result.fix.addedEnv; + gitignoreUpdated = result.fix.gitignoreUpdated; if (result.uppercaseWarnings) { scanResult.uppercaseWarnings = result.uppercaseWarnings; } diff --git a/src/config/types.ts b/src/config/types.ts index e7eec3f..ba622a2 100644 --- a/src/config/types.ts +++ b/src/config/types.ts @@ -349,6 +349,14 @@ export interface FixResult { gitignoreUpdated: boolean; } +/** + * Context for auto-fix operations, extending FixResult with applied status + */ +export interface FixContext extends FixResult { + /** Whether any fixes were applied */ + fixApplied: boolean; +} + /** * Warning about environment variable keys that are not uppercase. */ diff --git a/src/services/printScanResult.ts b/src/services/printScanResult.ts index 691c375..3058e0e 100644 --- a/src/services/printScanResult.ts +++ b/src/services/printScanResult.ts @@ -4,7 +4,7 @@ import type { ScanUsageOptions, ScanResult, ExitResult, - FixResult, + FixContext, } from '../config/types.js'; import { DEFAULT_ENV_FILE } from '../config/constants.js'; import { printHeader } from '../ui/scan/printHeader.js'; @@ -26,14 +26,6 @@ import { printHealthScore } from '../ui/scan/printHealthScore.js'; import { printExpireWarnings } from '../ui/scan/printExpireWarnings.js'; import { printInconsistentNamingWarning } from '../ui/scan/printInconsistentNamingWarning.js'; -/** - * Context for auto-fix operations, extending FixResult with applied status - */ -interface FixContext extends FixResult { - /** Whether any fixes were applied */ - fixApplied: boolean; -} - /** * Prints the scan result to the console. * @param scanResult - The result of the scan. @@ -193,14 +185,9 @@ export function printScanResult( if (opts.fix && fixContext) { printAutoFix( - fixContext.fixApplied, - { - removedDuplicates: fixContext.removedDuplicates, - addedEnv: fixContext.addedEnv, - }, + fixContext, comparedAgainst || DEFAULT_ENV_FILE, isJson, - fixContext.gitignoreUpdated, ); } diff --git a/src/services/processComparisonFile.ts b/src/services/processComparisonFile.ts index 6d14eb4..f9782b4 100644 --- a/src/services/processComparisonFile.ts +++ b/src/services/processComparisonFile.ts @@ -17,6 +17,7 @@ import type { ComparisonFile, ExpireWarning, InconsistentNamingWarning, + FixContext, } from '../config/types.js'; /** @@ -29,10 +30,7 @@ interface ProcessComparisonResult { duplicatesFound: boolean; dupsEnv: Duplicate[]; dupsEx: Duplicate[]; - fixApplied: boolean; - removedDuplicates: string[]; - addedEnv: string[]; - gitignoreUpdated: boolean; + fix: FixContext; exampleFull?: Record | undefined; uppercaseWarnings?: UppercaseWarning[]; expireWarnings?: ExpireWarning[]; @@ -57,15 +55,18 @@ export function processComparisonFile( let duplicatesFound = false; let dupsEnv: Duplicate[] = []; let dupsEx: Duplicate[] = []; - let fixApplied = false; - let removedDuplicates: string[] = []; - let addedEnv: string[] = []; - let gitignoreUpdated = false; let exampleFull: Record | undefined = undefined; let uppercaseWarnings: UppercaseWarning[] = []; let expireWarnings: ExpireWarning[] = []; let inconsistentNamingWarnings: InconsistentNamingWarning[] = []; + const fix: FixContext = { + fixApplied: false, + removedDuplicates: [], + addedEnv: [], + gitignoreUpdated: false, + }; + try { // Load .env.example (if exists) if (opts.examplePath) { @@ -134,10 +135,10 @@ export function processComparisonFile( if (changed) { // Update state based on what was actually fixed - fixApplied = true; - removedDuplicates = result.removedDuplicates; - addedEnv = result.addedEnv; - gitignoreUpdated = result.gitignoreUpdated; + fix.fixApplied = true; + fix.removedDuplicates = result.removedDuplicates; + fix.addedEnv = result.addedEnv; + fix.gitignoreUpdated = result.gitignoreUpdated; // clear the issues that were fixed scanResult.missing = []; @@ -148,7 +149,7 @@ export function processComparisonFile( } // Keep duplicates for output if not fixed - if (duplicatesFound && (!opts.fix || !fixApplied)) { + if (duplicatesFound && (!opts.fix || !fix.fixApplied)) { if (!scanResult.duplicates) scanResult.duplicates = {}; if (dupsEnv.length > 0) scanResult.duplicates.env = dupsEnv; if (dupsEx.length > 0) scanResult.duplicates.example = dupsEx; @@ -162,10 +163,7 @@ export function processComparisonFile( duplicatesFound, dupsEnv, dupsEx, - fixApplied, - removedDuplicates, - addedEnv, - gitignoreUpdated, + fix, exampleFull, uppercaseWarnings, expireWarnings, @@ -184,10 +182,7 @@ export function processComparisonFile( duplicatesFound, dupsEnv, dupsEx, - fixApplied, - removedDuplicates, - addedEnv, - gitignoreUpdated, + fix, exampleFull, uppercaseWarnings, expireWarnings, diff --git a/src/ui/shared/printAutoFix.ts b/src/ui/shared/printAutoFix.ts index c3efad9..a421425 100644 --- a/src/ui/shared/printAutoFix.ts +++ b/src/ui/shared/printAutoFix.ts @@ -1,34 +1,21 @@ import chalk from 'chalk'; - -/** - * Result of the auto-fix operation to be printed to the user. - */ -interface AutoFixResult { - /** List of duplicate keys removed from the environment file */ - removedDuplicates: string[]; - /** List of missing keys added to the environment file */ - addedEnv: string[]; -} +import type { FixContext } from '../../config/types.js'; /** * Prints the result of the auto-fix operation. - * @param changed - Whether any changes were made. * @param result - The result of the auto-fix operation. * @param envName - The name of the environment file. * @param json - Whether to output in JSON format. - * @param gitignoreUpdated - Whether the .gitignore file was updated to ignore the environment file. * @returns void */ export function printAutoFix( - changed: boolean, - result: AutoFixResult, + result: FixContext, envName: string, json: boolean, - gitignoreUpdated: boolean, ): void { if (json) return; - if (changed) { + if (result.fixApplied) { console.log(chalk.green('✅ Auto-fix applied:')); if (result.removedDuplicates.length) { console.log( @@ -44,7 +31,7 @@ export function printAutoFix( ), ); } - if (gitignoreUpdated) { + if (result.gitignoreUpdated) { console.log(chalk.green(` - Added ${envName} to .gitignore`)); } } else { diff --git a/test/unit/commands/compare.test.ts b/test/unit/commands/compare.test.ts index 7c4a13c..421e83e 100644 --- a/test/unit/commands/compare.test.ts +++ b/test/unit/commands/compare.test.ts @@ -384,11 +384,14 @@ describe('compareMany', () => { }); expect(mockPrintAutoFix).toHaveBeenCalledWith( - true, - expect.any(Object), + { + fixApplied: true, + removedDuplicates: [], + addedEnv: ['MISSING_KEY'], + gitignoreUpdated: false, + }, '.env', false, - false, ); });