Skip to content

Commit 54d5af6

Browse files
authored
feat: add flush command (#5)
1 parent c4b2390 commit 54d5af6

1 file changed

Lines changed: 45 additions & 0 deletions

File tree

index.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,44 @@ program
429429
}
430430
});
431431

432+
program
433+
.command('flush [service]')
434+
.description('Clear log file contents without deleting files')
435+
.action(async (service) => {
436+
try {
437+
const target = service || '';
438+
let flushedCount = 0;
439+
440+
const logFiles = readdirSync(LOG_DIR).filter((file) => {
441+
return (!target || file.startsWith(`${target}_`)) && file.endsWith('.log');
442+
});
443+
444+
if (logFiles.length === 0) {
445+
console.log('No log files found to flush.');
446+
return;
447+
}
448+
449+
for (const file of logFiles) {
450+
const filePath = path.join(LOG_DIR, file);
451+
try {
452+
const stats = await fs.stat(filePath);
453+
const sizeMB = (stats.size / 1024 / 1024).toFixed(2);
454+
await fs.writeFile(filePath, '');
455+
flushedCount++;
456+
console.log(`Flushed: ${file} (cleared ${sizeMB} MB)`);
457+
} catch (error) {
458+
console.error(`Failed to flush ${file}: ${error}`);
459+
}
460+
}
461+
462+
if (flushedCount > 0) {
463+
console.log(`\nTotal: Flushed ${flushedCount} log file(s)`);
464+
}
465+
} catch (error) {
466+
console.error(`Error in flush command: ${error}`);
467+
}
468+
});
469+
432470
const rotateCmd = new Command('rotate')
433471
.description('Manage log rotation');
434472

@@ -638,6 +676,13 @@ async function hasRunningProcesses(): Promise<boolean> {
638676

639677
program.hook('preAction', async (thisCommand) => {
640678
try {
679+
// Skip config loading for commands that don't need app config
680+
const isRotateCommand = process.argv.includes('rotate');
681+
const isFlushCommand = process.argv.includes('flush');
682+
if (isRotateCommand || isFlushCommand) {
683+
return;
684+
}
685+
641686
const configFile = thisCommand.opts().config;
642687

643688
// Try to find config file in different locations

0 commit comments

Comments
 (0)