| 20 | } |
| 21 | |
| 22 | export async function writeJsonAtomic(filePath: string, value: unknown) { |
| 23 | await mkdir(path.dirname(filePath), { recursive: true }) |
| 24 | const temporary = `${filePath}.${process.pid}.${crypto.randomUUID()}.tmp` |
| 25 | await Bun.write(temporary, JSON.stringify(value)).catch(async (error) => { |
| 26 | await rm(temporary, { force: true }).catch(() => undefined) |
| 27 | throw error |
| 28 | }) |
| 29 | await rename(temporary, filePath).catch(async (error) => { |
| 30 | await rm(temporary, { force: true }).catch(() => undefined) |
| 31 | throw error |
| 32 | }) |
| 33 | } |