UNPKG

@lucidcms/core

Version:

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

1 lines 2.77 kB
{"version":3,"file":"prepare-translations.mjs","names":["constants"],"sources":["../../../src/libs/i18n/prepare-translations.ts"],"sourcesContent":["import { mkdir, writeFile } from \"node:fs/promises\";\nimport path from \"node:path\";\nimport constants from \"../../constants/constants.js\";\nimport type { Config } from \"../../types/config.js\";\nimport { LucidError } from \"../../utils/errors/index.js\";\nimport { loadTranslationSources } from \"./load-project-translations.js\";\nimport { createTranslationStore } from \"./store.js\";\nimport type { TranslationBundles, TranslationStore } from \"./types.js\";\n\nexport type PrepareTranslationsResult = {\n\ttranslationStore: TranslationStore;\n};\n\n/**\n * Writes the merged runtime translation bundle for compiled runtimes to import.\n */\nexport const writeTranslationArtifact = async (props: {\n\ttranslationStore: TranslationStore;\n\toutputPath: string;\n}) => {\n\ttry {\n\t\tawait mkdir(props.outputPath, { recursive: true });\n\t\tawait writeFile(\n\t\t\tpath.join(props.outputPath, constants.i18n.renderedOutput),\n\t\t\tJSON.stringify(props.translationStore.bundles, null, 2),\n\t\t);\n\t} catch (error) {\n\t\tthrow new LucidError({\n\t\t\tmessage:\n\t\t\t\terror instanceof Error\n\t\t\t\t\t? error.message\n\t\t\t\t\t: \"Lucid failed to write the translation artifact.\",\n\t\t});\n\t}\n};\n\n/**\n * Loads or accepts merged translation bundles, creates the runtime store, and\n * optionally writes the build artifact consumed by compiled runtimes.\n */\nconst prepareTranslations = async (props: {\n\tconfig: Config;\n\tprojectRoot?: string;\n\tbundles?: TranslationBundles;\n\toutputPath?: string;\n}): Promise<PrepareTranslationsResult> => {\n\tconst translationStore = createTranslationStore({\n\t\tdefaultLocale: props.config.i18n.defaultLocale,\n\t\tbundles:\n\t\t\tprops.bundles ??\n\t\t\t(await loadTranslationSources({\n\t\t\t\tprojectRoot: props.projectRoot,\n\t\t\t\tsources: props.config.i18n.sources,\n\t\t\t})),\n\t});\n\n\tif (props.outputPath) {\n\t\tawait writeTranslationArtifact({\n\t\t\ttranslationStore,\n\t\t\toutputPath: props.outputPath,\n\t\t});\n\t}\n\n\treturn {\n\t\ttranslationStore,\n\t};\n};\n\nexport default prepareTranslations;\n"],"mappings":"8SAgBA,MAAa,EAA2B,KAAO,IAGzC,CACL,GAAI,CACH,MAAM,EAAM,EAAM,WAAY,CAAE,UAAW,EAAK,CAAC,EACjD,MAAM,EACL,EAAK,KAAK,EAAM,WAAYA,EAAU,KAAK,cAAc,EACzD,KAAK,UAAU,EAAM,iBAAiB,QAAS,KAAM,CAAC,CACvD,CACD,OAAS,EAAO,CACf,MAAM,IAAI,EAAW,CACpB,QACC,aAAiB,MACd,EAAM,QACN,iDACL,CAAC,CACF,CACD,EAMM,EAAsB,KAAO,IAKO,CACzC,IAAM,EAAmB,EAAuB,CAC/C,cAAe,EAAM,OAAO,KAAK,cACjC,QACC,EAAM,SACL,MAAM,EAAuB,CAC7B,YAAa,EAAM,YACnB,QAAS,EAAM,OAAO,KAAK,OAC5B,CAAC,CACH,CAAC,EASD,OAPI,EAAM,YACT,MAAM,EAAyB,CAC9B,mBACA,WAAY,EAAM,UACnB,CAAC,EAGK,CACN,kBACD,CACD"}