UNPKG

@lunora/cli

Version:

The Lunora CLI: init, dev, deploy, codegen, migrate, seed, doctor, insights, logs, registry, and the rest of the project commands

4 lines (3 loc) 3.67 kB
import{existsSync as S}from"node:fs";import{mkdtemp as k,writeFile as v,rm as R}from"node:fs/promises";import{tmpdir as $}from"node:os";import{discoverSchema as j,schemaFromIr as T}from"@lunora/codegen";import{seedPlan as C}from"@lunora/seed";import{join as m}from"@visulima/path";import{Project as F}from"ts-morph";import{t as b}from"../packem_shared/admin-token-VdUnvnKW.mjs";import{d as x}from"../packem_shared/command-DwC11Cwe.mjs";import{r as z}from"../packem_shared/resolve-target-BRvw8Pf7.mjs";import{b as I}from"../packem_shared/tui-prompts-DzYd2fL3.mjs";import{runResetCommand as A}from"./runResetCommand.mjs";import{r as P}from"../packem_shared/import-Dqd1MOOv.mjs";const N=(e,r)=>typeof r=="bigint"?Number(r):r instanceof ArrayBuffer?[...new Uint8Array(r)]:r,l=e=>({code:e,conflicts:0,generated:0,inserted:0,ndjson:""}),U=(e,r)=>{if(!S(r))return e.logger.error(`schema not found: ${r} — run \`vis generate lunora-table --name=<name>\` to create one`),l(1);if(e.reset===!0&&b({prod:e.prod,url:e.url}))return e.logger.error("--reset only clears local .wrangler/state and cannot be combined with --prod or a remote --url"),l(1)},W=async(e,r,t,n)=>{const d=await k(m($(),"lunora-seed-")),i=m(d,"rows.ndjson");await v(i,e,"utf8");try{const o=await P({batchSize:n.batchSize,cwd:t,fetchImpl:n.fetchImpl,file:i,logger:n.logger,prod:n.prod,token:n.token,url:n.url,yes:!0}),s=o.body?.conflicts??0;return s>0&&n.logger.warn(`${String(s)} row(s) skipped — their _id already exists. Seeding is deterministic; re-run with --reset to wipe local state first, or a different --seed for fresh ids.`),{code:o.code,conflicts:s,generated:r,inserted:o.inserted,ndjson:e}}finally{await R(d,{force:!0,recursive:!0}).catch(()=>{})}},_=(e,r)=>{if(e.table===void 0||r.tables.some(n=>n.name===e.table))return;const t=r.tables.map(n=>n.name).join(", ");return e.logger.error(`unknown table "${e.table}" — schema defines: ${t||"(no tables)"}`),l(1)},B=async(e,r)=>{if(!b({prod:e.prod,url:e.url})||e.yes===!0)return;if(!process.stdin.isTTY&&e.confirm===void 0)return e.logger.error("seed: refusing to insert into a non-local target without confirmation — re-run with --yes"),l(1);if(!await(e.confirm??I)(`This will insert ${String(r)} generated row(s) into ${e.url??"the production worker"}. Continue?`))return e.logger.info("seed: aborted"),l(1)},D=async e=>{const r=e.cwd??process.cwd(),t=m(r,"lunora","schema.ts"),n=U(e,t);if(n!==void 0)return n;const d=new F({skipAddingFilesFromTsConfig:!0}),i=j(d,t),o=_(e,i);if(o!==void 0)return o;const s=T(i),g=C(s,{defaultCount:e.count??10,now:e.now,only:e.table===void 0?void 0:[e.table],seed:e.seed??0}),u=[];for(const{rows:f,table:h}of g)for(const y of f)u.push(JSON.stringify({doc:y,table:h},N));const c=u.length>0?`${u.join(` `)} `:"",a=u.length;if(e.dryRun===!0)return c.length>0&&process.stdout.write(c),e.logger.info(`generated ${String(a)} row(s) across ${String(g.length)} table(s) — dry run, nothing inserted`),{code:0,conflicts:0,generated:a,inserted:0,ndjson:c};if(a===0)return e.logger.warn("no rows generated — nothing to insert"),{code:0,conflicts:0,generated:0,inserted:0,ndjson:c};if(e.reset===!0){const f=await A({confirm:e.confirm,cwd:r,logger:e.logger,yes:e.yes});if(f.code!==0)return{code:f.code,conflicts:0,generated:a,inserted:0,ndjson:c}}const w=await B(e,a);return w!==void 0?w:W(c,a,r,e)},Z=x(async({cwd:e,logger:r,options:t})=>({code:(await D({batchSize:t.batchSize,count:t.count,cwd:e,dryRun:t.dryRun===!0,logger:r,prod:t.prod===!0,reset:t.reset===!0,now:t.now,seed:t.seed,table:t.table,token:t.token,url:z({cwd:e,prod:t.prod===!0,url:t.url}),yes:t.yes===!0})).code}));export{Z as execute,D as runSeedCommand};