import{spawn as R}from"child_process";import{readdirSync as h,readFileSync as b}from"fs";import{dirname as y,resolve as v}from"path";import{fileURLToPath as x}from"url";import u from"picocolors";function w(){let t=x(import.meta.url),e=y(t),r=e,a="/";for(;r!==a;){try{let i=h(r);if(i.includes("pnpm-workspace.yaml"))return r;if(i.includes("package.json")){let s=v(r,"package.json");if(JSON.parse(b(s,"utf-8")).workspaces)return r}}catch{}r=y(r)}return e.split("/packages/")[0]||e}async function d(t){let{command:e,args:r,cwd:a=w(),env:c={},interactive:i=!1,label:s=e,dryRun:f=!1}=t;if(f){let o={mode:"dry-run",timestamp:new Date().toISOString(),command:e,args:r,cwd:a,env:Object.keys(c).length>0?c:void 0};return process.stdout.write(JSON.stringify(o,null,2)+`
`),{exitCode:0,stdout:JSON.stringify(o),stderr:""}}return i&&!process.env.NEBUTRA_QUIET&&process.stderr.write(u.cyan(`\u2192 ${s}: ${e} ${r.join(" ")}
`)),new Promise(o=>{let l=R(e,r,{cwd:a,env:{...process.env,...c},stdio:i?"inherit":["pipe","pipe","pipe"]}),p="",m="";i||(l.stdout?.on("data",n=>{p+=n.toString()}),l.stderr?.on("data",n=>{m+=n.toString()})),l.on("close",n=>{let g=n??1;!i&&!process.env.NEBUTRA_QUIET&&(g===0?process.stderr.write(u.green(`\u2713 ${s} completed
`)):process.stderr.write(u.red(`\u2716 ${s} failed with exit code ${g}
`))),o({exitCode:g,stdout:p,stderr:m})}),l.on("error",n=>{process.stderr.write(u.red(`\u2716 Failed to execute ${e}: ${n.message}
`)),o({exitCode:1,stdout:p,stderr:n.message})})})}async function P(t,e){let r=["run"];return e?.filter&&r.push("--filter",e.filter),r.push(t),e?.args&&r.push("--",...e.args),d({command:"pnpm",args:r,dryRun:e?.dryRun,interactive:e?.interactive??!0,label:`pnpm run ${t}`})}async function N(t,e){let r=["run",t];return e?.filter&&r.push("--filter",e.filter),d({command:"turbo",args:r,dryRun:e?.dryRun,interactive:!0,label:`turbo run ${t}`})}async function C(t,e){let r=["compose"];return e?.profile&&r.push("--profile",e.profile),e?.file&&r.push("-f",e.file),r.push(...t.split(" ")),d({command:"docker",args:r,dryRun:e?.dryRun,interactive:!0,label:`docker compose ${t}`})}async function T(t,e){let r=["prisma",t];return e?.args&&r.push(...e.args),d({command:"npx",args:r,dryRun:e?.dryRun,interactive:e?.interactive??!0,label:`prisma ${t}`})}export{w as a,d as b,P as c,N as d,C as e,T as f};
