UNPKG

@lunora/cli

Version:

The Lunora CLI: init, dev, deploy, codegen, migrate, seed, doctor, insights, logs, registry, and the rest of the project commands

2 lines (1 loc) 2.82 kB
import{existsSync as f,readFileSync as g,writeFileSync as m}from"node:fs";import{evaluateSchemaDrift as p,parseSchemaSnapshot as b,SchemaSnapshotParseError as S,serializeSchemaSnapshot as y}from"@lunora/codegen";const d="/_lunora/health",B="/_lunora/health/ready",k=(e,r=d)=>(e.endsWith("/")?e.slice(0,-1):e)+r,u=async(e,r,s)=>{let c={error:`health probe failed: no health path configured for ${e}`,url:e};for(const[i,h]of r.entries()){const n=k(e,h);let t;try{t=await s(n)}catch(o){const l=o instanceof Error?o.message:String(o);return{error:`health probe failed: could not reach ${n} (${l})`,url:n}}if(t.ok)return{url:n};if(c={error:`health probe failed: ${n} returned HTTP ${String(t.status)}`,url:n},t.status!==404||i===r.length-1)return c}return c},w=async e=>new Promise(r=>{setTimeout(r,e)}),O=async({attempts:e=1,baseUrl:r,delayMs:s=2e3,fetchImpl:c,paths:i=[d],sleep:h=w,timeoutMs:n=1e4})=>{const t=c??(a=>fetch(a,{signal:AbortSignal.timeout(n)})),o=Math.max(1,e);let l=await u(r,i,t);for(let a=1;a<o&&l.error!==void 0;a+=1)await h(s),l=await u(r,i,t);return l},H=e=>{if(!f(e))return{status:"absent"};let r;try{r=g(e,"utf8")}catch{return{status:"corrupt"}}try{const s=b(r);return s===void 0?{status:"corrupt"}:{snapshot:s,status:"ok"}}catch(s){if(s instanceof S)return{status:"corrupt"};throw s}},P=(e,r)=>{m(e,y(r),"utf8")},$=e=>{const r=`schema-drift gate: the committed baseline ${e.snapshotPath} is unreadable or malformed, so schema drift cannot be checked. Fix it (e.g. resolve a merge conflict in lunora/.lunora-schema.json), or pass --update-schema-baseline to regenerate it from the current schema.`;return e.updateBaseline&&!e.readOnly?(e.logger.warn(`schema baseline was unreadable; regenerating from the current schema on success (--update-schema-baseline): ${e.snapshotPath}`),{blocked:!1,changes:[],reason:r,rebless:e.rebless}):(e.readOnly||e.logger.error(r),{blocked:!0,changes:[],reason:r})},T=(e,r)=>(r.readOnly||r.logger.error(e.reason),r.updateBaseline&&!r.readOnly?(r.logger.warn(`schema baseline will be re-blessed despite breaking drift on success (--update-schema-baseline): ${r.snapshotPath}`),{blocked:!1,changes:e.changes,reason:e.reason,rebless:r.rebless}):{blocked:!0,changes:e.changes,reason:e.reason}),D=e=>{const{allowDrift:r,codegen:s,command:c,logger:i,readOnly:h=!1,updateBaseline:n=!1}=e,t=s.schemaSnapshotPath,o=H(t),l={logger:i,readOnly:h,rebless:()=>{P(t,s.schemaSnapshot)},snapshotPath:t,updateBaseline:n};if(o.status==="corrupt")return $(l);const a=p({allowDrift:r,baseline:o.status==="ok"?o.snapshot:void 0,command:c,current:s.schemaSnapshot});return a.blocked?T(a,l):a.changes.length>0?(h||i.info(a.reason),{blocked:!1,changes:a.changes,reason:a.reason,rebless:h?void 0:l.rebless}):{blocked:!1,changes:a.changes,reason:a.reason}};export{B as H,d as a,O as p,D as r};