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