import { CodeParser } from '../utils/parser.js';
import { FileOperations } from '../utils/file.js';
export interface CleanupResult {
filePath: string;
success: boolean;
removedCount: number;
linesRemoved: number[];
error?: string;
}
export class DebugCleaner {
private parser: CodeParser;
private fileOps: FileOperations;
constructor() {
this.parser = new CodeParser();
this.fileOps = new FileOperations();
}
/**
* Remove all debug blocks from a single file
*/
cleanupFile(filePath: string): CleanupResult {
// Check if file exists
if (!this.fileOps.fileExists(filePath)) {
return {
filePath,
success: false,
removedCount: 0,
linesRemoved: [],
error: 'File does not exist'
};
}
// Create backup before cleanup
const backupPath = this.fileOps.createBackup(filePath);
if (backupPath) {
console.error(`[Backup] Created: ${backupPath}`);
}
// Remove debug blocks
const result = this.parser.removeDebugBlocks(filePath);
return {
filePath,
success: result.success,
removedCount: result.removedCount,
linesRemoved: result.linesRemoved,
error: result.success ? undefined : 'Failed to remove debug blocks'
};
}
/**
* Remove debug blocks from multiple files
*/
cleanupFiles(filePaths: string[]): CleanupResult[] {
return filePaths.map(filePath => this.cleanupFile(filePath));
}
/**
* Remove debug blocks from all files in a directory
*/
cleanupDirectory(dirPath: string): CleanupResult[] {
const filesWithBlocks = this.parser.findFilesWithDebugBlocks(dirPath);
return this.cleanupFiles(filesWithBlocks);
}
/**
* Preview what would be removed (without actually removing)
*/
previewCleanup(filePath: string): {
debugBlocks: Array<{
startLine: number;
endLine: number;
content: string;
}>;
totalBlocks: number;
totalLines: number;
} {
const debugBlocks = this.parser.findDebugBlocks(filePath);
const totalLines = debugBlocks.reduce((sum, block) => {
return sum + (block.endLine - block.startLine + 1);
}, 0);
return {
debugBlocks,
totalBlocks: debugBlocks.length,
totalLines
};
}
/**
* Get cleanup summary for multiple files
*/
getCleanupSummary(filePaths: string[]): {
totalFiles: number;
totalBlocks: number;
filesWithBlocks: string[];
} {
const filesWithBlocks: string[] = [];
let totalBlocks = 0;
for (const filePath of filePaths) {
const count = this.parser.countDebugBlocks(filePath);
if (count > 0) {
filesWithBlocks.push(filePath);
totalBlocks += count;
}
}
return {
totalFiles: filePaths.length,
totalBlocks,
filesWithBlocks
};
}
/**
* Restore from backup
*/
restoreFromBackup(backupPath: string): CleanupResult {
// Extract original path from backup path
const originalPath = backupPath.replace(/\.backup\.\d+$/, '');
const success = this.fileOps.restoreBackup(backupPath, originalPath);
return {
filePath: originalPath,
success,
removedCount: 0,
linesRemoved: [],
error: success ? undefined : 'Failed to restore from backup'
};
}
}