UNPKG

zephyr-agent

Version:
49 lines (48 loc) • 1.59 kB
import "node:module"; import { randomUUID } from "node:crypto"; import { mkdir, open as promises_open, rename, rm } from "node:fs/promises"; import * as __rspack_external_node_path_806ed179 from "node:path"; async function syncDirectory(directory) { let handle; try { handle = await promises_open(directory, 'r'); await handle.sync(); } catch (error) { const code = error?.code; if (![ 'EACCES', 'EBADF', 'EISDIR', 'EINVAL', 'EPERM' ].includes(code ?? '')) throw error; } finally{ await handle?.close(); } } async function writeFileAtomically(destinationPath, content, hooks = {}) { const directory = __rspack_external_node_path_806ed179.dirname(destinationPath); await mkdir(directory, { recursive: true }); const temporaryPath = `${destinationPath}.${process.pid}.${randomUUID()}.tmp`; let handle; try { handle = await promises_open(temporaryPath, 'wx', 384); await handle.writeFile(content, 'utf8'); await handle.sync(); await handle.close(); handle = void 0; await hooks.beforeRename?.(temporaryPath, destinationPath); await rename(temporaryPath, destinationPath); await syncDirectory(directory); } catch (error) { await handle?.close().catch(()=>void 0); await rm(temporaryPath, { force: true }).catch(()=>void 0); throw error; } } export { writeFileAtomically }; //# sourceMappingURL=atomic-file.mjs.map