import { open, rename, unlink } from "node:fs/promises"; import { dirname, join } from "node:path"; import { randomUUID } from "node:crypto"; async function syncDirectory(path: string): Promise { const directory = await open(path, "r"); try { await directory.sync(); } finally { await directory.close(); } } export async function writeFileAtomically(path: string, content: string): Promise { const directory = dirname(path); const temporary = join(directory, `.${randomUUID()}.tmp`); const file = await open(temporary, "wx", 0o600); try { await file.writeFile(content, "utf8"); await file.sync(); } catch (error) { await file.close(); await unlink(temporary).catch(() => undefined); throw error; } await file.close(); try { await rename(temporary, path); await syncDirectory(directory); } catch (error) { await unlink(temporary).catch(() => undefined); throw error; } }