mira-consciousness
Version:
Memory & Intelligence Retention Archive - Preserving The Spark
169 lines โข 8.17 kB
JavaScript
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