UNPKG

@lunora/cli

Version:

The Lunora CLI: init, dev, deploy, codegen, migrate, seed, doctor, insights, logs, registry, and the rest of the project commands

6 lines (5 loc) 2.63 kB
import{createWriteStream as m}from"node:fs";import{unlink as g}from"node:fs/promises";import{l as y}from"./admin-token-BKmc3AUm.mjs";import{u as v}from"./admin-url-Ca-KI3d_.mjs";import{r as h}from"./shared-Ce9bKz5c.mjs";const b=o=>{if(o===void 0)return;const t=o.split(",").map(e=>e.trim()).filter(e=>e.length>0);return t.length>0?t:void 0},w=async(o,t)=>{o.write(t)||await new Promise((e,a)=>{let i=r=>{};const s=()=>{o.removeListener("error",i),e()};i=r=>{o.removeListener("drain",s),a(r)},o.once("drain",s),o.once("error",i)})},$=async(o,t)=>{const e=o.getReader(),a=new TextDecoder;let i=0,s=0,r="",n=!1;try{for(;!n;){const c=await e.read();if(n=c.done,c.value===void 0)continue;i+=c.value.length,r+=a.decode(c.value,{stream:!0});let d=r.indexOf(` `);for(;d!==-1;){s+=1;const l=`${r.slice(0,d)} `;await w(t,l),r=r.slice(d+1),d=r.indexOf(` `)}}return r.length>0&&(s+=1,await w(t,`${r} `)),{bytes:i,rows:s}}finally{e.releaseLock()}},p=async(o,t)=>{if(t!==void 0){o.destroy();try{await g(t)}catch{}}},x=async(o,t,e)=>{await new Promise((i,s)=>{o.end(r=>{const n=r??e();n===void 0?i():s(n)})});const a=e();if(a!==void 0)throw await p(o,t),a},P=async o=>{if(o.prod&&o.url===void 0)return o.logger.error("--prod requires an explicit --url (refusing to export from the implicit localhost worker)"),{bytes:0,code:1,rows:0};const t=v(o.url,o.logger,o.cwd);if(t===void 0)return{bytes:0,code:1,rows:0};const{token:e}=y({cwd:o.cwd??process.cwd(),token:o.token,url:t});if(!e)return o.logger.error("admin token required — pass --token, set LUNORA_ADMIN_TOKEN, or add it to .dev.vars (local targets only)"),{bytes:0,code:1,rows:0};const a=`${t}${h}`,i=b(o.tables),s=o.fetchImpl??globalThis.fetch;if(typeof s!="function")throw new TypeError("no fetch implementation available — pass fetchImpl or run on Node >= 18");o.logger.info(`POST ${a} -> export${i?` (tables: ${i.join(",")})`:""}`);const r=await s(a,{body:JSON.stringify(i?{tables:i}:{}),headers:{authorization:`Bearer ${e}`,"content-type":"application/json"},method:"POST"});if(!r.ok){const u=await r.text();return o.logger.error(`export failed: HTTP ${String(r.status)}: ${u}`),{bytes:0,code:1,rows:0}}if(!r.body)return o.logger.error("export response carried no body"),{bytes:0,code:1,rows:0};const n=o.out===void 0||o.out==="-"?void 0:o.out,c=n===void 0?process.stdout:m(n,{encoding:"utf8"});let d;n!==void 0&&c.on("error",u=>{d??=u});let l,f;try{({bytes:l,rows:f}=await $(r.body,c))}catch(u){throw await p(c,n),u}return n!==void 0&&(await x(c,n,()=>d),o.logger.success(`wrote ${String(f)} rows to ${n} (${String(l)} bytes)`)),{bytes:l,code:0,rows:f}};export{P as runExportCommand};