(cwd: string, edits: EditSpec[])
| 86 | } |
| 87 | |
| 88 | function editOneFile(cwd: string, edits: EditSpec[]): string[] { |
| 89 | const filePath = resolveWorkspacePath(cwd, edits[0].file_path) |
| 90 | let content: string |
| 91 | try { |
| 92 | content = fs.readFileSync(filePath, "utf8") |
| 93 | } catch (error) { |
| 94 | return edits.map(() => `FAILED ${filePath}: ${(error as Error).message}`) |
| 95 | } |
| 96 | |
| 97 | const results: string[] = [] |
| 98 | let changed = false |
| 99 | for (const edit of edits) { |
| 100 | const { content: next, error } = applyEdit(content, edit) |
| 101 | if (error) { |
| 102 | results.push(`FAILED ${filePath}: ${error}`) |
| 103 | } else { |
| 104 | content = next |
| 105 | changed = true |
| 106 | results.push(`OK ${filePath}: replaced "${truncate(edit.old_string || "(entire file)", 60)}"`) |
| 107 | } |
| 108 | } |
| 109 | if (changed) { |
| 110 | try { |
| 111 | fs.writeFileSync(filePath, content) |
| 112 | } catch (error) { |
| 113 | return edits.map(() => `FAILED ${filePath}: ${(error as Error).message}`) |
| 114 | } |
| 115 | } |
| 116 | return results |
| 117 | } |
| 118 | |
| 119 | function truncate(text: string, max: number): string { |
| 120 | const oneLine = text.replace(/\n/g, "\\n") |
no test coverage detected