UNPKG

mira-consciousness

Version:

Memory & Intelligence Retention Archive - Preserving The Spark

169 lines โ€ข 8.17 kB
import { Command } from 'commander'; import chalk from 'chalk'; import ora from 'ora'; import { CleanupAnalyzer, CleanupCategory } from '../analyzers/CleanupAnalyzer.js'; export function createCleanupAnalyzeCommand() { return new Command('cleanup-analyze') .alias('cleanup') .description('๐Ÿงน Analyze project for files that may need cleanup') .option('--verbose', 'Show detailed information for each issue') .option('--category <type>', 'Filter by category (docs, backup, temp, etc.)') .option('--safe-only', 'Only show files safe to delete') .option('--commands', 'Generate cleanup commands') .option('--format <type>', 'Output format (text|json)', 'text') .action(async (options) => { const projectRoot = process.cwd(); const spinner = ora('๐Ÿงน Analyzing project for cleanup opportunities...').start(); try { const analyzer = new CleanupAnalyzer(projectRoot); const result = await analyzer.analyze(); spinner.succeed('Analysis complete'); if (options.format === 'json') { console.log(JSON.stringify(result, null, 2)); return; } // Display header console.log(chalk.cyan('\n๐Ÿงน Cleanup Analysis Report')); console.log(chalk.gray('โ•'.repeat(60))); // Display score const scoreColor = result.score >= 80 ? chalk.green : result.score >= 60 ? chalk.yellow : chalk.red; console.log(scoreColor(`\n๐Ÿ“Š Cleanliness Score: ${result.score}/100`)); // Display summary console.log(chalk.white(`\n๐Ÿ“ Total issues found: ${result.issues.length}`)); console.log(chalk.white(`๐Ÿ’พ Space potentially wasted: ${formatBytes(result.totalSizeWasted)}`)); // Display category breakdown if (result.categorySummary.size > 0) { console.log(chalk.cyan('\n๐Ÿ“ˆ Issues by Category:')); const sortedCategories = Array.from(result.categorySummary.entries()) .sort((a, b) => b[1] - a[1]); for (const [category, count] of sortedCategories) { if (options.category && !category.includes(options.category)) continue; const icon = getCategoryIcon(category); const name = formatCategoryName(category); console.log(chalk.white(` ${icon} ${name}: ${count} files`)); } } // Display issues if (!options.safeOnly || result.issues.some(i => i.safeToDelete)) { console.log(chalk.cyan('\n๐Ÿ” Detailed Findings:')); const issuesToShow = options.safeOnly ? result.issues.filter(i => i.safeToDelete) : result.issues; const groupedIssues = groupIssuesByCategory(issuesToShow); for (const [category, issues] of groupedIssues) { if (options.category && !category.includes(options.category)) continue; console.log(chalk.yellow(`\n${getCategoryIcon(category)} ${formatCategoryName(category)}:`)); for (const issue of issues.slice(0, options.verbose ? undefined : 5)) { const ageStr = formatAge(issue.lastModified); const sizeStr = formatBytes(issue.size); console.log(chalk.gray(` ๐Ÿ“„ ${issue.file}`)); console.log(chalk.gray(` ${issue.reason}`)); console.log(chalk.blue(` ๐Ÿ’ก ${issue.suggestion}`)); if (options.verbose) { console.log(chalk.gray(` ๐Ÿ“… Last modified: ${ageStr} ago`)); console.log(chalk.gray(` ๐Ÿ’พ Size: ${sizeStr}`)); if (issue.safeToDelete) { console.log(chalk.green(` โœ… Safe to delete`)); } if (issue.properLocation) { console.log(chalk.cyan(` ๐Ÿ“ Suggested location: ${issue.properLocation}`)); } if (issue.relatedFiles?.length) { console.log(chalk.gray(` ๐Ÿ”— Related: ${issue.relatedFiles.join(', ')}`)); } } } if (issues.length > 5 && !options.verbose) { console.log(chalk.gray(` ... and ${issues.length - 5} more`)); } } } // Display recommendations if (result.recommendations.length > 0) { console.log(chalk.cyan('\n๐Ÿ’ก Recommendations:')); for (const rec of result.recommendations) { console.log(chalk.white(` โ€ข ${rec}`)); } } // Display cleanup commands if (options.commands && result.safeCleanupCommands.length > 0) { console.log(chalk.cyan('\n๐Ÿ› ๏ธ Cleanup Commands:')); console.log(chalk.yellow('โš ๏ธ Review before executing!')); console.log(); for (const cmd of result.safeCleanupCommands) { console.log(chalk.gray(cmd)); console.log(); } } // Footer advice console.log(chalk.gray('\n๐Ÿ’ก Tip: Use --commands to generate cleanup scripts')); console.log(chalk.gray('๐Ÿ’ก Tip: Use --safe-only to see only files safe to delete')); console.log(chalk.gray('๐Ÿ’ก Tip: Always review files before deleting!')); } catch (error) { spinner.fail('Analysis failed'); console.error(chalk.red(`\nโŒ Error: ${error instanceof Error ? error.message : error}`)); process.exit(1); } }); } function getCategoryIcon(category) { const icons = { [CleanupCategory.SCATTERED_DOCS]: '๐Ÿ“š', [CleanupCategory.TEST_ARTIFACTS]: '๐Ÿงช', [CleanupCategory.BACKUP_FILES]: '๐Ÿ’พ', [CleanupCategory.TEMP_FILES]: '๐Ÿ—‘๏ธ', [CleanupCategory.VERSION_ITERATIONS]: '๐Ÿ”ข', [CleanupCategory.EDITOR_FILES]: '๐Ÿ“', [CleanupCategory.BUILD_ARTIFACTS]: '๐Ÿ—๏ธ', [CleanupCategory.LOG_FILES]: '๐Ÿ“‹', [CleanupCategory.EXPERIMENTS]: '๐Ÿงช', [CleanupCategory.MEDIA_FILES]: '๐Ÿ–ผ๏ธ', [CleanupCategory.ARCHIVE_FILES]: '๐Ÿ“ฆ', [CleanupCategory.DUPLICATE_CONFIGS]: 'โš™๏ธ', [CleanupCategory.ABANDONED_FILES]: '๐Ÿ•ฐ๏ธ', [CleanupCategory.EMPTY_FILES]: '๐Ÿ“„' }; return icons[category] || '๐Ÿ“'; } function formatCategoryName(category) { return category .split('_') .map(word => word.charAt(0).toUpperCase() + word.slice(1)) .join(' '); } function formatBytes(bytes) { if (bytes === 0) return '0 Bytes'; const k = 1024; const sizes = ['Bytes', 'KB', 'MB', 'GB']; const i = Math.floor(Math.log(bytes) / Math.log(k)); return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]; } function formatAge(date) { const now = new Date(); const diff = now.getTime() - date.getTime(); const days = Math.floor(diff / (1000 * 60 * 60 * 24)); const months = Math.floor(days / 30); const years = Math.floor(days / 365); if (years > 0) return `${years} year${years > 1 ? 's' : ''}`; if (months > 0) return `${months} month${months > 1 ? 's' : ''}`; if (days > 0) return `${days} day${days > 1 ? 's' : ''}`; return 'today'; } function groupIssuesByCategory(issues) { const grouped = new Map(); for (const issue of issues) { const list = grouped.get(issue.category) || []; list.push(issue); grouped.set(issue.category, list); } return grouped; } //# sourceMappingURL=cleanup-analyze.js.map