import{createProcessOutputBuffer}from"./process-output.js";import{resolvePnpmInvocation}from"./pnpm-invocation.js";import{join}from"node:path";import{existsSync}from"node:fs";import{spawn}from"node:child_process";function spawnPnpm(n,r,i={}){return new Promise(a=>{let o=resolvePnpmInvocation([`--dir`,n,...existsSync(join(n,`pnpm-workspace.yaml`))?[]:[`--ignore-workspace`],...r]),s=i.onOutput&&createProcessOutputBuffer(i.onOutput),c=spawn(o.command,[...o.args],{cwd:n,stdio:i.onOutput?[`inherit`,`pipe`,`pipe`]:`inherit`,shell:o.shell});c.stdout?.on(`data`,e=>s?.write(`stdout`,e)),c.stderr?.on(`data`,e=>s?.write(`stderr`,e));let l=!1;function settle(e){l||(l=!0,s?.flush(),a(e))}function reportFailure(e){i.onOutput?i.onOutput({stream:`stderr`,text:e}):process.stderr.write(`\n${e}\n`)}c.on(`error`,e=>{e.code===`ENOENT`?reportFailure(`pnpm not found. Install it before running this step.`):reportFailure(`pnpm ${r.join(` `)} failed: ${e.message}`),settle(!1)}),c.on(`close`,e=>settle(e===0))})}function runPnpmInstall(e,t={}){return spawnPnpm(e,[`install`,`--no-frozen-lockfile`],t)}export{runPnpmInstall,spawnPnpm};