Something went wrong. Try again.
opencode v2 mirror
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334import path from "path"import { appendFile, mkdir, readFile, rename, rm, writeFile } from "fs/promises"
export function readText(filePath: string) { return readFile(filePath, "utf8")}
export async function readJson<T>(filePath: string) { return JSON.parse(await readFile(filePath, "utf8")) as T}
export async function writeText(filePath: string, content: string) { await mkdir(path.dirname(filePath), { recursive: true }) await writeFile(filePath, content)}
export async function appendText(filePath: string, content: string) { await mkdir(path.dirname(filePath), { recursive: true }) await appendFile(filePath, content)}
export async function writeJsonAtomic(filePath: string, value: unknown) { await mkdir(path.dirname(filePath), { recursive: true }) const temporary = `${filePath}.${process.pid}.${crypto.randomUUID()}.tmp` await writeFile(temporary, JSON.stringify(value)).catch(async (error) => { await rm(temporary, { force: true }).catch(() => undefined) throw error }) await rename(temporary, filePath).catch(async (error) => { await rm(temporary, { force: true }).catch(() => undefined) throw error })}