UNPKG

@lucidcms/core

Version:

The core of the Lucid CMS. It's responsible for spinning up the API and serving the CMS.

1 lines 3.24 kB
{"version":3,"file":"render-mustache-template.mjs","names":["constants"],"sources":["../../../../src/libs/email/templates/render-mustache-template.ts"],"sourcesContent":["import { readFile } from \"node:fs/promises\";\nimport path from \"node:path\";\nimport Mustache from \"mustache\";\nimport constants from \"../../../constants/constants.js\";\nimport type { ServiceFn } from \"../../../utils/services/types.js\";\nimport { copy } from \"../../i18n/index.js\";\nimport type { RenderedTemplates } from \"../types.js\";\n\n/**\n * Dynamically renders a mustache template with the given data.\n */\nconst renderMustacheTemplate: ServiceFn<\n\t[\n\t\t{\n\t\t\ttemplate: string;\n\t\t\tdata: Record<string, unknown> | null;\n\t\t},\n\t],\n\tstring\n> = async (context, data) => {\n\tconst renderedTemplates = context.config.email.templates.rendered;\n\n\t//* use pre-rendered templates if available\n\tif (renderedTemplates) {\n\t\tconst preRenderedTemplate = renderedTemplates[data.template];\n\t\tif (preRenderedTemplate === undefined) {\n\t\t\treturn {\n\t\t\t\terror: {\n\t\t\t\t\tmessage: copy(\"server:core.email.templates.not.found.message\"),\n\t\t\t\t\tstatus: 404,\n\t\t\t\t},\n\t\t\t\tdata: undefined,\n\t\t\t};\n\t\t}\n\n\t\tconst renderedTemplate = Mustache.render(preRenderedTemplate, data.data);\n\n\t\treturn {\n\t\t\terror: undefined,\n\t\t\tdata: renderedTemplate,\n\t\t};\n\t}\n\n\ttry {\n\t\tconst templatesPath = path.resolve(\n\t\t\tprocess.cwd(),\n\t\t\tcontext.config.build.paths.outDir,\n\t\t\tconstants.email.renderedOutput,\n\t\t);\n\t\tconst renderedTemplates = JSON.parse(\n\t\t\tawait readFile(templatesPath, \"utf-8\"),\n\t\t) as RenderedTemplates;\n\n\t\tconst templateData = renderedTemplates[data.template];\n\t\tif (!templateData) {\n\t\t\treturn {\n\t\t\t\terror: {\n\t\t\t\t\tmessage: copy(\"server:core.email.templates.not.found.message\"),\n\t\t\t\t\tstatus: 404,\n\t\t\t\t},\n\t\t\t\tdata: undefined,\n\t\t\t};\n\t\t}\n\n\t\tconst renderedTemplate = Mustache.render(templateData.html, data.data);\n\n\t\treturn {\n\t\t\terror: undefined,\n\t\t\tdata: renderedTemplate,\n\t\t};\n\t} catch (error) {\n\t\treturn {\n\t\t\terror: {\n\t\t\t\tmessage: copy(\n\t\t\t\t\t\"server:core.email.templates.not.found.message\",\n\t\t\t\t\terror instanceof Error\n\t\t\t\t\t\t? { defaultMessage: error.message }\n\t\t\t\t\t\t: undefined,\n\t\t\t\t),\n\t\t\t\tstatus: 404,\n\t\t\t},\n\t\t\tdata: undefined,\n\t\t};\n\t}\n};\n\nexport default renderMustacheTemplate;\n"],"mappings":"wLAWA,MAAM,EAQF,MAAO,EAAS,IAAS,CAC5B,IAAM,EAAoB,EAAQ,OAAO,MAAM,UAAU,SAGzD,GAAI,EAAmB,CACtB,IAAM,EAAsB,EAAkB,EAAK,UAanD,OAZI,IAAwB,IAAA,GACpB,CACN,MAAO,CACN,QAAS,EAAK,+CAA+C,EAC7D,OAAQ,GACT,EACA,KAAM,IAAA,EACP,EAKM,CACN,MAAO,IAAA,GACP,KAJwB,EAAS,OAAO,EAAqB,EAAK,IAI7C,CACtB,CACD,CAEA,GAAI,CACH,IAAM,EAAgB,EAAK,QAC1B,QAAQ,IAAI,EACZ,EAAQ,OAAO,MAAM,MAAM,OAC3BA,EAAU,MAAM,cACjB,EAKM,EAJoB,KAAK,MAC9B,MAAM,EAAS,EAAe,OAAO,CAGD,CAAC,CAAC,EAAK,UAa5C,OAZK,EAYE,CACN,MAAO,IAAA,GACP,KAJwB,EAAS,OAAO,EAAa,KAAM,EAAK,IAI3C,CACtB,EAdQ,CACN,MAAO,CACN,QAAS,EAAK,+CAA+C,EAC7D,OAAQ,GACT,EACA,KAAM,IAAA,EACP,CASF,OAAS,EAAO,CACf,MAAO,CACN,MAAO,CACN,QAAS,EACR,gDACA,aAAiB,MACd,CAAE,eAAgB,EAAM,OAAQ,EAChC,IAAA,EACJ,EACA,OAAQ,GACT,EACA,KAAM,IAAA,EACP,CACD,CACD"}