const require_chunk = require("./chunk-DakpK96I.cjs");
let path = require("path");
path = require_chunk.__toESM(path, 1);
let fs = require("fs");
fs = require_chunk.__toESM(fs, 1);
let crypto = require("crypto");
//#region src/fs/atomic-write.ts
/**
* Atomically write `content` to `filePath`, creating parent directories as
* needed. The payload is written to a sibling temp file and then renamed over
* the target. rename(2) is atomic within a filesystem, so a crash or a
* concurrent reader can never observe a half-written (truncated/corrupt) file —
* only the complete old or complete new content. Used for every durable file
* the product owns (customer Markdown, JSON config/state, audit logs).
*/
function writeFileAtomic(filePath, content) {
	fs.default.mkdirSync(path.default.dirname(filePath), { recursive: true });
	const tmp = `${filePath}.${process.pid}.${(0, crypto.randomBytes)(4).toString("hex")}.tmp`;
	try {
		fs.default.writeFileSync(tmp, content, "utf-8");
		fs.default.renameSync(tmp, filePath);
	} catch (err) {
		try {
			if (fs.default.existsSync(tmp)) fs.default.unlinkSync(tmp);
		} catch {}
		throw err;
	}
}
//#endregion
Object.defineProperty(exports, "writeFileAtomic", {
	enumerable: true,
	get: function() {
		return writeFileAtomic;
	}
});

//# sourceMappingURL=atomic-write-BYmF-ThH.cjs.map