UNPKG

@intlayer/chokidar

Version:

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

64 lines (62 loc) 3.18 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 _intlayer_types_nodeType = require("@intlayer/types/nodeType"); let node_child_process = require("node:child_process"); //#region src/writeContentDeclaration/writeMarkdownFile.ts const stringifyYamlFrontmatter = (fields) => Object.entries(fields).filter(([, value]) => value !== void 0 && value !== null).map(([key, value]) => { if (Array.isArray(value)) return `${key}:\n${value.map((item) => ` - ${JSON.stringify(item)}`).join("\n")}`; if (typeof value === "string" && (value.includes(":") || value.includes("\n") || value.includes("#"))) return `${key}: ${JSON.stringify(value)}`; return `${key}: ${value}`; }).join("\n"); const getMarkdownBody = (content) => { const lines = content.split(/\r?\n/); const firstNonEmptyIndex = lines.findIndex((line) => line.trim() !== ""); if (firstNonEmptyIndex === -1 || lines[firstNonEmptyIndex].trim() !== "---") return content; let endIndex = -1; for (let i = firstNonEmptyIndex + 1; i < lines.length; i++) if (lines[i].trim() === "---") { endIndex = i; break; } if (endIndex === -1) return content; return lines.slice(endIndex + 1).join("\n").trimStart(); }; const EXCLUDED_FRONTMATTER_KEYS = new Set([ "content", "$schema", "id", "filePath" ]); const writeMarkdownFile = async (absoluteFilePath, dictionary, configuration) => { const content = dictionary.content; const markdownBody = getMarkdownBody(typeof content === "object" && content?.nodeType === _intlayer_types_nodeType.MARKDOWN ? content[_intlayer_types_nodeType.MARKDOWN] ?? "" : ""); const fileContent = `---\n${stringifyYamlFrontmatter(Object.fromEntries(Object.entries(dictionary).filter(([k, v]) => !EXCLUDED_FRONTMATTER_KEYS.has(k) && v !== void 0)))}\n---\n\n${markdownBody}`; 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.writeMarkdownFile = writeMarkdownFile; //# sourceMappingURL=writeMarkdownFile.cjs.map