@lunora/cli
Version:
The Lunora CLI: init, dev, deploy, codegen, migrate, seed, doctor, insights, logs, registry, and the rest of the project commands
2 lines (1 loc) • 2.06 kB
JavaScript
import{spawn as C}from"node:child_process";import{StringDecoder as v}from"node:string_decoder";import{LunoraError as g}from"@lunora/errors";const D=/[\s"%&<>^|]/u,L=500,N=e=>{let i="",r=0;for(const o of e)o==="\\"?r+=1:(i+=o==='"'?`${"\\".repeat(r*2+1)}"`:`${"\\".repeat(r)}${o}`,r=0);return i+"\\".repeat(r*2)},_=(e,i,r=process.platform)=>{if(r!=="win32"||e===process.execPath)return{args:[...i],command:e,shell:!1};const o=s=>s===""?'""':D.test(s)?`"${N(s)}"`:s;return{args:i.map(s=>o(s)),command:o(e),shell:!0}},x=e=>new Promise((i,r)=>{const o=typeof e.input=="string",s=e.captureStdout===!0,a=e.captureStdoutSilently===!0;let u="inherit";s||a?u="pipe":e.stdoutToStderr&&(u=2);const c=e.captureStderr===!0||e.captureStderrSilently===!0,d=_(e.command,e.args),t=C(d.command,d.args,{cwd:e.cwd??process.cwd(),env:e.env?{...process.env,...e.env}:process.env,shell:d.shell,detached:e.timeoutMs!==void 0&&process.platform!=="win32",stdio:[o?"pipe":"inherit",u,c?"pipe":"inherit"]}),p=e.timeoutMs===void 0?void 0:setTimeout(()=>{process.platform==="win32"||t.pid===void 0?t.kill("SIGKILL"):process.kill(-t.pid,"SIGKILL")},e.timeoutMs),f=new v("utf8"),m=new v("utf8");let w="",S="";c&&t.stderr&&t.stderr.on("data",n=>{S+=m.write(n),e.captureStderr===!0&&process.stderr.write(n)}),(s||a)&&t.stdout&&t.stdout.on("data",n=>{w+=f.write(n),s&&process.stdout.write(n)});let l=!1;t.on("error",n=>{if(clearTimeout(p),l=!0,n.code==="ENOENT"){r(new g("LOCAL_DEPENDENCY_MISSING",`\`${e.command}\` is not installed or not on your PATH — nothing was run.`,{cause:n}));return}r(n)});let h=0,T;const E=()=>{l||(l=!0,clearTimeout(T),clearTimeout(p),i({code:h,stderr:c?S+m.end():void 0,stdout:s||a?w+f.end():void 0}))};t.on("exit",(n,I)=>{h=n??(I?1:0),T=setTimeout(()=>{t.stdout?.destroy(),t.stderr?.destroy(),E()},L)}),t.on("close",E),o&&t.stdin&&(t.stdin.on("error",()=>{}),t.stdin.end(e.input))}),P=(e=0)=>{const i=[];return{calls:i,spawner:o=>(i.push({descriptor:o}),Promise.resolve({code:e}))}};export{P as createRecordingSpawner,x as defaultSpawner,_ as spawnShellCompat};