@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.66 kB
JavaScript
import{existsSync as S}from"node:fs";import{mkdtemp as k,writeFile as v,rm as R}from"node:fs/promises";import{tmpdir as T}from"node:os";import{discoverSchema as $,schemaFromIr as j}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{i as x}from"../packem_shared/admin-token-rxXZbVMZ.mjs";import{d as z}from"../packem_shared/command-DwC11Cwe.mjs";import{a as I}from"../packem_shared/resolve-target-C_ZloTvd.mjs";import{b as A}from"../packem_shared/tui-prompts-Dd0pDuNr.mjs";import{runResetCommand as P}from"./runResetCommand.mjs";import{r as U}from"../packem_shared/import-Db3Gk4mx.mjs";const b=e=>e===""?!1:x(e),L=(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:""}),N=(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&&(e.prod===!0||!b(e.url)))return e.logger.error("--reset only clears local .wrangler/state and cannot be combined with --prod or a remote --url"),l(1)},_=async(e,r,t,n)=>{const i=await k(m(T(),"lunora-seed-")),s=m(i,"rows.ndjson");await v(s,e,"utf8");try{const o=await U({batchSize:n.batchSize,cwd:t,fetchImpl:n.fetchImpl,file:s,logger:n.logger,prod:n.prod,token:n.token,url:n.url}),d=o.body?.conflicts??0;return d>0&&n.logger.warn(`${String(d)} 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:d,generated:r,inserted:o.inserted,ndjson:e}}finally{await R(i,{force:!0,recursive:!0}).catch(()=>{})}},B=(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)},D=async(e,r)=>{if(!(e.prod===!0||!b(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??A)(`This will insert ${String(r)} generated row(s) into ${e.url??"the production worker"}. Continue?`))return e.logger.info("seed: aborted"),l(1)},H=async e=>{const r=e.cwd??process.cwd(),t=m(r,"lunora","schema.ts"),n=N(e,t);if(n!==void 0)return n;const i=new F({skipAddingFilesFromTsConfig:!0}),s=$(i,t),o=B(e,s);if(o!==void 0)return o;const d=j(s),g=C(d,{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},L));const a=u.length>0?`${u.join(`
`)}
`:"",c=u.length;if(e.dryRun===!0)return a.length>0&&process.stdout.write(a),e.logger.info(`generated ${String(c)} row(s) across ${String(g.length)} table(s) — dry run, nothing inserted`),{code:0,conflicts:0,generated:c,inserted:0,ndjson:a};if(e.reset===!0){const f=await P({cwd:r,logger:e.logger,yes:!0});if(f.code!==0)return{code:f.code,conflicts:0,generated:c,inserted:0,ndjson:a}}if(c===0)return e.logger.warn("no rows generated — nothing to insert"),{code:0,conflicts:0,generated:0,inserted:0,ndjson:a};const w=await D(e,c);return w!==void 0?w:_(a,c,r,e)},p=z(async({cwd:e,logger:r,options:t})=>({code:(await H({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:I({cwd:e,prod:t.prod===!0,url:t.url}),yes:t.yes===!0})).code}));export{p as execute,H as runSeedCommand};