@lunora/cli
Version:
The Lunora CLI: init, dev, deploy, codegen, migrate, seed, doctor, insights, logs, registry, and the rest of the project commands
9 lines (8 loc) • 1.15 kB
JavaScript
import{Project as f,SyntaxKind as o,Node as i}from"ts-morph";const x=/^[A-Za-z_$][\w$]*$/u,s=r=>`// lunora:add:${r}:start`,a=r=>`// lunora:add:${r}:end`,p=r=>`./${r}/schema`,m=r=>{for(const e of r)if(e.getExpression().getText()==="defineSchema")return e},g=r=>{let e=r;for(let t=e.getParent();t!==void 0;t=e.getParent())if(i.isPropertyAccessExpression(t)&&t.getExpression()===e)e=t;else if(i.isCallExpression(t)&&t.getExpression()===e)e=t;else break;return e},E=(r,e)=>{if(!x.test(e))return{ok:!1,reason:"invalid-identifier"};if(r.includes(s(e)))return{ok:!1,reason:"already-applied"};const t=new f({compilerOptions:{allowJs:!0},useInMemoryFileSystem:!0}).createSourceFile("schema.ts",r,{overwrite:!0}),l=t.getDescendantsOfKind(o.CallExpression),n=m(l);if(!n)return{ok:!1,reason:"no-define-schema"};if(n.getArguments()[0]?.getKind()!==o.ObjectLiteralExpression)return{ok:!1,reason:"non-object-argument"};const c=g(n).getEnd(),d=`
${s(e)}
.extend(${e}.extension)
${a(e)}
`;t.insertText(c,d);const u=`${s(e)}
import { ${e} } from "${p(e)}";
${a(e)}
`;return t.insertText(0,u),{ok:!0,text:t.getFullText()}};export{E as insertSchemaExtension};