@intlayer/chokidar
Version:
Uses chokidar to scan and build Intlayer declaration files into dictionaries based on Intlayer configuration.
51 lines (49 loc) • 2 kB
JavaScript
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const require_runtime = require('./_virtual/_rolldown/runtime.cjs');
let node_fs_promises = require("node:fs/promises");
let node_path = require("node:path");
let node_crypto = require("node:crypto");
let node_fs = require("node:fs");
//#region src/writeFileIfChanged.ts
const activeTempFiles = /* @__PURE__ */ new Set();
process.on("exit", () => {
for (const file of activeTempFiles) try {
(0, node_fs.rmSync)(file, { force: true });
} catch {}
});
const getFileHash = (path) => {
return new Promise((resolve) => {
const hash = (0, node_crypto.createHash)("sha256");
const stream = (0, node_fs.createReadStream)(path);
stream.on("data", (chunk) => hash.update(chunk));
stream.on("end", () => resolve(hash.digest("hex")));
stream.on("error", () => resolve(null));
});
};
const writeFileIfChanged = async (path, data, { encoding = "utf8", tempDir } = {}) => {
if ((0, node_crypto.createHash)("sha256").update(data, encoding).digest("hex") === await getFileHash(path)) return false;
if (tempDir) await (0, node_fs_promises.mkdir)(tempDir, { recursive: true });
const tempFileName = `${(0, node_path.basename)(path)}.${Date.now()}-${(0, node_crypto.randomBytes)(4).toString("hex")}.tmp`;
const tempPath = tempDir ? (0, node_path.join)(tempDir, tempFileName) : `${path}.${tempFileName}`;
activeTempFiles.add(tempPath);
try {
let mode;
try {
mode = (await (0, node_fs_promises.stat)(path)).mode;
} catch {}
await (0, node_fs_promises.writeFile)(tempPath, data, { encoding });
if (mode !== void 0) await (0, node_fs_promises.chmod)(tempPath, mode);
await (0, node_fs_promises.rename)(tempPath, path);
} catch (error) {
try {
await (0, node_fs_promises.rm)(tempPath, { force: true });
} catch {}
throw error;
} finally {
activeTempFiles.delete(tempPath);
}
return true;
};
//#endregion
exports.writeFileIfChanged = writeFileIfChanged;
//# sourceMappingURL=writeFileIfChanged.cjs.map