@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) • 3.82 kB
JavaScript
import{randomUUID as x}from"node:crypto";import{createWriteStream as E}from"node:fs";import{unlink as b,rename as S}from"node:fs/promises";import{LunoraError as O}from"@lunora/errors";import{E as T,e as $}from"./shared-CV7ESvuj.mjs";import{r as N}from"./admin-token-VdUnvnKW.mjs";import{r as k,a as U}from"./admin-url-DJB8P6Jh.mjs";import{EXIT_CODE as h,isRefusal as I,exitCodeForStatus as A}from"./EXIT_CODE-DQVhSE-1.mjs";const L=e=>{if(e===void 0)return;const r=e.split(",").map(t=>t.trim()).filter(t=>t.length>0);return r.length>0?r:void 0},p=async(e,r)=>{e.write(r)||await new Promise((t,a)=>{let i=o=>{};const s=()=>{e.removeListener("error",i),t()};i=o=>{e.removeListener("drain",s),a(o)},e.once("drain",s),e.once("error",i)})},P=e=>{try{return JSON.parse(e)}catch{return}},R=async(e,r)=>{const t=e.getReader(),a=new TextDecoder;let i=0,s=0,o="",d="",c=!1;try{for(;!c;){const n=await t.read();if(c=n.done,n.value===void 0)continue;i+=n.value.length,o+=a.decode(n.value,{stream:!0});let l=o.indexOf(`
`);for(;l!==-1;){s+=1,d=o.slice(0,l);const f=`${d}
`;await p(r,f),o=o.slice(l+1),l=o.indexOf(`
`)}}if(o.length>0&&(s+=1,d=o,await p(r,`${o}
`)),s===0||$(P(d))!==s-1)throw new O("INTERNAL","export failed: the response has no end marker with a matching row count, so it may have been cut short; nothing was kept");return{bytes:i,rows:s-1}}finally{t.releaseLock()}},v=async(e,r)=>{if(r===void 0)return;const t=e;await new Promise(a=>{if(t.closed){a();return}t.once("close",()=>{a()}),t.destroy()});try{await b(r)}catch{}},_=async(e,r,t)=>{await new Promise((i,s)=>{e.end(o=>{const d=o??t();d===void 0?i():s(d)})});const a=t();if(a!==void 0)throw await v(e,r),a},D=async(e,r,t)=>{await _(e,r.stage,t);try{await S(r.stage,r.path)}catch(a){throw await v(e,r.stage),a}},y="export --format json needs a file destination (--out <file>) — with --out - the NDJSON stream already owns stdout.",q=e=>{const r=e.out===void 0||e.out==="-"?void 0:e.out;if(e.format==="json"&&r===void 0){e.logger.error(y);return}return{destination:r}},j=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)"),{refused:h.USAGE};const r=k(e.url,e.logger,e.cwd);if(r===void 0)return{refused:h.USAGE};const{token:t}=N({cwd:e.cwd??process.cwd(),token:e.token,url:r});return t?{fetchImpl:e.fetchImpl??U,requestUrl:`${r}${T}`,tables:L(e.tables),token:t}:(e.logger.error("admin token required — pass --token, set LUNORA_ADMIN_TOKEN, or add it to .dev.vars (local targets only)"),{refused:h.AUTH})},z=async e=>{const r=q(e);if(r===void 0)return{bytes:0,code:h.USAGE,error:y,rows:0};const{destination:t}=r,a=j(e);if(I(a))return{bytes:0,code:a.refused,error:"export: could not resolve the worker URL and admin token",rows:0};const{fetchImpl:i,requestUrl:s,tables:o,token:d}=a;e.logger.info(`POST ${s} -> export${o?` (tables: ${o.join(",")})`:""}`);const c=await i(s,{body:JSON.stringify(o?{tables:o}:{}),headers:{authorization:`Bearer ${d}`,"content-type":"application/json"},method:"POST"});if(!c.ok){const u=await c.text(),g=`export failed: HTTP ${String(c.status)}: ${u}`;return e.logger.error(g),{bytes:0,code:A(c.status),error:g,rows:0}}if(!c.body)return e.logger.error("export response carried no body"),{bytes:0,code:1,error:"export response carried no body",rows:0};const n=t===void 0?void 0:{path:t,stage:`${t}.${x()}.partial`},l=n===void 0?process.stdout:E(n.stage,{encoding:"utf8",mode:384});let f;n!==void 0&&l.on("error",u=>{f??=u});let m,w;try{({bytes:m,rows:w}=await R(c.body,l))}catch(u){throw await v(l,n?.stage),u}return n!==void 0&&(await D(l,n,()=>f),e.logger.success(`wrote ${String(w)} rows to ${n.path} (${String(m)} bytes)`)),{bytes:m,code:0,data:t===void 0?void 0:{bytes:m,out:t,rows:w,tables:o},rows:w}};export{z as runExportCommand};