MCPcopy Create free account
hub / github.com/MatterAIOrg/OrbCode / editOneFile

Function editOneFile

src/tools/executors/files.ts:88–117  ·  view source on GitHub ↗
(cwd: string, edits: EditSpec[])

Source from the content-addressed store, hash-verified

86}
87
88function 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
119function truncate(text: string, max: number): string {
120 const oneLine = text.replace(/\n/g, "\\n")

Callers 2

fileEditFunction · 0.85
multiFileEditFunction · 0.85

Calls 3

resolveWorkspacePathFunction · 0.85
applyEditFunction · 0.85
truncateFunction · 0.70

Tested by

no test coverage detected