fix: preserve BOM in text tool round-trips (#23797)
This commit is contained in:
31
packages/opencode/src/util/bom.ts
Normal file
31
packages/opencode/src/util/bom.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import { Effect } from "effect"
|
||||
import { AppFileSystem } from "@opencode-ai/shared/filesystem"
|
||||
|
||||
const BOM_CODE = 0xfeff
|
||||
const BOM = String.fromCharCode(BOM_CODE)
|
||||
|
||||
export function split(text: string) {
|
||||
if (text.charCodeAt(0) !== BOM_CODE) return { bom: false, text }
|
||||
return { bom: true, text: text.slice(1) }
|
||||
}
|
||||
|
||||
export function join(text: string, bom: boolean) {
|
||||
const stripped = split(text).text
|
||||
if (!bom) return stripped
|
||||
return BOM + stripped
|
||||
}
|
||||
|
||||
export const readFile = Effect.fn("Bom.readFile")(function* (fs: AppFileSystem.Interface, filePath: string) {
|
||||
return split(new TextDecoder("utf-8", { ignoreBOM: true }).decode(yield* fs.readFile(filePath)))
|
||||
})
|
||||
|
||||
export const syncFile = Effect.fn("Bom.syncFile")(function* (
|
||||
fs: AppFileSystem.Interface,
|
||||
filePath: string,
|
||||
bom: boolean,
|
||||
) {
|
||||
const current = yield* readFile(fs, filePath)
|
||||
if (current.bom === bom) return current.text
|
||||
yield* fs.writeWithDirs(filePath, join(current.text, bom))
|
||||
return current.text
|
||||
})
|
||||
Reference in New Issue
Block a user