UNPKG

@intlayer/chokidar

Version:

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

44 lines (42 loc) 1.95 kB
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); const require_runtime = require('../_virtual/_rolldown/runtime.cjs'); const require_detectFormatCommand = require('../detectFormatCommand.cjs'); let node_fs_promises = require("node:fs/promises"); let node_path = require("node:path"); let node_child_process = require("node:child_process"); let _intlayer_core_utils = require("@intlayer/core/utils"); //#region src/writeContentDeclaration/writeYamlFile.ts const EXCLUDED_YAML_KEYS = new Set([ "$schema", "id", "filePath" ]); const writeYamlFile = async (absoluteFilePath, dictionary, configuration) => { const fileContent = (0, _intlayer_core_utils.stringifyYaml)(Object.fromEntries(Object.entries(dictionary).filter(([k, v]) => !EXCLUDED_YAML_KEYS.has(k) && v !== void 0))); await (0, node_fs_promises.mkdir)((0, node_path.dirname)(absoluteFilePath), { recursive: true }); const tempDir = configuration.system?.tempDir; if (tempDir) await (0, node_fs_promises.mkdir)(tempDir, { recursive: true }); const tempFileName = `${(0, node_path.basename)(absoluteFilePath)}.${Date.now()}-${Math.random().toString(36).slice(2)}.tmp`; const tempPath = tempDir ? (0, node_path.join)(tempDir, tempFileName) : `${absoluteFilePath}.${tempFileName}`; try { await (0, node_fs_promises.writeFile)(tempPath, fileContent, "utf-8"); await (0, node_fs_promises.rename)(tempPath, absoluteFilePath); } catch (error) { try { await (0, node_fs_promises.rm)(tempPath, { force: true }); } catch {} throw error; } const formatCommand = require_detectFormatCommand.detectFormatCommand(configuration); if (formatCommand) try { (0, node_child_process.execSync)(formatCommand.replace("{{file}}", absoluteFilePath), { stdio: "inherit", cwd: configuration.system.baseDir }); } catch (error) { console.error(error); } }; //#endregion exports.writeYamlFile = writeYamlFile; //# sourceMappingURL=writeYamlFile.cjs.map