import { writeFileSync, renameSync, mkdirSync, existsSync } from 'fs' import path from 'path' /** * Atomically write JSON to a file by writing to a temp file first, then * renaming it into place. Prevents partial writes from corrupting the file. */ export function atomicWriteJSON(filePath: string, data: unknown): void { const dir = path.dirname(filePath) if (!existsSync(dir)) mkdirSync(dir, { recursive: true }) const tmp = `${filePath}.tmp` writeFileSync(tmp, JSON.stringify(data, null, 2), 'utf-8') renameSync(tmp, filePath) }