@lunora/cli
Version:
The Lunora CLI: init, dev, deploy, codegen, run, reset, and migrate commands
3 lines (2 loc) • 4.19 kB
JavaScript
import{readLinkedProject as m}from"@lunora/config";import{i as g}from"../packem_shared/command-Bjv4VmYA.mjs";import{M as p,P as f}from"../packem_shared/detect-package-manager-B2eIuAiP.mjs";import{defaultSpawner as v}from"../packem_shared/createRecordingSpawner-C8_7nffs.mjs";import{createR2Sql as h}from"@lunora/bindings/r2sql";import{createPipelineLogReader as w}from"@lunora/runtime";const b=new Set(["debug","error","fatal","info","log","trace","warn"]),y=/^\d+$/,c=(r,e)=>{if(r===void 0)return;const t=r.trim();if(y.test(t))return Number(t);const o=Date.parse(t);if(Number.isNaN(o))throw new TypeError(`logs: invalid ${e} "${r}" — pass epoch-millis or an ISO 8601 date`);return o},d=(r,e)=>{if(r!==void 0){if(!b.has(r))throw new TypeError(`logs: invalid ${e} "${r}" — expected one of trace, debug, log, info, warn, error, fatal`);return r}},_=r=>{const e={},t=c(r.since,"--since");t!==void 0&&(e.sinceTs=t);const o=c(r.until,"--until");o!==void 0&&(e.untilTs=o);const s=d(r.level,"--level");s!==void 0&&(e.level=s);const n=d(r.minLevel,"--min-level");if(n!==void 0&&(e.minLevel=n),r.functionPrefix!==void 0&&(e.functionPathPrefix=r.functionPrefix),r.traceId!==void 0&&(e.traceId=r.traceId),r.shardKey!==void 0&&(e.shardKey=r.shardKey),r.userId!==void 0&&(e.userId=r.userId),r.limit!==void 0){const i=Number(r.limit);if(!Number.isFinite(i))throw new TypeError(`logs: invalid --limit "${r.limit}" — expected a number`);e.limit=i}if(r.cursor!==void 0){const i=Number(r.cursor);if(!Number.isFinite(i))throw new TypeError(`logs: invalid --cursor "${r.cursor}" — expected the epoch-millis ts from a prior page's nextCursor`);e.cursor={ts:i}}return e},S=r=>{const e=new Date(r.ts).toISOString(),t=r.level.toUpperCase().padEnd(5);return`${e} ${t} ${r.functionPath} ${r.message}`},L=async r=>{const e=r.environment??process.env,t=e.R2_SQL_ACCOUNT_ID,o=e.R2_SQL_TOKEN,s=e.R2_SQL_BUCKET,n=[];if((t===void 0||t.length===0)&&n.push("R2_SQL_ACCOUNT_ID"),(o===void 0||o.length===0)&&n.push("R2_SQL_TOKEN"),(s===void 0||s.length===0)&&n.push("R2_SQL_BUCKET"),(r.table===void 0||r.table.length===0)&&n.push("--table"),n.length>0)return r.logger.error(`logs --durable: R2 SQL not configured (missing ${n.join(", ")}). The Pipeline must write to an R2 Data Catalog (Iceberg) table, and you must supply R2_SQL_ACCOUNT_ID / R2_SQL_TOKEN / R2_SQL_BUCKET plus --table — see the observability docs.`),{code:1,error:"not configured"};let i;try{i=_(r)}catch(a){return r.logger.error(a instanceof Error?a.message:String(a)),{code:1,error:"invalid option"}}const u=h({accountId:t,apiToken:o,bucket:s,fetch:r.fetch}),l=await w(u,{namespace:r.namespace,table:r.table}).query(i);for(const a of l.rows)process.stdout.write(`${r.ndjson===!0?JSON.stringify(a):S(a)}
`);return l.rows.length===0?r.logger.info("logs --durable: no matching log records"):l.nextCursor!==void 0&&r.logger.info(`logs --durable: more rows available — pass --cursor ${String(l.nextCursor.ts)} for the next page`),{code:0,rows:l.rows}},I=new Set(["json","pretty"]),T=async r=>{const e=r.cwd??process.cwd();if(r.format!==void 0&&!I.has(r.format))return r.logger.error(`logs: unknown --format "${r.format}" — expected pretty | json`),{code:1,descriptor:void 0,error:"invalid format"};const t=["tail"];r.worker!==void 0&&t.push(r.worker);const o=r.env??m(e)?.env;o!==void 0&&t.push("--env",o),r.format!==void 0&&t.push("--format",r.format),r.status!==void 0&&t.push("--status",r.status),r.search!==void 0&&t.push("--search",r.search),r.temporary&&t.push("--temporary");const s=p(f(e),"wrangler",t),n={args:s.args,command:s.command,cwd:e};return r.logger.info(`tailing logs via ${n.command} ${n.args.join(" ")}`),{code:(await(r.spawner??v)(n)).code,descriptor:n}},K=g(({argument:r,cwd:e,logger:t,options:o})=>o.durable===!0?L({cursor:o.cursor,functionPrefix:o.functionPrefix,level:o.level,limit:o.limit,logger:t,minLevel:o.minLevel,namespace:o.namespace,ndjson:o.ndjson===!0,shardKey:o.shardKey,since:o.since,table:o.table,traceId:o.traceId,until:o.until,userId:o.userId}):T({cwd:e,env:o.env,format:o.format,logger:t,search:o.search,status:o.status,temporary:o.temporary===!0,worker:r[0]}));export{K as execute,T as runLogsCommand};