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
51 changes: 51 additions & 0 deletions packages/cli/src/baseline/writeBaseline.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import type { ExitResult, ScanResult } from '../config/types.js';
import {
printBaselineWritten,
printBaselineError,
} from '../ui/scan/printBaseline.js';
import {
BASELINE_FILE,
collectBaselineEntries,
writeBaselineFile,
} from './scanBaseline.js';

/**
* Writes the current scan state as a baseline file and reports the outcome.
* Returns exitWithError: false so the caller exits cleanly.
* @param scanResult The full scan result to convert into baseline entries and write to disk
* @param cwd The current working directory to resolve the baseline file from
* @param asJson Whether to output results in JSON format (true) or console format (false)
* @returns An object indicating whether to exit with an error code (always false for this function)
*/
export async function writeBaseline(
scanResult: ScanResult,
cwd: string,
asJson: boolean,
): Promise<ExitResult> {
const entries = collectBaselineEntries(scanResult);
let filePath: string;
try {
filePath = await writeBaselineFile(cwd, entries);
} catch (err) {
const message = err instanceof Error ? err.message : String(err);
if (asJson) {
console.log(JSON.stringify({ ok: false, error: message }, null, 2));
} else {
printBaselineError(message);
}
return { exitWithError: true };
}

if (asJson) {
console.log(
JSON.stringify(
{ file: BASELINE_FILE, warningsStored: entries.length },
null,
2,
),
);
} else {
printBaselineWritten(entries.length, filePath);
}
return { exitWithError: false };
}
49 changes: 1 addition & 48 deletions packages/cli/src/commands/scanUsage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,10 @@ import { DEFAULT_EXAMPLE_FILE } from '../config/constants.js';
import { checkGitignoreStatus } from '../services/git.js';
import { promptNoEnvScenario } from './prompts/promptNoEnvScenario.js';
import {
printBaselineWritten,
printBaselineError,
} from '../ui/scan/printBaseline.js';
import {
BASELINE_FILE,
collectBaselineEntries,
writeBaselineFile,
loadBaselineFile,
applyBaselineEntries,
} from '../baseline/scanBaseline.js';
import { writeBaseline } from '../baseline/writeBaseline.js';

/**
* Scans the codebase for environment variable usage and compares it with
Expand Down Expand Up @@ -196,47 +190,6 @@ export async function scanUsage(opts: ScanUsageOptions): Promise<ExitResult> {
return { exitWithError: result.exitWithError };
}

/**
* Writes the current scan state as a baseline file and reports the outcome.
* Returns exitWithError: false so the caller exits cleanly.
* @param scanResult The full scan result to convert into baseline entries and write to disk
* @param cwd The current working directory to resolve the baseline file from
* @param asJson Whether to output results in JSON format (true) or console format (false)
* @returns An object indicating whether to exit with an error code (always false for this function)
*/
async function writeBaseline(
scanResult: ScanResult,
cwd: string,
asJson: boolean,
): Promise<ExitResult> {
const entries = collectBaselineEntries(scanResult);
let filePath: string;
try {
filePath = await writeBaselineFile(cwd, entries);
} catch (err) {
const message = err instanceof Error ? err.message : String(err);
if (asJson) {
console.log(JSON.stringify({ ok: false, error: message }, null, 2));
} else {
printBaselineError(message);
}
return { exitWithError: true };
}

if (asJson) {
console.log(
JSON.stringify(
{ file: BASELINE_FILE, warningsStored: entries.length },
null,
2,
),
);
} else {
printBaselineWritten(entries.length, filePath);
}
return { exitWithError: false };
}

/**
* Recalculates statistics for a scan result after filtering usages.
* @param scanResult The current scan result
Expand Down