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.59 kB
import{randomUUID as b}from"node:crypto";import{createWriteStream as x}from"node:fs";import{unlink as E,rename as S}from"node:fs/promises";import{r as T}from"./admin-token-VdUnvnKW.mjs";import{r as O}from"./admin-url-BhF5ufg1.mjs";import{EXIT_CODE as w,isRefusal as $,exitCodeForStatus as N}from"./EXIT_CODE-08cwt3MK.mjs";import{E as U}from"./shared-D-zCOmgY.mjs";const k=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 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)})},I=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 p(r,l),o=o.slice(s+1),s=o.indexOf(` `)}}return o.length>0&&(n+=1,await p(r,`${o} `)),{bytes:d,rows:n}}finally{t.releaseLock()}},v=async(e,r)=>{if(r!==void 0){e.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},P=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}},y="export --format json needs a file destination (--out <file>) — with --out - the NDJSON stream already owns stdout.",_=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}},D=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=O(e.url,e.logger,e.cwd);if(r===void 0)return{refused:w.USAGE};const{token:t}=T({cwd:e.cwd??process.cwd(),token:e.token,url:r});if(!t)return e.logger.error("admin token required — pass --token, set LUNORA_ADMIN_TOKEN, or add it to .dev.vars (local targets only)"),{refused:w.AUTH};const a=e.fetchImpl??globalThis.fetch;if(typeof a!="function")throw new TypeError("no fetch implementation available — pass fetchImpl or run on Node >= 18");return{fetchImpl:a,requestUrl:`${r}${U}`,tables:k(e.tables),token:t}},G=async e=>{const r=_(e);if(r===void 0)return{bytes:0,code:w.USAGE,error:y,rows:0};const{destination:t}=r,a=D(e);if($(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}.${b()}.partial`},l=s===void 0?process.stdout:x(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 I(i.body,l))}catch(u){throw await v(l,s?.stage),u}return s!==void 0&&(await P(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{G as runExportCommand};