UNPKG

@intlayer/chokidar

Version:

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

69 lines 2.33 kB
import { deepTransformNode, NodeType } from "@intlayer/core"; import { existsSync, mkdirSync, writeFileSync } from "fs"; import { join } from "path"; const writeFilePlugin = { id: "write-file-plugin", canHandle: (node) => typeof node === "object" && node?.nodeType === 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 abolsuteFilePath = join(process.cwd(), filePath); const fileDirectory = join(process.cwd(), filePath); if (!fileDirectory.startsWith(process.cwd())) { throw new Error("File directory not found"); } if (!existsSync(fileDirectory)) { mkdirSync(fileDirectory, { recursive: true }); } writeFileSync(abolsuteFilePath, fileContent); } catch (error) { throw new Error(`Error writing file to ${filePath}: ${error}`); } const transformedFileContent = { nodeType: NodeType.File, [NodeType.File]: node.file }; return transformedFileContent; } }; const mardownFilePlugin = { id: "markdown-file-plugin", canHandle: (node) => typeof node === "object" && node?.nodeType === NodeType.Markdown, transform: (node, props, deepTransformNode2) => { const simplifiedMarkdownNode = { nodeType: NodeType.Markdown, [NodeType.Markdown]: deepTransformNode2(node.markdown, props) }; return simplifiedMarkdownNode; } }; const insertionFilePlugin = { id: "insertion-file-plugin", canHandle: (node) => typeof node === "object" && node?.nodeType === NodeType.Insertion, transform: (node, props, deepTransformNode2) => { const simplifiedInsertionNode = { nodeType: NodeType.Insertion, [NodeType.Insertion]: deepTransformNode2(node.insertion, props) }; return simplifiedInsertionNode; } }; const prepareContentDeclaration = async (dictionary) => deepTransformNode(dictionary, { dictionaryKey: dictionary.key, keyPath: [], plugins: [writeFilePlugin, mardownFilePlugin, insertionFilePlugin] }); export { prepareContentDeclaration }; //# sourceMappingURL=prepareContentDeclaration.mjs.map