UNPKG

@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) • 1.57 kB
import{spawn as m}from"node:child_process";import{StringDecoder as S}from"node:string_decoder";import{LunoraError as E}from"@lunora/errors";const h=/[\s"%&<>^|]/u,N=/(\\*)"/gu,g=/(\\+)$/u,_=(t,s,u=process.platform)=>{if(u!=="win32"||t===process.execPath)return{args:[...s],command:t,shell:!1};const o=e=>e===""?'""':h.test(e)?`"${e.replaceAll(N,String.raw`$1$1\"`).replace(g,"$1$1")}"`:e;return{args:s.map(e=>o(e)),command:o(t),shell:!0}},$=t=>new Promise((s,u)=>{const o=typeof t.input=="string",e=t.captureStdout===!0,a=t.captureStdoutSilently===!0;let c="inherit";e||a?c="pipe":t.stdoutToStderr&&(c=2);const d=_(t.command,t.args),n=m(d.command,d.args,{cwd:t.cwd??process.cwd(),env:t.env?{...process.env,...t.env}:process.env,shell:d.shell,stdio:[o?"pipe":"inherit",c,t.captureStderr===!0?"pipe":"inherit"]}),i=new S("utf8"),p=new S("utf8");let l="",w="";t.captureStderr===!0&&n.stderr&&n.stderr.on("data",r=>{w+=p.write(r),process.stderr.write(r)}),(e||a)&&n.stdout&&n.stdout.on("data",r=>{l+=i.write(r),e&&process.stdout.write(r)}),n.on("error",r=>{if(r.code==="ENOENT"){u(new E("LOCAL_DEPENDENCY_MISSING",`\`${t.command}\` is not installed or not on your PATH — nothing was run.`,{cause:r}));return}u(r)}),n.on("exit",(r,f)=>{s({code:r??(f?1:0),stderr:t.captureStderr===!0?w+p.end():void 0,stdout:e||a?l+i.end():void 0})}),o&&n.stdin&&(n.stdin.on("error",()=>{}),n.stdin.end(t.input))}),I=(t=0)=>{const s=[];return{calls:s,spawner:o=>(s.push({descriptor:o}),Promise.resolve({code:t}))}};export{I as createRecordingSpawner,$ as defaultSpawner,_ as spawnShellCompat};