@lucidcms/core
Version:
The core of the Lucid CMS. It's responsible for spinning up the API and serving the CMS.
1 lines • 3.91 kB
Source Map (JSON)
{"version":3,"file":"load-email-templates.mjs","names":["cliLogger"],"sources":["../../../../src/libs/email/templates/load-email-templates.ts"],"sourcesContent":["import { access, readdir, readFile, stat } from \"node:fs/promises\";\nimport path from \"node:path\";\nimport type { Config } from \"../../../types.js\";\nimport { getDirName } from \"../../../utils/helpers/index.js\";\nimport cliLogger from \"../../cli/logger.js\";\nimport type { RenderedTemplates } from \"../types.js\";\n\nconst currentDir = getDirName(import.meta.url);\nconst templateExtensions = new Set([\".html\", \".mustache\"]);\n\nconst pathExists = async (targetPath: string): Promise<boolean> => {\n\ttry {\n\t\tawait access(targetPath);\n\t\treturn true;\n\t} catch {\n\t\treturn false;\n\t}\n};\n\nconst getTemplateName = (file: string) => {\n\tconst extension = path.extname(file);\n\tif (!templateExtensions.has(extension)) return null;\n\n\treturn file.slice(0, -extension.length);\n};\n\nconst processTemplatesInDirectory = async (\n\tdirectory: string,\n\trenderedTemplates: RenderedTemplates,\n\toverwrite = true,\n\tsilent = false,\n\tverbose = false,\n): Promise<void> => {\n\tif (!(await pathExists(directory))) return;\n\n\tconst files = (await readdir(directory, { withFileTypes: true }))\n\t\t.filter((entry) => entry.isFile())\n\t\t.map((entry) => entry.name)\n\t\t.toSorted();\n\n\tfor (const file of files) {\n\t\tconst templateName = getTemplateName(file);\n\t\tif (!templateName) continue;\n\n\t\tif (!overwrite && renderedTemplates[templateName]) {\n\t\t\tcontinue;\n\t\t}\n\n\t\tconst filePath = path.join(directory, file);\n\t\tconst [html, fileStat] = await Promise.all([\n\t\t\treadFile(filePath, \"utf-8\"),\n\t\t\tstat(filePath),\n\t\t]);\n\n\t\trenderedTemplates[templateName] = {\n\t\t\thtml,\n\t\t\tlastModified: fileStat.mtime.toISOString(),\n\t\t};\n\n\t\tif (verbose) {\n\t\t\tcliLogger.info(\"Prepared email template:\", cliLogger.color.green(file), {\n\t\t\t\tsilent,\n\t\t\t});\n\t\t}\n\t}\n};\n\nconst loadEmailTemplates = async (props: {\n\tconfig: Config;\n\tsilent?: boolean;\n\tverbose?: boolean;\n}): Promise<RenderedTemplates> => {\n\tconst silent = props.silent ?? false;\n\tconst verbose = props.verbose ?? false;\n\tconst renderedTemplates: RenderedTemplates = {};\n\n\tconst projectTemplatePath = props.config.email.templates.directory;\n\tconst packageTemplatePath = path.join(currentDir, \"../../../../templates\");\n\n\tawait processTemplatesInDirectory(\n\t\tprojectTemplatePath,\n\t\trenderedTemplates,\n\t\ttrue,\n\t\tsilent,\n\t\tverbose,\n\t);\n\tawait processTemplatesInDirectory(\n\t\tpackageTemplatePath,\n\t\trenderedTemplates,\n\t\tfalse,\n\t\tsilent,\n\t\tverbose,\n\t);\n\n\treturn renderedTemplates;\n};\n\nexport default loadEmailTemplates;\n"],"mappings":"mMAOA,MAAM,EAAa,EAAW,OAAO,KAAK,GAAG,EACvC,EAAqB,IAAI,IAAI,CAAC,QAAS,WAAW,CAAC,EAEnD,EAAa,KAAO,IAAyC,CAClE,GAAI,CAEH,OADA,MAAM,EAAO,CAAU,EAChB,EACR,MAAQ,CACP,MAAO,EACR,CACD,EAEM,EAAmB,GAAiB,CACzC,IAAM,EAAY,EAAK,QAAQ,CAAI,EAGnC,OAFK,EAAmB,IAAI,CAAS,EAE9B,EAAK,MAAM,EAAG,CAAC,EAAU,MAAM,EAFS,IAGhD,EAEM,EAA8B,MACnC,EACA,EACA,EAAY,GACZ,EAAS,GACT,EAAU,KACS,CACnB,GAAI,CAAE,MAAM,EAAW,CAAS,EAAI,OAEpC,IAAM,GAAS,MAAM,EAAQ,EAAW,CAAE,cAAe,EAAK,CAAC,EAAA,CAC7D,OAAQ,GAAU,EAAM,OAAO,CAAC,CAAC,CACjC,IAAK,GAAU,EAAM,IAAI,CAAC,CAC1B,SAAS,EAEX,IAAK,IAAM,KAAQ,EAAO,CACzB,IAAM,EAAe,EAAgB,CAAI,EAGzC,GAFI,CAAC,GAED,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