import { rename, unlink, writeFile } from 'node:fs/promises'; export async function writeTextFileAtomic(path: string, content: string): Promise { const tempPath = `${path}.${process.pid}.${Date.now()}.${Math.random().toString(36).slice(2)}.tmp`; try { await writeFile(tempPath, content); await rename(tempPath, path); } catch (error) { await unlink(tempPath).catch(() => {}); throw error; } }