UNPKG

@intlayer/chokidar

Version:

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

95 lines (93 loc) 3.51 kB
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); const require_runtime = require('../_virtual/_rolldown/runtime.cjs'); let node_path = require("node:path"); let _intlayer_config_logger = require("@intlayer/config/logger"); let node_fs = require("node:fs"); let _intlayer_core_interpreter = require("@intlayer/core/interpreter"); let _intlayer_types_nodeType = require("@intlayer/types/nodeType"); _intlayer_types_nodeType = require_runtime.__toESM(_intlayer_types_nodeType); //#region src/writeContentDeclaration/processContentDeclarationContent.ts /** * Write file plugin */ const writeFilePlugin = { id: "write-file-plugin", canHandle: (node) => typeof node === "object" && node?.nodeType === _intlayer_types_nodeType.FILE, transform: (node) => { const fileContent = node.content; const filePath = node.fixedPath; if (typeof fileContent !== "string") throw new Error("File content must be a string"); if (typeof filePath !== "string") throw new Error("File path must be a string"); try { const absoluteFilePath = (0, node_path.join)(process.cwd(), filePath); const fileDirectory = (0, node_path.dirname)(absoluteFilePath); if (!(0, node_fs.existsSync)(fileDirectory)) (0, node_fs.mkdirSync)(fileDirectory, { recursive: true }); (0, node_fs.writeFileSync)(absoluteFilePath, fileContent); } catch (error) { throw new Error(`${_intlayer_config_logger.x} Error writing file to ${(0, _intlayer_config_logger.colorizePath)(filePath)}: ${error}`); } return { nodeType: _intlayer_types_nodeType.FILE, [_intlayer_types_nodeType.FILE]: node[_intlayer_types_nodeType.FILE] }; } }; /** * Markdown file plugin */ const markdownFilePlugin = { id: "markdown-file-plugin", canHandle: (node) => typeof node === "object" && node?.nodeType === _intlayer_types_nodeType.MARKDOWN, transform: (node, props, deepTransformNode) => { return { nodeType: _intlayer_types_nodeType.MARKDOWN, [_intlayer_types_nodeType.MARKDOWN]: deepTransformNode(node[_intlayer_types_nodeType.MARKDOWN], props) }; } }; /** * Insertion file plugin */ const insertionFilePlugin = { id: "insertion-file-plugin", canHandle: (node) => typeof node === "object" && node?.nodeType === _intlayer_types_nodeType.INSERTION, transform: (node, props, deepTransformNode) => { return { nodeType: _intlayer_types_nodeType.INSERTION, [_intlayer_types_nodeType.INSERTION]: deepTransformNode(node[_intlayer_types_nodeType.INSERTION], props) }; } }; /** * HTML file plugin */ const htmlFilePlugin = { id: "html-file-plugin", canHandle: (node) => typeof node === "object" && node?.nodeType === _intlayer_types_nodeType.HTML, transform: (node, props, deepTransformNode) => { return { nodeType: _intlayer_types_nodeType.HTML, [_intlayer_types_nodeType.HTML]: deepTransformNode(node[_intlayer_types_nodeType.HTML], props) }; } }; /** * Process content declaration content * * It filter node that are autogenerated by intlayer to do not rewrite them in the content declaration file. * * And write external sources as file content if necessary. */ const processContentDeclarationContent = async (dictionary) => (0, _intlayer_core_interpreter.deepTransformNode)(dictionary, { dictionaryKey: dictionary.key, keyPath: [], plugins: [ writeFilePlugin, markdownFilePlugin, insertionFilePlugin, htmlFilePlugin ] }); //#endregion exports.processContentDeclarationContent = processContentDeclarationContent; //# sourceMappingURL=processContentDeclarationContent.cjs.map