import * as fs from "node:fs"; import * as path from "node:path"; export function writeFileAtomic(filePath: string, content: string) { fs.mkdirSync(path.dirname(filePath), { recursive: true }); const temporary = `${filePath}.${process.pid}.${Date.now()}.tmp`; try { fs.writeFileSync(temporary, content, { encoding: "utf8", mode: 0o600 }); fs.renameSync(temporary, filePath); } catch (error) { try { fs.unlinkSync(temporary); } catch {} throw error; } }