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) • 3.53 kB
import{randomUUID as x}from"node:crypto";import{createWriteStream as b}from"node:fs";import{unlink as E,rename as S}from"node:fs/promises";import{r as O}from"./admin-token-VdUnvnKW.mjs";import{r as T,a as $}from"./admin-url-DJB8P6Jh.mjs";import{EXIT_CODE as w,isRefusal as U,exitCodeForStatus as N}from"./EXIT_CODE-D_XCixv8.mjs";import{E as k}from"./shared-D-zCOmgY.mjs";const I=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},y=async(e,r)=>{e.write(r)||await new Promise((t,a)=>{let d=o=>{};const n=()=>{e.removeListener("error",d),t()};d=o=>{e.removeListener("drain",n),a(o)},e.once("drain",n),e.once("error",d)})},P=async(e,r)=>{const t=e.getReader(),a=new TextDecoder;let d=0,n=0,o="",c=!1;try{for(;!c;){const i=await t.read();if(c=i.done,i.value===void 0)continue;d+=i.value.length,o+=a.decode(i.value,{stream:!0});let s=o.indexOf(` `);for(;s!==-1;){n+=1;const l=`${o.slice(0,s)} `;await y(r,l),o=o.slice(s+1),s=o.indexOf(` `)}}return o.length>0&&(n+=1,await y(r,`${o} `)),{bytes:d,rows:n}}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 E(r)}catch{}},A=async(e,r,t)=>{await new Promise((d,n)=>{e.end(o=>{const c=o??t();c===void 0?d():n(c)})});const a=t();if(a!==void 0)throw await v(e,r),a},_=async(e,r,t)=>{await A(e,r.stage,t);try{await S(r.stage,r.path)}catch(a){throw await v(e,r.stage),a}},p="export --format json needs a file destination (--out <file>) — with --out - the NDJSON stream already owns stdout.",D=e=>{const r=e.out===void 0||e.out==="-"?void 0:e.out;if(e.format==="json"&&r===void 0){e.logger.error(p);return}return{destination:r}},R=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:w.USAGE};const r=T(e.url,e.logger,e.cwd);if(r===void 0)return{refused:w.USAGE};const{token:t}=O({cwd:e.cwd??process.cwd(),token:e.token,url:r});return t?{fetchImpl:e.fetchImpl??$,requestUrl:`${r}${k}`,tables:I(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:w.AUTH})},H=async e=>{const r=D(e);if(r===void 0)return{bytes:0,code:w.USAGE,error:p,rows:0};const{destination:t}=r,a=R(e);if(U(a))return{bytes:0,code:a.refused,error:"export: could not resolve the worker URL and admin token",rows:0};const{fetchImpl:d,requestUrl:n,tables:o,token:c}=a;e.logger.info(`POST ${n} -> export${o?` (tables: ${o.join(",")})`:""}`);const i=await d(n,{body:JSON.stringify(o?{tables:o}:{}),headers:{authorization:`Bearer ${c}`,"content-type":"application/json"},method:"POST"});if(!i.ok){const u=await i.text(),h=`export failed: HTTP ${String(i.status)}: ${u}`;return e.logger.error(h),{bytes:0,code:N(i.status),error:h,rows:0}}if(!i.body)return e.logger.error("export response carried no body"),{bytes:0,code:1,error:"export response carried no body",rows:0};const s=t===void 0?void 0:{path:t,stage:`${t}.${x()}.partial`},l=s===void 0?process.stdout:b(s.stage,{encoding:"utf8",mode:384});let g;s!==void 0&&l.on("error",u=>{g??=u});let f,m;try{({bytes:f,rows:m}=await P(i.body,l))}catch(u){throw await v(l,s?.stage),u}return s!==void 0&&(await _(l,s,()=>g),e.logger.success(`wrote ${String(m)} rows to ${s.path} (${String(f)} bytes)`)),{bytes:f,code:0,data:t===void 0?void 0:{bytes:f,out:t,rows:m,tables:o},rows:m}};export{H as runExportCommand};