|
| 1 | +import { logger } from '../utils/logger.js' |
| 2 | +import { GitWorktreeManager } from '../lib/GitWorktreeManager.js' |
| 3 | +import type { CleanupOptions } from '../types/index.js' |
| 4 | + |
| 5 | +/** |
| 6 | + * Input structure for CleanupCommand.execute() |
| 7 | + */ |
| 8 | +export interface CleanupCommandInput { |
| 9 | + identifier?: string |
| 10 | + options: CleanupOptions |
| 11 | +} |
| 12 | + |
| 13 | +/** |
| 14 | + * Parsed and validated cleanup command input |
| 15 | + * Mode determines which cleanup operation to perform |
| 16 | + */ |
| 17 | +export interface ParsedCleanupInput { |
| 18 | + mode: 'list' | 'single' | 'issue' | 'all' |
| 19 | + identifier?: string |
| 20 | + issueNumber?: number |
| 21 | + branchName?: string |
| 22 | + originalInput?: string |
| 23 | + options: CleanupOptions |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * Manages cleanup command execution with option parsing and validation |
| 28 | + * Follows the command pattern established by StartCommand |
| 29 | + * |
| 30 | + * This implementation handles ONLY parsing, validation, and mode determination. |
| 31 | + * Actual cleanup operations are deferred to subsequent sub-issues. |
| 32 | + */ |
| 33 | +export class CleanupCommand { |
| 34 | + // Will be used in subsequent sub-issues for actual cleanup operations |
| 35 | + // @ts-expect-error - Intentionally unused until sub-issues 2-5 implement cleanup operations |
| 36 | + private readonly gitWorktreeManager: GitWorktreeManager |
| 37 | + |
| 38 | + constructor(gitWorktreeManager?: GitWorktreeManager) { |
| 39 | + this.gitWorktreeManager = gitWorktreeManager ?? new GitWorktreeManager() |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Main entry point for the cleanup command |
| 44 | + * Parses input, validates options, and determines operation mode |
| 45 | + */ |
| 46 | + public async execute(input: CleanupCommandInput): Promise<void> { |
| 47 | + try { |
| 48 | + // Step 1: Parse input and determine mode |
| 49 | + const parsed = this.parseInput(input) |
| 50 | + |
| 51 | + // Step 2: Validate option combinations |
| 52 | + this.validateInput(parsed) |
| 53 | + |
| 54 | + // Step 3: Log what mode was determined (for now - actual operations in later issues) |
| 55 | + logger.info(`Cleanup mode: ${parsed.mode}`) |
| 56 | + if (parsed.mode === 'list') { |
| 57 | + logger.info('Would list all worktrees') |
| 58 | + } else if (parsed.mode === 'all') { |
| 59 | + logger.info('Would remove all worktrees') |
| 60 | + } else if (parsed.mode === 'issue') { |
| 61 | + logger.info(`Would cleanup worktrees for issue #${parsed.issueNumber}`) |
| 62 | + } else if (parsed.mode === 'single') { |
| 63 | + logger.info(`Would cleanup worktree: ${parsed.branchName}`) |
| 64 | + } |
| 65 | + |
| 66 | + // Actual cleanup operations will be implemented in subsequent sub-issues: |
| 67 | + // - Sub-issue #2: List functionality |
| 68 | + // - Sub-issue #3: Single worktree removal |
| 69 | + // - Sub-issue #4: Issue-based cleanup |
| 70 | + // - Sub-issue #5: Bulk cleanup (all) |
| 71 | + |
| 72 | + logger.success('Command parsing and validation successful') |
| 73 | + } catch (error) { |
| 74 | + if (error instanceof Error) { |
| 75 | + logger.error(`${error.message}`) |
| 76 | + } else { |
| 77 | + logger.error('An unknown error occurred') |
| 78 | + } |
| 79 | + throw error |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Parse input to determine cleanup mode and extract relevant data |
| 85 | + * Implements auto-detection: numeric input = issue number, non-numeric = branch name |
| 86 | + * |
| 87 | + * @private |
| 88 | + */ |
| 89 | + private parseInput(input: CleanupCommandInput): ParsedCleanupInput { |
| 90 | + const { identifier, options } = input |
| 91 | + |
| 92 | + // Trim identifier if present |
| 93 | + const trimmedIdentifier = identifier?.trim() ?? undefined |
| 94 | + |
| 95 | + // Mode: List (takes priority - it's informational only) |
| 96 | + if (options.list) { |
| 97 | + const result: ParsedCleanupInput = { |
| 98 | + mode: 'list', |
| 99 | + options |
| 100 | + } |
| 101 | + if (trimmedIdentifier) { |
| 102 | + result.identifier = trimmedIdentifier |
| 103 | + } |
| 104 | + return result |
| 105 | + } |
| 106 | + |
| 107 | + // Mode: All (remove everything) |
| 108 | + if (options.all) { |
| 109 | + const result: ParsedCleanupInput = { |
| 110 | + mode: 'all', |
| 111 | + options |
| 112 | + } |
| 113 | + if (trimmedIdentifier) { |
| 114 | + result.identifier = trimmedIdentifier |
| 115 | + } |
| 116 | + if (options.issue !== undefined) { |
| 117 | + result.issueNumber = options.issue |
| 118 | + } |
| 119 | + return result |
| 120 | + } |
| 121 | + |
| 122 | + // Mode: Explicit issue number via --issue flag |
| 123 | + if (options.issue !== undefined) { |
| 124 | + // Need to determine if identifier is branch or numeric to set branchName |
| 125 | + if (trimmedIdentifier) { |
| 126 | + const numericPattern = /^[0-9]+$/ |
| 127 | + if (!numericPattern.test(trimmedIdentifier)) { |
| 128 | + // Identifier is a branch name with explicit --issue flag |
| 129 | + return { |
| 130 | + mode: 'issue', |
| 131 | + issueNumber: options.issue, |
| 132 | + branchName: trimmedIdentifier, |
| 133 | + identifier: trimmedIdentifier, |
| 134 | + originalInput: trimmedIdentifier, |
| 135 | + options |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + const result: ParsedCleanupInput = { |
| 140 | + mode: 'issue', |
| 141 | + issueNumber: options.issue, |
| 142 | + options |
| 143 | + } |
| 144 | + if (trimmedIdentifier) { |
| 145 | + result.identifier = trimmedIdentifier |
| 146 | + } |
| 147 | + return result |
| 148 | + } |
| 149 | + |
| 150 | + // Mode: Auto-detect from identifier |
| 151 | + if (!trimmedIdentifier) { |
| 152 | + throw new Error('Missing required argument: identifier. Use --all to remove all worktrees or --list to list them.') |
| 153 | + } |
| 154 | + |
| 155 | + // Auto-detection: Check if identifier is purely numeric |
| 156 | + // Pattern from bash script line 364: ^[0-9]+$ |
| 157 | + const numericPattern = /^[0-9]+$/ |
| 158 | + if (numericPattern.test(trimmedIdentifier)) { |
| 159 | + // Numeric input = issue number |
| 160 | + return { |
| 161 | + mode: 'issue', |
| 162 | + issueNumber: parseInt(trimmedIdentifier, 10), |
| 163 | + identifier: trimmedIdentifier, |
| 164 | + originalInput: trimmedIdentifier, |
| 165 | + options |
| 166 | + } |
| 167 | + } else { |
| 168 | + // Non-numeric = branch name |
| 169 | + return { |
| 170 | + mode: 'single', |
| 171 | + branchName: trimmedIdentifier, |
| 172 | + identifier: trimmedIdentifier, |
| 173 | + originalInput: trimmedIdentifier, |
| 174 | + options |
| 175 | + } |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * Validate parsed input for option conflicts |
| 181 | + * Throws descriptive errors for invalid option combinations |
| 182 | + * |
| 183 | + * @private |
| 184 | + */ |
| 185 | + private validateInput(parsed: ParsedCleanupInput): void { |
| 186 | + const { mode, options, branchName } = parsed |
| 187 | + |
| 188 | + // Conflict: --list is informational only, incompatible with destructive operations |
| 189 | + if (mode === 'list') { |
| 190 | + if (options.all) { |
| 191 | + throw new Error('Cannot use --list with --all (list is informational only)') |
| 192 | + } |
| 193 | + if (options.issue !== undefined) { |
| 194 | + throw new Error('Cannot use --list with --issue (list is informational only)') |
| 195 | + } |
| 196 | + if (parsed.identifier) { |
| 197 | + throw new Error('Cannot use --list with a specific identifier (list shows all worktrees)') |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + // Conflict: --all removes everything, can't combine with specific identifier or --issue |
| 202 | + if (mode === 'all') { |
| 203 | + if (parsed.identifier) { |
| 204 | + throw new Error('Cannot use --all with a specific identifier. Use one or the other.') |
| 205 | + } |
| 206 | + if (parsed.issueNumber !== undefined) { |
| 207 | + throw new Error('Cannot use --all with a specific identifier. Use one or the other.') |
| 208 | + } |
| 209 | + } |
| 210 | + |
| 211 | + // Conflict: explicit --issue flag with branch name identifier |
| 212 | + // (This prevents confusion when user provides both) |
| 213 | + if (options.issue !== undefined && branchName) { |
| 214 | + throw new Error('Cannot use --issue flag with branch name identifier. Use numeric identifier or --issue flag alone.') |
| 215 | + } |
| 216 | + |
| 217 | + // Note: --force and --dry-run are compatible with all modes (no conflicts) |
| 218 | + } |
| 219 | +} |
0 commit comments