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