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