@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.68 kB
JavaScript
import{readdirSync as g,lstatSync as S,existsSync as y}from"node:fs";import{join as E,basename as T}from"node:path";import{pathToFileURL as w}from"node:url";import{d as R}from"../packem_shared/command-Mmxzxyr5.mjs";import{EXIT_CODE as u}from"../packem_shared/EXIT_CODE-08cwt3MK.mjs";const l=".eval.ts",$=new Set([".git","_generated","dist","node_modules"]),v=(e,s=[])=>{let r;try{r=g(e)}catch{return s}for(const t of r){const o=E(e,t);let n;try{n=S(o)}catch{continue}if(n.isDirectory()){if($.has(t))continue;v(o,s)}else n.isFile()&&t.endsWith(l)&&s.push(o)}return s.toSorted((t,o)=>t.localeCompare(o))},_=e=>typeof e=="object"&&e!==null&&typeof e.run=="function",m=e=>typeof e=="number"&&Number.isFinite(e)&&e>=0&&e<=1,F="evals",O='"lunora eval" needs native TypeScript execution — run Node ≥23.6, or a TS loader/transform (tracked in plans/245-eval-runner-design.md §8.3)',N=/unknown file extension ".ts"/i,p=e=>e instanceof Error?e.code==="ERR_UNKNOWN_FILE_EXTENSION"?!0:N.test(e.message):!1,f=e=>T(e,l),x=e=>({average:e.result?.average,error:e.error,items:e.result?.items,name:e.name,passed:e.passed,path:e.path,threshold:e.threshold}),b=e=>({evals:e.evals.map(s=>x(s))}),i=(e,s,r)=>(e.error(s),{code:r,error:s,evals:[]}),A=(e,s,r)=>{if(!y(e))return r.info(`eval: no "${s}" directory — nothing to run`),[];const t=v(e);return t.length===0&&r.info(`eval: no ${l} files found under "${s}"`),t},I=async e=>{const r=(await import(w(e).href)).default;if(!_(r))throw new Error("does not default-export an eval module (expected `{ run(): Promise<EvalResult>, name?, threshold? }`)");return r},L=async(e,s)=>{let r;try{r=await I(e)}catch(n){if(p(n))throw n;return{error:n instanceof Error?n.message:String(n),name:f(e),passed:!1,path:e}}const t=r.name??f(e);if(r.threshold!==void 0&&!m(r.threshold))return{error:`invalid \`threshold\` export: must be a number in [0, 1] — received ${JSON.stringify(r.threshold)}`,name:t,passed:!1,path:e};const o=r.threshold??s;try{const n=await r.run(),a=o===void 0||n.average>=o;return{name:t,passed:a,path:e,result:n,threshold:o}}catch(n){return{error:n instanceof Error?n.message:String(n),name:t,passed:!1,path:e,threshold:o}}},U=async(e,s)=>{const r=[];for(const t of e)r.push(await L(t,s));return r},D=e=>{if(e.error!==void 0)return`ERROR: ${e.error}`;const s=e.passed?"pass":"FAIL",r=e.result?.items.length??0;return`${s} (${String(r)} cases)`},C=e=>{if(e.length===0)return[];const s=Math.max(4,...e.map(t=>t.name.length)),r=[`${"NAME".padEnd(s)} SCORE THRESHOLD STATUS`];for(const t of e){const o=t.result===void 0?"—":t.result.average.toFixed(3),n=t.threshold===void 0?"—":t.threshold.toFixed(3);r.push(`${t.name.padEnd(s)} ${o.padEnd(6)} ${n.padEnd(9)} ${D(t)}`)}return r},M=async e=>{const s=e.cwd??process.cwd(),{logger:r}=e;if(e.threshold!==void 0&&!m(e.threshold))return i(r,`eval: --threshold must be a number in [0, 1] — received "${String(e.threshold)}"`,u.USAGE);const t=e.dir??F,o=E(s,t),n=A(o,t,r);let a;try{a=await U(n,e.threshold)}catch(d){if(!p(d))throw d;return i(r,O,1)}if(e.threshold!==void 0&&a.length===0){const d=`eval: --threshold ${String(e.threshold)} was set but 0 eval files were discovered under "${t}" — nothing was gated`;return i(r,d,u.USAGE)}for(const d of C(a))r.info(d);const c=a.filter(d=>!d.passed),h=c.length>0?1:0;return h===0?r.success(`eval: ${String(a.length)} eval(s) passed`):r.error(`eval: ${String(c.length)}/${String(a.length)} eval(s) failed`),{code:h,evals:a}},V=R(async({cwd:e,format:s,logger:r,options:t})=>{const o=await M({cwd:e,dir:t.dir,format:s,logger:r,threshold:t.threshold});return{code:o.code,data:b(o),error:o.error}});export{V as execute,M as runEvalCommand};