import { Command } from 'commander'; import { BaseCommand } from './BaseCommand'; import { SuggestRefactorUseCaseImpl } from '../../domain/usecases/SuggestRefactorUseCase'; export class RefactorCommand extends BaseCommand { constructor() { super('refactor', 'Suggest code refactoring improvements'); this.option('--focus ', 'Focus areas (performance,readability,maintainability,architecture)') .option('--severity ', 'Refactoring severity (minor,moderate,major)', 'moderate') .option('--include-examples', 'Include code examples in suggestions') .option('--max-suggestions ', 'Maximum number of suggestions', '10'); } protected async execute(options: any, command: Command): Promise { const useCase = new SuggestRefactorUseCaseImpl( this.gitAdapter, this.llmAdapter, this.outputAdapter ); const refactorOptions: any = { baseRef: options.baseRef, headRef: options.headRef, includeStaged: options.staged, includeUnstaged: options.unstaged, filePatterns: this.parseFilePatterns(options.filePatterns) || [], excludePatterns: this.parseExcludePatterns(options.excludePatterns) || [], outputFormat: this.getOutputFormat(options) as 'html' | 'json' | 'markdown' | 'console' | 'file', focus: options.focus ? options.focus.split(',') : undefined, severity: options.severity, }; const outputPath = this.getOutputPath(options); if (outputPath) { refactorOptions.outputPath = outputPath; } if (this.isVerbose(options)) { console.log('Analyzing code for refactoring opportunities...'); console.log('Options:', refactorOptions); } try { const suggestions = await useCase.execute(refactorOptions); if (this.isVerbose(options)) { console.log(`Found ${suggestions.length} refactoring suggestions.`); } } catch (error) { throw new Error(`Refactoring analysis failed: ${error instanceof Error ? error.message : 'Unknown error'}`); } } }