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.41 kB
import{readdirSync as E,lstatSync as S,existsSync as y}from"node:fs";import{join as m,basename as $}from"node:path";import{d as w}from"../packem_shared/command-B6vu47iR.mjs";import{EXIT_CODE as h}from"../packem_shared/EXIT_CODE-DVjUgIaG.mjs";const l=".eval.ts",F=new Set([".git","_generated","dist","node_modules"]),p=(e,s=[])=>{let r;try{r=E(e)}catch{return s}for(const t of r){const n=m(e,t);let o;try{o=S(n)}catch{continue}if(o.isDirectory()){if(F.has(t))continue;p(n,s)}else o.isFile()&&t.endsWith(l)&&s.push(n)}return s.toSorted((t,n)=>t.localeCompare(n))},b=e=>typeof e=="object"&&e!==null&&typeof e.run=="function",g=e=>typeof e=="number"&&Number.isFinite(e)&&e>=0&&e<=1,R="evals";let u;const T=async()=>(u??=import("jiti").then(({createJiti:e})=>e(import.meta.url,{interopDefault:!1})),u),f=e=>$(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}),A=e=>({evals:e.evals.map(s=>x(s))}),v=(e,s,r)=>(e.error(s),{code:r,error:s,evals:[]}),L=(e,s,r)=>{if(!y(e))return r.info(`eval: no "${s}" directory — nothing to run`),[];const t=p(e);return t.length===0&&r.info(`eval: no ${l} files found under "${s}"`),t},I=async e=>{const t=(await(await T()).import(e)).default;if(!b(t))throw new Error("does not default-export an eval module (expected `{ run(): Promise<EvalResult>, name?, threshold? }`)");return t},O=async(e,s)=>{let r;try{r=await I(e)}catch(o){return{error:o instanceof Error?o.message:String(o),name:f(e),passed:!1,path:e}}const t=r.name??f(e);if(r.threshold!==void 0&&!g(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 n=r.threshold??s;try{const o=await r.run(),a=n===void 0||o.average>=n;return{name:t,passed:a,path:e,result:o,threshold:n}}catch(o){return{error:o instanceof Error?o.message:String(o),name:t,passed:!1,path:e,threshold:n}}},_=async(e,s)=>{const r=[];for(const t of e)r.push(await O(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 n=t.result===void 0?"—":t.result.average.toFixed(3),o=t.threshold===void 0?"—":t.threshold.toFixed(3);r.push(`${t.name.padEnd(s)} ${n.padEnd(6)} ${o.padEnd(9)} ${D(t)}`)}return r},M=async e=>{const s=e.cwd??process.cwd(),{logger:r}=e;if(e.threshold!==void 0&&!g(e.threshold))return v(r,`eval: --threshold must be a number in [0, 1] — received "${String(e.threshold)}"`,h.USAGE);const t=e.dir??R,n=m(s,t),o=L(n,t,r),a=await _(o,e.threshold);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 v(r,d,h.USAGE)}for(const d of C(a))r.info(d);const i=a.filter(d=>!d.passed),c=i.length>0?1:0;return c===0?r.success(`eval: ${String(a.length)} eval(s) passed`):r.error(`eval: ${String(i.length)}/${String(a.length)} eval(s) failed`),{code:c,evals:a}},N=w(async({cwd:e,format:s,logger:r,options:t})=>{const n=await M({cwd:e,dir:t.dir,format:s,logger:r,threshold:t.threshold});return{code:n.code,data:A(n),error:n.error}});export{N as execute,M as runEvalCommand};