import { existsSync, mkdirSync, writeFileSync } from 'node:fs'; import { dirname, join } from 'node:path'; import { colorizePath, x } from '@intlayer/config/logger'; import { deepTransformNode, type Plugins } from '@intlayer/core/interpreter'; import type { FileContent, FileContentConstructor, HTMLContent, HTMLContentConstructor, InsertionContent, InsertionContentConstructor, MarkdownContent, MarkdownContentConstructor, } from '@intlayer/core/transpiler'; import type { Dictionary } from '@intlayer/types/dictionary'; import * as NodeTypes from '@intlayer/types/nodeType'; /** * Receives the content of a `file()` node instead of writing it to disk. * `filePath` is relative to the project base directory. */ export type ExternalFileHandler = (filePath: string, content: string) => void; /** * Writes the content of a `file()` node to disk. */ const writeExternalFileToDisk: ExternalFileHandler = ( filePath, fileContent ) => { try { const absoluteFilePath = join(process.cwd(), filePath); // Create the file directory if it doesn't exist const fileDirectory = dirname(absoluteFilePath); if (!existsSync(fileDirectory)) { mkdirSync(fileDirectory, { recursive: true }); } // Write the file writeFileSync(absoluteFilePath, fileContent); } catch (error) { throw new Error( `${x} Error writing file to ${colorizePath(filePath)}: ${error}` ); } }; /** * Write file plugin */ const getWriteFilePlugin = (onExternalFile: ExternalFileHandler): Plugins => ({ id: 'write-file-plugin', canHandle: (node) => typeof node === 'object' && node?.nodeType === NodeTypes.FILE, transform: (node: FileContent) => { 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'); } onExternalFile(filePath, fileContent); const transformedFileContent: FileContentConstructor = { nodeType: NodeTypes.FILE, [NodeTypes.FILE]: node[NodeTypes.FILE], }; return transformedFileContent; }, }); /** * Markdown file plugin */ const markdownFilePlugin: Plugins = { id: 'markdown-file-plugin', canHandle: (node) => typeof node === 'object' && node?.nodeType === NodeTypes.MARKDOWN, transform: (node: MarkdownContent, props, deepTransformNode) => { const simplifiedMarkdownNode: MarkdownContentConstructor = { nodeType: NodeTypes.MARKDOWN, [NodeTypes.MARKDOWN]: deepTransformNode(node[NodeTypes.MARKDOWN], props), }; return simplifiedMarkdownNode; }, }; /** * Insertion file plugin */ const insertionFilePlugin: Plugins = { id: 'insertion-file-plugin', canHandle: (node) => typeof node === 'object' && node?.nodeType === NodeTypes.INSERTION, transform: (node: InsertionContent, props, deepTransformNode) => { const simplifiedInsertionNode: InsertionContentConstructor = { nodeType: NodeTypes.INSERTION, [NodeTypes.INSERTION]: deepTransformNode( node[NodeTypes.INSERTION], props ), }; return simplifiedInsertionNode; }, }; /** * HTML file plugin */ const htmlFilePlugin: Plugins = { id: 'html-file-plugin', canHandle: (node) => typeof node === 'object' && node?.nodeType === NodeTypes.HTML, transform: (node: HTMLContent, props, deepTransformNode) => { const simplifiedHTMLNode: HTMLContentConstructor = { nodeType: NodeTypes.HTML, [NodeTypes.HTML]: deepTransformNode(node[NodeTypes.HTML], props), }; return simplifiedHTMLNode; }, }; /** * 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. Pass * `onExternalFile` to collect them instead of writing them to disk. */ export const processContentDeclarationContent = async ( dictionary: Dictionary, onExternalFile: ExternalFileHandler = writeExternalFileToDisk ) => deepTransformNode(dictionary, { dictionaryKey: dictionary.key, keyPath: [], plugins: [ getWriteFilePlugin(onExternalFile), markdownFilePlugin, insertionFilePlugin, htmlFilePlugin, ], });