UNPKG

@intlayer/chokidar

Version:

Uses chokidar to scan and build Intlayer declaration files into dictionaries based on Intlayer configuration.

96 lines (94 loc) 3.25 kB
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 _intlayer_core_package_json = require("@intlayer/core/package.json"); _intlayer_core_package_json = require_runtime.__toESM(_intlayer_core_package_json); //#region src/utils/runOnce.ts const DEFAULT_RUN_ONCE_OPTIONS = { cacheTimeoutMs: 60 * 1e3 }; const writeSentinelFile = async (sentinelFilePath, currentTimestamp) => { const data = { version: _intlayer_core_package_json.default.version, timestamp: currentTimestamp }; try { await (0, node_fs_promises.mkdir)((0, node_path.dirname)(sentinelFilePath), { recursive: true }); await (0, node_fs_promises.writeFile)(sentinelFilePath, JSON.stringify(data), { flag: "wx" }); } catch (err) { if (err.code === "EEXIST") return; if (err.code === "ENOENT") try { await (0, node_fs_promises.mkdir)((0, node_path.dirname)(sentinelFilePath), { recursive: true }); await (0, node_fs_promises.writeFile)(sentinelFilePath, JSON.stringify(data), { flag: "wx" }); return; } catch (retryErr) { if (retryErr.code === "EEXIST") return; } throw err; } }; /** * Ensures a callback function runs only once within a specified time window across multiple processes. * Uses a sentinel file to coordinate execution and prevent duplicate work. * * @param sentinelFilePath - Path to the sentinel file used for coordination * @param callback - The function to execute (should be async) * @param options - The options for the runOnce function * * @example * ```typescript * await runPrepareIntlayerOnce( * '/tmp/intlayer-sentinel', * async () => { * // Your initialization logic here * await prepareIntlayer(); * }, * 30 * 1000 // 30 seconds cache * ); * ``` * * @throws {Error} When there are unexpected filesystem errors */ const runOnce = async (sentinelFilePath, callback, options) => { const { onIsCached, cacheTimeoutMs, forceRun } = { ...DEFAULT_RUN_ONCE_OPTIONS, ...options ?? {} }; const currentTimestamp = Date.now(); try { const sentinelAge = currentTimestamp - (await (0, node_fs_promises.stat)(sentinelFilePath)).mtime.getTime(); let shouldRebuild = Boolean(forceRun) || sentinelAge > cacheTimeoutMs; if (!shouldRebuild) try { const raw = await (0, node_fs_promises.readFile)(sentinelFilePath, "utf8"); let cachedVersion; try { cachedVersion = JSON.parse(raw).version; } catch { cachedVersion = void 0; } if (!cachedVersion || cachedVersion !== _intlayer_core_package_json.default.version) shouldRebuild = true; } catch { shouldRebuild = true; } if (shouldRebuild) try { await (0, node_fs_promises.unlink)(sentinelFilePath); } catch {} else { await onIsCached?.(); return; } } catch (err) { if (err.code === "ENOENT") {} else throw err; } await writeSentinelFile(sentinelFilePath, currentTimestamp); try { await callback(); await writeSentinelFile(sentinelFilePath, currentTimestamp); } catch { try { await (0, node_fs_promises.unlink)(sentinelFilePath); } catch {} } }; //#endregion exports.runOnce = runOnce; //# sourceMappingURL=runOnce.cjs.map