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) 3.13 kB
import{existsSync as p,readFileSync as b,writeFileSync as S}from"node:fs";import{parseSchemaSnapshot as w,SchemaSnapshotParseError as y,evaluateSchemaDrift as k,serializeSchemaSnapshot as H}from"@lunora/codegen";const f="/_lunora/health",E="/_lunora/health/ready",P=(e,r=f)=>(e.endsWith("/")?e.slice(0,-1):e)+r,d=async(e,r,a)=>{let h={error:`health probe failed: no health path configured for ${e}`,url:e};for(const[c,i]of r.entries()){const t=P(e,i);let s;try{s=await a(t)}catch(o){const l=o instanceof Error?o.message:String(o);return{error:`health probe failed: could not reach ${t} (${l})`,url:t}}if(s.ok)return{url:t};if(h={error:`health probe failed: ${t} returned HTTP ${String(s.status)}`,url:t},s.status!==404||c===r.length-1)return h}return h},v=async e=>new Promise(r=>{setTimeout(r,e)}),_=async({attempts:e=1,baseUrl:r,delayMs:a=2e3,fetchImpl:h,paths:c=[f],sleep:i=v,timeoutMs:t=1e4})=>{const s=h??(u=>fetch(u,{signal:AbortSignal.timeout(t)})),o=Math.max(1,e);let l=await d(r,c,s);for(let u=1;u<o&&l.error!==void 0;u+=1)await i(a),l=await d(r,c,s);return l},$=e=>{if(!p(e))return{status:"absent"};let r;try{r=b(e,"utf8")}catch{return{status:"corrupt"}}try{const a=w(r);return a===void 0?{status:"corrupt"}:{snapshot:a,status:"ok"}}catch(a){if(a instanceof y)return{status:"corrupt"};throw a}},O=(e,r)=>{S(e,H(r),"utf8")},T=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})},A=(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}),F=e=>{const{allowDrift:r,codegen:a,command:h,logger:c,readOnly:i=!1,updateBaseline:t=!1}=e,s=a.schemaSnapshotPath,o=$(s),l={logger:c,readOnly:i,rebless:()=>{O(s,a.schemaSnapshot)},snapshotPath:s,updateBaseline:t};if(o.status==="corrupt")return T(l);const u=m=>k({allowDrift:m,baseline:o.status==="ok"?o.snapshot:void 0,command:h,current:a.schemaSnapshot,migrations:a.migrations}),n=u(r);if(n.blocked)return A(n,l);const g=r&&n.changes.length>0&&u(!1).blocked;return n.changes.length>0?(i||c.info(n.reason),g&&!t?(i||c.warn(`schema baseline left at its committed shape — --allow-schema-drift overrides this run only. Pass --update-schema-baseline to accept the new shape: ${s}`),{blocked:!1,changes:n.changes,reason:n.reason}):{blocked:!1,changes:n.changes,reason:n.reason,rebless:i?void 0:l.rebless}):{blocked:!1,changes:n.changes,reason:n.reason}};export{E as H,f as a,_ as p,F as r};