Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions src/commands/compare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions src/commands/scanUsage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@ export async function scanUsage(opts: ScanUsageOptions): Promise<ExitResult> {
} 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;
}
Expand Down
8 changes: 8 additions & 0 deletions src/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
17 changes: 2 additions & 15 deletions src/services/printScanResult.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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.
Expand Down Expand Up @@ -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,
);
}

Expand Down
37 changes: 16 additions & 21 deletions src/services/processComparisonFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import type {
ComparisonFile,
ExpireWarning,
InconsistentNamingWarning,
FixContext,
} from '../config/types.js';

/**
Expand All @@ -29,10 +30,7 @@ interface ProcessComparisonResult {
duplicatesFound: boolean;
dupsEnv: Duplicate[];
dupsEx: Duplicate[];
fixApplied: boolean;
removedDuplicates: string[];
addedEnv: string[];
gitignoreUpdated: boolean;
fix: FixContext;
exampleFull?: Record<string, string> | undefined;
uppercaseWarnings?: UppercaseWarning[];
expireWarnings?: ExpireWarning[];
Expand All @@ -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<string, string> | 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) {
Expand Down Expand Up @@ -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 = [];
Expand All @@ -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;
Expand All @@ -162,10 +163,7 @@ export function processComparisonFile(
duplicatesFound,
dupsEnv,
dupsEx,
fixApplied,
removedDuplicates,
addedEnv,
gitignoreUpdated,
fix,
exampleFull,
uppercaseWarnings,
expireWarnings,
Expand All @@ -184,10 +182,7 @@ export function processComparisonFile(
duplicatesFound,
dupsEnv,
dupsEx,
fixApplied,
removedDuplicates,
addedEnv,
gitignoreUpdated,
fix,
exampleFull,
uppercaseWarnings,
expireWarnings,
Expand Down
21 changes: 4 additions & 17 deletions src/ui/shared/printAutoFix.ts
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -44,7 +31,7 @@ export function printAutoFix(
),
);
}
if (gitignoreUpdated) {
if (result.gitignoreUpdated) {
console.log(chalk.green(` - Added ${envName} to .gitignore`));
}
} else {
Expand Down
9 changes: 6 additions & 3 deletions test/unit/commands/compare.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,11 +384,14 @@ describe('compareMany', () => {
});

expect(mockPrintAutoFix).toHaveBeenCalledWith(
true,
expect.any(Object),
{
fixApplied: true,
removedDuplicates: [],
addedEnv: ['MISSING_KEY'],
gitignoreUpdated: false,
},
'.env',
false,
false,
);
});

Expand Down
Loading