Skip to content
Open
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
64 changes: 23 additions & 41 deletions src/services/printScanResult.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,52 +47,35 @@ export function printScanResult(
printHeader(comparedAgainst);

// Show stats if requested
printStats(scanResult.stats, isJson, opts.showStats ?? true);
if (opts.showStats ?? true) {
printStats(scanResult.stats, true);
}

// Missing variables (used in code but not in env file)
if (
printMissing(
scanResult.missing,
scanResult.used,
comparedAgainst,
opts.isCiMode ?? false,
isJson,
)
) {
if (printMissing(scanResult.missing, scanResult.used, comparedAgainst)) {
exitWithError = true;
}

if (scanResult.frameworkWarnings && scanResult.frameworkWarnings.length > 0) {
printFrameworkWarnings(scanResult.frameworkWarnings, isJson);
if (scanResult.frameworkWarnings) {
printFrameworkWarnings(scanResult.frameworkWarnings);
}

if (scanResult.uppercaseWarnings && scanResult.uppercaseWarnings.length > 0) {
printUppercaseWarning(
scanResult.uppercaseWarnings,
comparedAgainst,
isJson,
);
if (scanResult.uppercaseWarnings) {
printUppercaseWarning(scanResult.uppercaseWarnings, comparedAgainst);
}

if (
scanResult.inconsistentNamingWarnings &&
scanResult.inconsistentNamingWarnings.length > 0
) {
printInconsistentNamingWarning(
scanResult.inconsistentNamingWarnings,
isJson,
);
if (scanResult.inconsistentNamingWarnings) {
printInconsistentNamingWarning(scanResult.inconsistentNamingWarnings);
}

printExampleWarnings(scanResult.exampleWarnings ?? [], isJson);
if (scanResult.exampleWarnings) {
printExampleWarnings(scanResult.exampleWarnings);
}

// Unused
printUnused(
scanResult.unused,
comparedAgainst,
opts.showUnused ?? false,
isJson,
);
if (opts.showUnused ?? true) {
printUnused(scanResult.unused, comparedAgainst);
}

// Duplicates
printDuplicates(
Expand All @@ -104,10 +87,13 @@ export function printScanResult(
);

// Print potential secrets found
printSecrets(scanResult.secrets ?? [], isJson);

if (opts.secrets) {
printSecrets(scanResult.secrets);
}
// Console log usage warning
printConsolelogWarning(scanResult.logged ?? [], isJson);
if (scanResult.logged) {
printConsolelogWarning(scanResult.logged);
}

// Expiration warnings
printExpireWarnings(scanResult.expireWarnings ?? [], isJson);
Expand Down Expand Up @@ -184,11 +170,7 @@ export function printScanResult(
}

if (opts.fix && fixContext) {
printAutoFix(
fixContext,
comparedAgainst || DEFAULT_ENV_FILE,
isJson,
);
printAutoFix(fixContext, comparedAgainst || DEFAULT_ENV_FILE, isJson);
}

// Health score
Expand Down
7 changes: 1 addition & 6 deletions src/ui/scan/printConsolelogWarning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,12 @@
* Print environment variables that were logged using console.log / warn / error.
*
* @param logged - List of EnvUsage entries where isLogged=true
* @param json - Whether JSON output is enabled
* @returns true if anything was printed
*/
export function printConsolelogWarning(
logged: EnvUsage[],
json: boolean,
): boolean {
if (json) return false;
export function printConsolelogWarning(logged: EnvUsage[]): boolean {
if (!logged || logged.length === 0) return false;

console.log(chalk.yellow(`⚠️ Environment variables logged to console:`));

Check warning on line 14 in src/ui/scan/printConsolelogWarning.ts

View workflow job for this annotation

GitHub Actions / build-test-lint

Strings must use singlequote

const grouped = logged.reduce((acc: VariableUsages, entry) => {
if (!acc[entry.variable]) acc[entry.variable] = [];
Expand Down
7 changes: 0 additions & 7 deletions src/ui/scan/printExampleWarnings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,12 @@ import type { ExampleSecretWarning } from '../../config/types.js';
/**
* Prints example file secret warnings to the console.
* @param warnings - List of example file secret warnings
* @param json - Whether to output in JSON format
*/
export function printExampleWarnings(
warnings: ExampleSecretWarning[],
json: boolean,
): void {
if (!warnings || warnings.length === 0) return;

if (json) {
console.log(JSON.stringify({ exampleWarnings: warnings }, null, 2));
return;
}

console.log(chalk.yellow('🚨 Potential real secrets found in .env.example:'));
for (const w of warnings) {
console.log(
Expand Down
11 changes: 1 addition & 10 deletions src/ui/scan/printFrameworkWarnings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,10 @@ const FRAMEWORK_LABELS: Record<DetectedFramework, string> = {
/**
* Prints environment variable usage warnings to the console.
* @param warnings - List of environment variable warnings
* @param json - Whether to output in JSON format
*/
export function printFrameworkWarnings(
warnings: FrameworkWarning[],
json: boolean,
): void {
export function printFrameworkWarnings(warnings: FrameworkWarning[]): void {
if (!warnings || warnings.length === 0) return;

if (json) {
console.log(JSON.stringify({ frameworkWarnings: warnings }, null, 2));
return;
}

// Deduplicate warnings by variable + file + line + reason
const uniqueWarnings = Array.from(
new Map(
Expand Down
14 changes: 3 additions & 11 deletions src/ui/scan/printInconsistentNamingWarning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,20 @@ import type { InconsistentNamingWarning } from '../../config/types.js';
/**
* Prints warnings about inconsistent naming patterns in environment variables.
* @param warnings Array of inconsistent naming warnings
* @param isJson Whether to output in JSON format
* @returns void
*/
export function printInconsistentNamingWarning(
warnings: InconsistentNamingWarning[],
isJson: boolean,
) {
if (isJson || warnings.length === 0) {
if (warnings.length === 0) {
return;
}

console.log(chalk.yellow('⚠️ Inconsistent naming found:'));

for (const { key1, key2, suggestion } of warnings) {
console.log(
chalk.yellow(
` - ${chalk.cyan(key1)} ↔ ${chalk.cyan(key2)}`,
),
);
console.log(
chalk.gray(` Suggested name: ${suggestion}`),
);
console.log(chalk.yellow(` - ${chalk.cyan(key1)} ↔ ${chalk.cyan(key2)}`));
console.log(chalk.gray(` Suggested name: ${suggestion}`));
}

console.log();
Expand Down
18 changes: 7 additions & 11 deletions src/ui/scan/printMissing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,13 @@ import { normalizePath } from '../../core/helpers/normalizePath.js';
* @param missing - List of missing variables
* @param used - All usages found in the codebase
* @param comparedAgainst - Name of the env file or example file
* @param isCiMode - Whether we are in CI mode (extra error message)
* @param json - Whether to output in JSON format
* @returns true if any missing variables were printed
*/
export function printMissing(
missing: string[],
used: EnvUsage[],
comparedAgainst: string,
isCiMode: boolean,
json: boolean,
): boolean {
if (json) return false;
if (missing.length === 0) return false;

const fileType = comparedAgainst || 'environment file';
Expand All @@ -33,8 +28,11 @@ export function printMissing(
}, {});

// Group by file first
const byFile = new Map<string, Array<{ variable: string; usage: EnvUsage }>>();

const byFile = new Map<
string,
Array<{ variable: string; usage: EnvUsage }>
>();

for (const [variable, usages] of Object.entries(grouped)) {
for (const usage of usages) {
const file = normalizePath(usage.file);
Expand All @@ -46,11 +44,9 @@ export function printMissing(
// Print grouped by file
for (const [file, items] of byFile) {
console.log(chalk.bold(` ${file}`));

for (const { variable, usage } of items) {
console.log(
chalk.red(` ${variable}: Line ${usage.line}`),
);
console.log(chalk.red(` ${variable}: Line ${usage.line}`));
console.log(chalk.red.dim(` ${usage.context.trim()}`));
}
}
Expand Down
4 changes: 1 addition & 3 deletions src/ui/scan/printSecrets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,9 @@ function getSeverityLabel(severity: SecretFinding['severity']): string {
/**
* Print potential secrets detected in the codebase.
* @param secrets - List of secret findings
* @param json - Whether to output in JSON format
* @returns void
*/
export function printSecrets(secrets: SecretFinding[], json: boolean): void {
if (json) return;
export function printSecrets(secrets: SecretFinding[]): void {
if (!secrets || secrets.length === 0) return;

// Sort by severity (high -> medium -> low)
Expand Down
3 changes: 1 addition & 2 deletions src/ui/scan/printStats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ import type { ScanStats } from '../../config/types.js';
*/
export function printStats(
stats: ScanStats,
json: boolean,
showStats: boolean,
): void {
if (json || !showStats) return;
if (!showStats) return;
console.log();
console.log(chalk.magenta('📊 Scan Statistics:'));
console.log(chalk.magenta.dim(` Files scanned: ${stats.filesScanned}`));
Expand Down
9 changes: 1 addition & 8 deletions src/ui/scan/printUnused.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,8 @@ import chalk from 'chalk';
* @param unused - Array of unused variable names
* @param comparedAgainst - File name (.env eller andet)
* @param showUnused - Whether unused should be shown at all
* @param json - Whether to output in JSON format
*/
export function printUnused(
unused: string[],
comparedAgainst: string,
showUnused: boolean,
json: boolean,
): void {
if (json || !showUnused) return;
export function printUnused(unused: string[], comparedAgainst: string): void {
if (unused.length === 0) return;

const fileType = comparedAgainst || 'environment file';
Expand Down
4 changes: 1 addition & 3 deletions src/ui/scan/printUppercaseWarning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ import type { UppercaseWarning } from '../../config/types.js';
*
* @param warnings - List of non-uppercase env keys
* @param comparedAgainst - The .env file name being checked
* @param json - Whether JSON output is enabled
*/
export function printUppercaseWarning(
warnings: UppercaseWarning[],
comparedAgainst: string,
json: boolean,
): void {
if (json || warnings.length === 0) return;
if (warnings.length === 0) return;

console.log(
chalk.yellow(
Expand Down
Loading