refactor(server): canonicalize service API (#31049)

This commit is contained in:
Dax
2026-06-07 03:27:28 +00:00
committed by GitHub
parent 53ff1b57c9
commit fe0c4f8c74
388 changed files with 7103 additions and 4092 deletions
+33
View File
@@ -0,0 +1,33 @@
import path from "path"
import { appendFile, mkdir, rename, rm } from "fs/promises"
export function readText(filePath: string) {
return Bun.file(filePath).text()
}
export function readJson<T>(filePath: string) {
return Bun.file(filePath).json() as Promise<T>
}
export async function writeText(filePath: string, content: string) {
await mkdir(path.dirname(filePath), { recursive: true })
await Bun.write(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 Bun.write(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
})
}