61 lines
1.7 KiB
TypeScript
61 lines
1.7 KiB
TypeScript
import fs from 'fs-extra';
|
||
|
||
const colors = {
|
||
reset: '\x1b[0m',
|
||
bright: '\x1b[1m',
|
||
green: '\x1b[32m',
|
||
blue: '\x1b[34m',
|
||
yellow: '\x1b[33m',
|
||
red: '\x1b[31m',
|
||
cyan: '\x1b[36m'
|
||
} as const;
|
||
|
||
type Color = keyof typeof colors;
|
||
|
||
let _logFilePath: string | null = null;
|
||
|
||
/** Initialises the generation log file, overwriting any previous run. */
|
||
export function initGenerationLog(filePath: string): void {
|
||
_logFilePath = filePath;
|
||
fs.writeFileSync(filePath, `Generation log — ${new Date().toISOString()}\n${'='.repeat(60)}\n`);
|
||
}
|
||
|
||
/** Writes a detailed entry to the generation log file (not to console). */
|
||
export function logDetail(category: string, message: string): void {
|
||
if (!_logFilePath) return;
|
||
const line = `[${new Date().toISOString()}] [${category.toUpperCase().padEnd(8)}] ${message}\n`;
|
||
fs.appendFileSync(_logFilePath, line);
|
||
}
|
||
|
||
/** Prints a console message with the given ANSI colour. */
|
||
export function log(message: string, color: Color = 'reset'): void {
|
||
console.log(`${colors[color]}${message}${colors.reset}`);
|
||
}
|
||
|
||
/** Prints a success message (green). */
|
||
export function logSuccess(message: string): void {
|
||
log(`✅ ${message}`, 'green');
|
||
}
|
||
|
||
/** Prints an informational message (blue). */
|
||
export function logInfo(message: string): void {
|
||
log(`ℹ️ ${message}`, 'blue');
|
||
}
|
||
|
||
/** Prints a warning message (yellow). */
|
||
export function logWarning(message: string): void {
|
||
log(`⚠️ ${message}`, 'yellow');
|
||
}
|
||
|
||
/** Prints an error message (red). */
|
||
export function logError(message: string): void {
|
||
log(`❌ ${message}`, 'red');
|
||
}
|
||
|
||
/** Prints a step/stage header (cyan). */
|
||
export function logStep(message: string): void {
|
||
log(`\n🚀 ${message}`, 'cyan');
|
||
}
|
||
|
||
export { colors };
|