(command: string)
| 47 | * Returns null if the command is not a valid sed in-place edit |
| 48 | */ |
| 49 | export function parseSedEditCommand(command: string): SedEditInfo | null { |
| 50 | const trimmed = command.trim() |
| 51 | |
| 52 | // Must start with sed |
| 53 | const sedMatch = trimmed.match(/^\s*sed\s+/) |
| 54 | if (!sedMatch) return null |
| 55 | |
| 56 | const withoutSed = trimmed.slice(sedMatch[0].length) |
| 57 | const parseResult = tryParseShellCommand(withoutSed) |
| 58 | if (!parseResult.success) return null |
| 59 | const tokens = parseResult.tokens |
| 60 | |
| 61 | // Extract string tokens only |
| 62 | const args: string[] = [] |
| 63 | for (const token of tokens) { |
| 64 | if (typeof token === 'string') { |
| 65 | args.push(token) |
| 66 | } else if ( |
| 67 | typeof token === 'object' && |
| 68 | token !== null && |
| 69 | 'op' in token && |
| 70 | token.op === 'glob' |
| 71 | ) { |
| 72 | // Glob patterns are too complex for this simple parser |
| 73 | return null |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | // Parse flags and arguments |
| 78 | let hasInPlaceFlag = false |
| 79 | let extendedRegex = false |
| 80 | let expression: string | null = null |
| 81 | let filePath: string | null = null |
| 82 | |
| 83 | let i = 0 |
| 84 | while (i < args.length) { |
| 85 | const arg = args[i]! |
| 86 | |
| 87 | // Handle -i flag (with or without backup suffix) |
| 88 | if (arg === '-i' || arg === '--in-place') { |
| 89 | hasInPlaceFlag = true |
| 90 | i++ |
| 91 | // On macOS, -i requires a suffix argument (even if empty string) |
| 92 | // Check if next arg looks like a backup suffix (empty, or starts with dot) |
| 93 | // Don't consume flags (-E, -r) or sed expressions (starting with s, y, d) |
| 94 | if (i < args.length) { |
| 95 | const nextArg = args[i] |
| 96 | // If next arg is empty string or starts with dot, it's a backup suffix |
| 97 | if ( |
| 98 | typeof nextArg === 'string' && |
| 99 | !nextArg.startsWith('-') && |
| 100 | (nextArg === '' || nextArg.startsWith('.')) |
| 101 | ) { |
| 102 | i++ // Skip the backup suffix |
| 103 | } |
| 104 | } |
| 105 | continue |
| 106 | } |
no test coverage detected