UNPKG

@lucidcms/core

Version:

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

2 lines 1.7 kB
import e from"../../utils/errors/lucid-error.mjs";import{pathExists as t,resolveSourcePath as n}from"../../utils/helpers/resolve-source-path.mjs";import{mergeTranslationBundles as r,normalizeTranslationBundles as i}from"./translations.mjs";import a from"zod";import o from"node:fs/promises";import s from"node:path";const c=a.record(a.string(),a.string()),l=/^(.+)\.(admin|server)\.json$/,u=async t=>{let n=s.basename(t),r=n.match(l);if(!r)throw new e({message:`Invalid translation file name "${n}". Expected "<locale>.admin.json" or "<locale>.server.json".`,data:{filePath:t}});let[,i,a]=r,u;try{u=c.parse(JSON.parse(await o.readFile(t,`utf-8`)))}catch(r){throw new e({message:`Invalid translation file "${n}". Expected a flat JSON object of string keys and string values.`,data:{filePath:t,error:r instanceof Error?r.message:r}})}return{[i]:{admin:a===`admin`?u:{},server:a===`server`?u:{}}}},d=async(n,i)=>{if(!await t(n)){if(i?.optional)return{};throw new e({message:`Translation source "${n}" does not exist.`})}let a=await o.stat(n);if(a.isFile())return u(n);if(!a.isDirectory())throw new e({message:`Translation source "${n}" must be a file or directory.`});let c=await o.readdir(n,{withFileTypes:!0});return r(...await Promise.all(c.filter(e=>e.isFile()&&e.name.endsWith(`.json`)).map(e=>u(s.join(n,e.name)))))},f=async e=>{let t=await Promise.all((e.sources??[]).map(async t=>d(await n(t,{projectRoot:e.projectRoot,label:`Translation source`})))),a=e.includeProjectDirectory===!1||!e.projectRoot?{}:await d(s.join(e.projectRoot,e.projectDirectory??`translations`),{optional:!0});return i(r(...t,a))};export{f as loadTranslationSources}; //# sourceMappingURL=load-project-translations.mjs.map