@lunora/cli
Version:
The Lunora CLI: init, dev, deploy, codegen, migrate, seed, doctor, insights, logs, registry, and the rest of the project commands
3 lines (2 loc) • 5.45 kB
JavaScript
import{randomUUID as L}from"node:crypto";import{readdirSync as $,readFileSync as g,existsSync as d,rmSync as y,mkdtempSync as U,mkdirSync as k,copyFileSync as M,writeFileSync as R,lstatSync as C}from"node:fs";import{tmpdir as E}from"node:os";import{join as l,resolve as w,dirname as h}from"node:path";import{resolveSchemaDirectory as P,resolveLazyFunctions as A,DEFAULT_BUNDLE_META_PATH as z}from"@lunora/config";import{p as I}from"../packem_shared/api-spec-lYyq4vL6.mjs";import{d as j}from"../packem_shared/command-BpMv2n0o.mjs";import{p as x,v as N,r as O}from"./handler.mjs";import{defaultSpawner as F}from"../packem_shared/createRecordingSpawner-DchLi0gR.mjs";import{gzipSync as W}from"node:zlib";import{toolchainExecArgs as H,detectPackageManager as K}from"@lunora/config/cli";const v=64*1024*1024,Y=e=>!e.endsWith(".map")&&e!=="bundle-meta.json"&&e!=="README.md",Z=e=>{let r;try{r=$(e,{recursive:!0,withFileTypes:!0})}catch{return}let n=0,t=0,i=0;for(const o of r){if(!o.isFile()||!Y(o.name))continue;const c=g(l(o.parentPath,o.name));n+=1,i+=c.byteLength,t+=W(c).byteLength}return n===0?void 0:{files:n,gzipBytes:t,rawBytes:i}},J=1e3,b=200,D=3,V="https://lunora.sh/docs/startup-cpu-limit",q=/Active:\s+([\d.]+) ms/u,G=12e4,Q=e=>{const r=l(e,P(e),"_generated","functions.ts");return d(r)&&g(r,"utf8").includes("export const loadAllFunctions")},X=async({bundleFile:e,cwd:r,spawner:n})=>{const t=[],i=l(h(e),"startup.cpuprofile"),{args:o,command:c}=H(K(r),{args:["check","startup","--worker",e,"--outfile",i],tool:"wrangler"});for(let a=0;a<D;a+=1){let u;try{const s=await n({args:o,captureStderrSilently:!0,captureStdoutSilently:!0,command:c,cwd:r,timeoutMs:G});if(s.code!==0)return{skip:`\`wrangler check startup\` exited with code ${String(s.code)}`};u=s.stdout}catch(s){return{skip:`\`wrangler check startup\` could not run (${s instanceof Error?s.message:String(s)})`}}const f=q.exec(u??"");if(f?.[1]===void 0)return{skip:"`wrangler check startup` printed no Active time"};t.push(Number(f[1]))}return t.sort((a,u)=>a-u),{median:t[Math.floor(t.length/2)]}},ee=async e=>{const{bundleFile:r,cwd:n,logger:t}=e;if(!d(w(n,r))){t.debug?.(`startup check skipped: no upload form at ${r}`);return}const i=await X(e);if("skip"in i){t.debug?.(`startup check skipped: ${i.skip}`);return}const{median:o}=i,c=`startup: ${o.toFixed(0)} ms local CPU (median of ${String(D)} \`wrangler check startup\` runs)`;if(o<=b)return t.info(c),o;const a=Q(n)?"":" This build is eager: `--lazy-functions` (or LUNORA_LAZY_FUNCTIONS=1) defers function modules to first use.";return t.warn(`${c} is above the ${String(b)} ms warning line. Cloudflare rejects a Worker whose startup exceeds ${String(J)} ms of ITS CPU (error 10021), and how local CPU maps to Cloudflare's is unknown: a 0.4–0.7 s local profile has been rejected, so the line is a heuristic, not a prediction.${a} See ${V}`),o},_=".lunora/build",te=async e=>F({...e,stdoutToStderr:!0}),B=e=>`${(e/1024).toFixed(1)} KiB`,T=e=>d(e)?$(e,{encoding:"utf8",recursive:!0}).map(r=>{try{const n=C(l(e,r));return`${r}:${String(n.size)}:${String(n.mtimeMs)}`}catch{return`${r}:gone`}}).join(`
`):"",re=(e,r,n)=>{const t=Z(e);if(t===void 0)return n.warn(`could not weigh the bundle — nothing uploadable was found in ${r}`),t;const i=(t.rawBytes/v*100).toFixed(1),o=`bundle: ${B(t.rawBytes)} raw across ${String(t.files)} file(s) — ${i}% of Cloudflare's 64 MiB uncompressed Worker size limit (${B(t.gzipBytes)} gzipped, for reference; there is no compressed limit)`;return t.rawBytes>v?n.warn(`${o} — Cloudflare will reject this upload`):n.info(o),t},S=e=>{if(e!==void 0)try{y(h(e),{force:!0,recursive:!0})}catch{}},oe=async(e,r,n,t)=>{if(r!==void 0)try{await ee({bundleFile:r,cwd:e,logger:n,spawner:t})}finally{S(r)}},ne=async e=>{const r=e.outDir??_,n=e.format==="json",{logger:t}=e,i=e.cwd??process.cwd(),o=w(i,r,"bundle-meta.json"),c=N(i,L()),a=d(o)?g(o):void 0;y(o,{force:!0}),y(c,{force:!0});const u=w(i,r),f=T(u);let s;const m=e.startupCheck===!1?void 0:l(U(l(E(),"lunora-startup-")),"worker.bundle");try{s=await O({allowSchemaDrift:e.allowSchemaDrift,apiSpec:e.apiSpec,bundleMetaPath:c,frameworkBuild:e.frameworkBuild,lazyFunctions:A(e.lazyFunctions),commandName:"build",clientBuildOutputJson:n,cwd:e.cwd,dryRun:!0,emitBindings:e.emitBindings,format:void 0,interactive:n?!1:void 0,logger:t,outDir:r,outFile:m,smokeLauncher:e.smokeLauncher,smokeLoad:e.smokeLoad,spawner:e.spawner??(n?te:void 0),strictAdvisories:e.strictAdvisories,target:e.target});const p=e.frameworkBuild===!1?l(i,z):c;s.code===0&&!d(o)&&d(p)&&(k(h(o),{recursive:!0}),M(p,o))}finally{s===void 0&&S(m),y(c,{force:!0}),a!==void 0&&!d(o)&&T(u)===f&&s?.code!==0&&(k(h(o),{recursive:!0}),R(o,a))}if(s.code!==0)return S(m),s;try{t.success(`build complete — bundle written to ${r}`);const p=re(w(e.cwd??process.cwd(),r),r,t);return await oe(i,m,t,e.spawner??F),{...s,bundle:p}}finally{S(m)}},we=j(async({cwd:e,format:r,logger:n,options:t})=>{const i=x(e,t.root),o=await ne({allowSchemaDrift:t.allowSchemaDrift===!0,apiSpec:I(t.apiSpec),lazyFunctions:A(t.lazyFunctions),cwd:i,emitBindings:t.emitBindings,format:r,frameworkBuild:t.frameworkBuild,logger:n,outDir:t.outDir,smokeLoad:t.smokeLoad,startupCheck:t.startupCheck,strictAdvisories:t.strictAdvisories,target:t.target});return{code:o.code,data:{bundle:o.bundle,deployment:o.deployment,outDir:t.outDir??_,validation:o.validation},error:o.error}});export{we as execute,ne as runBuildCommand};