@lucidcms/core
Version:
The core of the Lucid CMS. It's responsible for spinning up the API and serving the CMS.
4 lines • 2.42 kB
JavaScript
import e from"../../constants/constants.mjs";import t from"../../utils/errors/lucid-error.mjs";import{pathExists as n,resolveSourcePath as r}from"../../utils/helpers/resolve-source-path.mjs";import i from"node:fs/promises";import a from"node:path";import{pathToFileURL as o}from"node:url";const s=[`.ts`,`.mts`,`.js`,`.mjs`],c=e=>e.endsWith(`.d.ts`)||e.endsWith(`.d.mts`)?!1:s.includes(a.extname(e)),l=(n,r)=>{if(!e.db.externalMigrationNameRegex.test(n))throw new t({message:`Invalid migration name "${n}". Migration names must start with a 13 digit timestamp followed by lowercase letters, numbers, hyphens and underscores, eg. "1751400000000-example". Use the "migrate:new" command to create one.`,data:{origin:r}})},u=async(e,r)=>{if(!await n(e)){if(r?.optional)return[];throw new t({message:`Migration source "${e}" does not exist.`})}let o=await i.stat(e);if(o.isFile()){if(!c(a.basename(e)))throw new t({message:`Migration source "${e}" must be a ${s.join(`, `)} file.`});return[e]}if(!o.isDirectory())throw new t({message:`Migration source "${e}" must be a file or directory.`});return(await i.readdir(e,{withFileTypes:!0})).filter(e=>e.isFile()&&c(e.name)).map(t=>a.join(e,t.name)).sort()},d=async n=>{let i=new Set,s=[];for(let e of n.sources??[]){if(typeof e==`object`&&!(e instanceof URL)){s.push(e);continue}let t=await r(e,{projectRoot:n.projectRoot,label:`Migration source`});for(let e of await u(t))i.add(e)}if(n.projectRoot){let t=await u(a.join(n.projectRoot,e.db.externalMigrationDirectory),{optional:!0});for(let e of t)i.add(e)}let c={},d={},f=(e,n,r)=>{if(d[e])throw new t({message:`Duplicate migration name "${e}". Migration names must be unique across all migration sources.`,data:{origins:[d[e],r]}});c[e]=n,d[e]=r};for(let e of i){let n=a.basename(e),r=n.slice(0,n.length-a.extname(n).length);l(r,e);let i=await import(
/*! @vite-ignore */
`${o(e).href}?t=${Date.now()}`);if(typeof i.default!=`function`)throw new t({message:`Invalid migration file "${n}". Migration files must default export a migration created with the "defineMigration" helper.`,data:{filePath:e}});f(r,i.default,e)}for(let e of s){let t=`inline source "${e.name}"`;l(e.name,t),f(e.name,e.migration,t)}return c},f=async(e,t)=>{e.db.registerExternalMigrations(await d({sources:e.migrations?.sources,projectRoot:t}))};export{d as default,f as prepareExternalMigrations};
//# sourceMappingURL=load-external-migrations.mjs.map