@intlayer/chokidar
Version:
Uses chokidar to scan and build Intlayer declaration files into dictionaries based on Intlayer configuration.
92 lines (90 loc) • 2.87 kB
JavaScript
import { dirname, join } from "node:path";
import { colorizePath, x } from "@intlayer/config/logger";
import { existsSync, mkdirSync, writeFileSync } from "node:fs";
import { deepTransformNode } from "@intlayer/core/interpreter";
import * as NodeTypes from "@intlayer/types/nodeType";
//#region src/writeContentDeclaration/processContentDeclarationContent.ts
/**
* Write file plugin
*/
const writeFilePlugin = {
id: "write-file-plugin",
canHandle: (node) => typeof node === "object" && node?.nodeType === NodeTypes.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 = join(process.cwd(), filePath);
const fileDirectory = dirname(absoluteFilePath);
if (!existsSync(fileDirectory)) mkdirSync(fileDirectory, { recursive: true });
writeFileSync(absoluteFilePath, fileContent);
} catch (error) {
throw new Error(`${x} Error writing file to ${colorizePath(filePath)}: ${error}`);
}
return {
nodeType: NodeTypes.FILE,
[NodeTypes.FILE]: node[NodeTypes.FILE]
};
}
};
/**
* Markdown file plugin
*/
const markdownFilePlugin = {
id: "markdown-file-plugin",
canHandle: (node) => typeof node === "object" && node?.nodeType === NodeTypes.MARKDOWN,
transform: (node, props, deepTransformNode) => {
return {
nodeType: NodeTypes.MARKDOWN,
[NodeTypes.MARKDOWN]: deepTransformNode(node[NodeTypes.MARKDOWN], props)
};
}
};
/**
* Insertion file plugin
*/
const insertionFilePlugin = {
id: "insertion-file-plugin",
canHandle: (node) => typeof node === "object" && node?.nodeType === NodeTypes.INSERTION,
transform: (node, props, deepTransformNode) => {
return {
nodeType: NodeTypes.INSERTION,
[NodeTypes.INSERTION]: deepTransformNode(node[NodeTypes.INSERTION], props)
};
}
};
/**
* HTML file plugin
*/
const htmlFilePlugin = {
id: "html-file-plugin",
canHandle: (node) => typeof node === "object" && node?.nodeType === NodeTypes.HTML,
transform: (node, props, deepTransformNode) => {
return {
nodeType: NodeTypes.HTML,
[NodeTypes.HTML]: deepTransformNode(node[NodeTypes.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) => deepTransformNode(dictionary, {
dictionaryKey: dictionary.key,
keyPath: [],
plugins: [
writeFilePlugin,
markdownFilePlugin,
insertionFilePlugin,
htmlFilePlugin
]
});
//#endregion
export { processContentDeclarationContent };
//# sourceMappingURL=processContentDeclarationContent.mjs.map