import { OptionValues } from 'commander'; import { readFileSync, readdirSync, statSync } from 'fs'; import { join } from 'path'; import { PathDiscovery } from '../services/path-discovery.js'; // 1.1 ==================================== async function showLog(logPath: string, logType: string, tail: number): Promise { // 1.2 ==================================== try { const content = readFileSync(logPath, 'utf8'); const lines = content.split('\n').filter(line => line.trim()); const displayLines = lines.slice(-tail); console.log(`📋 ${logType} Logs (last ${tail} lines):`); console.log(` File: ${logPath}`); console.log(''); // 1.3 ==================================== if (displayLines.length === 0) { console.log(' No log entries found'); // ======================================= } else { displayLines.forEach(line => { console.log(` ${line}`); }); } // ======================================= console.log(''); // ======================================= } catch (error) { // 1.4 ==================================== console.log(`❌ Could not read ${logType.toLowerCase()} log: ${logPath}`); // ======================================= } // ======================================= } // 2.1 ==================================== export async function logs(options: OptionValues = {}): Promise { // 2.2 ==================================== const logsDir = PathDiscovery.getLogsDirectory(); const tail = parseInt(options.tail) || 20; // ======================================= // Find most recent log file try { const files = readdirSync(logsDir); const logFiles = files .filter(f => f.startsWith('claude-mem-') && f.endsWith('.log')) .map(f => ({ name: f, path: join(logsDir, f), mtime: statSync(join(logsDir, f)).mtime })) .sort((a, b) => b.mtime.getTime() - a.mtime.getTime()); if (logFiles.length === 0) { console.log('❌ No log files found in ~/.claude-mem/logs/'); return; } // Show most recent log await showLog(logFiles[0].path, 'Most Recent', tail); if (options.all && logFiles.length > 1) { console.log(`📚 Found ${logFiles.length} total log files`); } } catch (error) { console.log('❌ Could not read logs directory: ~/.claude-mem/logs/'); console.log(' Run a compression first to generate logs'); } // 2.5 ==================================== if (options.follow) { console.log('Following logs... (Press Ctrl+C to stop)'); // Basic follow implementation - would need more sophisticated watching in real usage setInterval(() => { // This would need proper file watching implementation }, 1000); } // ======================================= // ======================================= }