@lucidcms/core
Version:
The core of the Lucid CMS. It's responsible for spinning up the API and serving the CMS.
4 lines (3 loc) • 5.61 kB
JavaScript
import e from"../../utils/errors/lucid-error.mjs";import{configArtifactEntries as t}from"./config-artifacts.mjs";import{mkdir as n,readFile as r,writeFile as i}from"node:fs/promises";import a from"node:path";import o from"typescript";const s=new Map([[`@lucidcms/runtime-node`,`@lucidcms/runtime-node/runtime`],[`@lucidcms/runtime-cloudflare`,`@lucidcms/runtime-cloudflare/runtime`]]),c={config:`config`,db:`db`,runtime:`runtime`},l=e=>e.endsWith(`.tsx`)?o.ScriptKind.TSX:e.endsWith(`.jsx`)?o.ScriptKind.JSX:e.endsWith(`.js`)||e.endsWith(`.mjs`)?o.ScriptKind.JS:o.ScriptKind.TS,u=(e,t)=>{if(o.isIdentifier(e)){t.add(e.text);return}for(let n of e.elements)o.isOmittedExpression(n)||u(n.name,t)},d=e=>{let t=new Set,n=e=>{(o.isFunctionDeclaration(e)||o.isClassDeclaration(e)||o.isInterfaceDeclaration(e)||o.isTypeAliasDeclaration(e)||o.isEnumDeclaration(e))&&e.name&&t.add(e.name.text),o.isVariableDeclaration(e)&&u(e.name,t),o.isParameter(e)&&u(e.name,t),o.forEachChild(e,n)};return n(e),t},f=e=>{let t=e.parent;return o.isVariableDeclaration(t)&&t.name===e||o.isFunctionDeclaration(t)&&t.name===e||o.isClassDeclaration(t)&&t.name===e||o.isInterfaceDeclaration(t)&&t.name===e||o.isTypeAliasDeclaration(t)&&t.name===e||o.isEnumDeclaration(t)&&t.name===e||o.isParameter(t)&&t.name===e||o.isImportSpecifier(t)&&t.name===e||o.isImportClause(t)&&t.name===e||o.isNamespaceImport(t)&&t.name===e},p=e=>{let t=e.parent;return o.isShorthandPropertyAssignment(t)?!1:o.isPropertyAccessExpression(t)&&t.name===e||o.isPropertyAssignment(t)&&t.name===e||o.isMethodDeclaration(t)&&t.name===e||o.isPropertyDeclaration(t)&&t.name===e||o.isPropertySignature(t)&&t.name===e||o.isMethodSignature(t)&&t.name===e||o.isGetAccessorDeclaration(t)&&t.name===e||o.isSetAccessorDeclaration(t)&&t.name===e||o.isQualifiedName(t)&&t.right===e},m=e=>{let t=new Set,n=d(e),r=e=>{o.isIdentifier(e)&&!n.has(e.text)&&!f(e)&&!p(e)&&t.add(e.text),o.forEachChild(e,r)};return r(e),t},h=(e,t)=>t.getFullText(e).trim(),g=e=>e.replace(/^export\s+declare\s+/,`declare `).replace(/^export\s+/,``),_=t=>{for(let e of t.statements){if(!o.isExportAssignment(e)||!o.isCallExpression(e.expression))continue;let[t]=e.expression.arguments;if(t&&o.isObjectLiteralExpression(t))return t}throw new e({message:"Lucid config artifact splitting requires `export default configureLucid({ runtime, db, config })`."})},v=e=>{if(o.isIdentifier(e)||o.isStringLiteral(e))return e.text},y=(t,n)=>{for(let e of t.properties){if(o.isShorthandPropertyAssignment(e)&&e.name.text===n)return e.name;if(o.isPropertyAssignment(e)&&v(e.name)===n)return e.initializer}throw new e({message:`Lucid config is missing the top-level \`${n}\` property.`})},b=e=>{for(let t of e.statements)if(o.isVariableStatement(t)&&t.modifiers?.some(e=>e.kind===o.SyntaxKind.ExportKeyword)){for(let e of t.declarationList.declarations)if(o.isIdentifier(e.name)&&e.name.text===`env`&&e.initializer)return e.initializer}},x=e=>{let t=new Map;for(let n of e.statements){if(o.isImportDeclaration(n)||o.isExportAssignment(n))continue;let e=d(n);for(let r of e)t.set(r,n)}return t},S=(e,t)=>{let n=x(e),r=t?Array.from(m(t)):[],i=new Set,a=new Set(r);for(;r.length>0;){let e=r.shift();if(!e)continue;let t=n.get(e);if(!(!t||i.has(t))){i.add(t);for(let e of m(t))a.has(e)||(a.add(e),r.push(e))}}return{neededStatements:i,neededIdentifiers:a}},C=(e,t)=>{let n=t.propertyName?`${t.propertyName.getText(e)} as ${t.name.getText(e)}`:t.name.getText(e);return t.isTypeOnly?`type ${n}`:n},w=e=>e.startsWith(`.`)?e:`./${e}`,T=e=>e.split(a.sep).join(`/`),E=e=>{if(!e.modulePath.startsWith(`.`))return e.modulePath;let n=a.dirname(e.sourceFile.fileName),r=a.dirname(a.join(e.outputPath,`${t[e.target]}.ts`)),i=a.resolve(n,e.modulePath),o=a.relative(r,i);return w(T(o))},D=(e,t,n,r,i)=>{let a=t.importClause;if(!a)return;let c=[];if(a.name&&n.has(a.name.text)&&c.push(a.name.text),a.namedBindings)if(o.isNamespaceImport(a.namedBindings))n.has(a.namedBindings.name.text)&&c.push(`* as ${a.namedBindings.name.text}`);else{let t=a.namedBindings.elements.filter(e=>n.has(e.name.text));t.length>0&&c.push(`{ ${t.map(t=>C(e,t)).join(`, `)} }`)}if(c.length===0)return;let l=o.isStringLiteral(t.moduleSpecifier)?t.moduleSpecifier.text:t.moduleSpecifier.getText(e),u=r===`runtime`?s.get(l)??l:l,d=E({modulePath:u,sourceFile:e,target:r,outputPath:i});return`import${a.isTypeOnly?` type`:``} ${c.join(`, `)} from ${JSON.stringify(d)};`},O=t=>{let{neededStatements:n,neededIdentifiers:r}=S(t.sourceFile,t.expression),i=[];for(let e of t.sourceFile.statements){if(!o.isImportDeclaration(e))continue;let n=D(t.sourceFile,e,r,t.target,t.outputPath);n&&i.push(n)}for(let e of t.sourceFile.statements)n.has(e)&&i.push(g(h(t.sourceFile,e)));if(t.target===`env`)t.expression?i.push(`export const env = ${t.expression.getText(t.sourceFile)};`):i.push(`export {};`);else{if(!t.expression)throw new e({message:`Lucid config is missing the \`${t.target}\` artifact expression.`});i.push(`export default ${t.expression.getText(t.sourceFile)};`)}return`${i.join(`
`)}\n`},k=async e=>{let s=await r(e.configPath,`utf-8`),u=o.createSourceFile(e.configPath,s,o.ScriptTarget.Latest,!0,l(e.configPath)),d=_(u),f={config:y(d,c.config),db:y(d,c.db),runtime:y(d,c.runtime),env:b(u)},p=Object.fromEntries(Object.entries(t).map(([t,n])=>[t,a.join(e.outputPath,`${n}.ts`)]));return await n(a.join(e.outputPath,`lucid`),{recursive:!0}),await Promise.all(Object.entries(p).map(async([t,n])=>{let r=t;await i(n,O({sourceFile:u,target:r,outputPath:e.outputPath,expression:f[r]}))})),p};export{k as default};
//# sourceMappingURL=prepare-config-artifacts.mjs.map