refactor: migrate src/tool/edit.ts from Bun.file() to Filesystem module (#14120)
This commit is contained in:
@@ -49,7 +49,7 @@ export const EditTool = Tool.define("edit", {
|
|||||||
let contentNew = ""
|
let contentNew = ""
|
||||||
await FileTime.withLock(filePath, async () => {
|
await FileTime.withLock(filePath, async () => {
|
||||||
if (params.oldString === "") {
|
if (params.oldString === "") {
|
||||||
const existed = await Bun.file(filePath).exists()
|
const existed = await Filesystem.exists(filePath)
|
||||||
contentNew = params.newString
|
contentNew = params.newString
|
||||||
diff = trimDiff(createTwoFilesPatch(filePath, filePath, contentOld, contentNew))
|
diff = trimDiff(createTwoFilesPatch(filePath, filePath, contentOld, contentNew))
|
||||||
await ctx.ask({
|
await ctx.ask({
|
||||||
@@ -61,7 +61,7 @@ export const EditTool = Tool.define("edit", {
|
|||||||
diff,
|
diff,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
await Bun.write(filePath, params.newString)
|
await Filesystem.write(filePath, params.newString)
|
||||||
await Bus.publish(File.Event.Edited, {
|
await Bus.publish(File.Event.Edited, {
|
||||||
file: filePath,
|
file: filePath,
|
||||||
})
|
})
|
||||||
@@ -73,12 +73,11 @@ export const EditTool = Tool.define("edit", {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const file = Bun.file(filePath)
|
const stats = Filesystem.stat(filePath)
|
||||||
const stats = await file.stat().catch(() => {})
|
|
||||||
if (!stats) throw new Error(`File ${filePath} not found`)
|
if (!stats) throw new Error(`File ${filePath} not found`)
|
||||||
if (stats.isDirectory()) throw new Error(`Path is a directory, not a file: ${filePath}`)
|
if (stats.isDirectory()) throw new Error(`Path is a directory, not a file: ${filePath}`)
|
||||||
await FileTime.assert(ctx.sessionID, filePath)
|
await FileTime.assert(ctx.sessionID, filePath)
|
||||||
contentOld = await file.text()
|
contentOld = await Filesystem.readText(filePath)
|
||||||
contentNew = replace(contentOld, params.oldString, params.newString, params.replaceAll)
|
contentNew = replace(contentOld, params.oldString, params.newString, params.replaceAll)
|
||||||
|
|
||||||
diff = trimDiff(
|
diff = trimDiff(
|
||||||
@@ -94,7 +93,7 @@ export const EditTool = Tool.define("edit", {
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
await file.write(contentNew)
|
await Filesystem.write(filePath, contentNew)
|
||||||
await Bus.publish(File.Event.Edited, {
|
await Bus.publish(File.Event.Edited, {
|
||||||
file: filePath,
|
file: filePath,
|
||||||
})
|
})
|
||||||
@@ -102,7 +101,7 @@ export const EditTool = Tool.define("edit", {
|
|||||||
file: filePath,
|
file: filePath,
|
||||||
event: "change",
|
event: "change",
|
||||||
})
|
})
|
||||||
contentNew = await file.text()
|
contentNew = await Filesystem.readText(filePath)
|
||||||
diff = trimDiff(
|
diff = trimDiff(
|
||||||
createTwoFilesPatch(filePath, filePath, normalizeLineEndings(contentOld), normalizeLineEndings(contentNew)),
|
createTwoFilesPatch(filePath, filePath, normalizeLineEndings(contentOld), normalizeLineEndings(contentNew)),
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user