{"version":3,"file":"atomic.mjs","names":[],"sources":["../../../../../../@warlock.js/fs/src/atomic.ts"],"sourcesContent":["import { randomBytes } from \"node:crypto\";\nimport { mkdir, rename, unlink, writeFile } from \"node:fs/promises\";\nimport path from \"node:path\";\n\n/**\n * Write a file atomically.\n *\n * Writes to a uniquely-named sibling temp file first, then renames it onto\n * the target. Readers either see the old content or the complete new\n * content — never a half-written file. If anything fails mid-write the\n * temp file is cleaned up.\n *\n * Parent directories are created if missing.\n *\n * @example\n *   await atomicWriteAsync(\"manifest.json\", JSON.stringify(data));\n */\nexport async function atomicWriteAsync(filePath: string, content: string | Buffer): Promise<void> {\n  const dir = path.dirname(filePath);\n  await mkdir(dir, { recursive: true });\n\n  // Random suffix so concurrent writers don't fight over the same temp file.\n  const tempPath = path.join(dir, `.${path.basename(filePath)}.${randomBytes(6).toString(\"hex\")}.tmp`);\n\n  try {\n    await writeFile(tempPath, content);\n    await rename(tempPath, filePath);\n  } catch (error) {\n    await unlink(tempPath).catch(() => undefined);\n    throw error;\n  }\n}\n\n/**\n * Atomic write convenience for JSON values (pretty-printed, 2-space indent).\n */\nexport async function atomicWriteJsonAsync(filePath: string, value: unknown): Promise<void> {\n  await atomicWriteAsync(filePath, JSON.stringify(value, null, 2));\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAiBA,eAAsB,iBAAiB,UAAkB,SAAyC;CAChG,MAAM,MAAM,KAAK,QAAQ,QAAQ;CACjC,MAAM,MAAM,KAAK,EAAE,WAAW,KAAK,CAAC;CAGpC,MAAM,WAAW,KAAK,KAAK,KAAK,IAAI,KAAK,SAAS,QAAQ,EAAE,GAAG,YAAY,CAAC,CAAC,CAAC,SAAS,KAAK,EAAE,KAAK;CAEnG,IAAI;EACF,MAAM,UAAU,UAAU,OAAO;EACjC,MAAM,OAAO,UAAU,QAAQ;CACjC,SAAS,OAAO;EACd,MAAM,OAAO,QAAQ,CAAC,CAAC,YAAY,MAAS;EAC5C,MAAM;CACR;AACF;;;;AAKA,eAAsB,qBAAqB,UAAkB,OAA+B;CAC1F,MAAM,iBAAiB,UAAU,KAAK,UAAU,OAAO,MAAM,CAAC,CAAC;AACjE"}