Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Aquaticat/Monochromatic. TypeScript and Rust monorepo: Claude Code and Pi coding-agent plugins, a m
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138/** Captured-output pnpm invocations used by the diagnosis.
The runner is a parameter everywhere it is used so tests substitute a fake that simulates pnpm's file writes without network access.
@module */
import spawn, { SubprocessError, } from 'nano-spawn';
import { tagged, } from '@monochromatic-dev/module-logger/ts';
/** Module logger for pnpm invocations. */const l = tagged({ tag: 'deps-update/pnpm', },);
//region Types
/** Runs pnpm with arguments in a directory and returns its stdout. */export type RunPnpm = (options: { readonly args: readonly string[]; readonly cwd: string;},) => Promise<string>;
//endregion Types
//region Errors
/** Thrown when a captured pnpm invocation exits unsuccessfully. */export class PnpmCommandError extends Error { /** pnpm's stdout and stderr interleaved; pnpm prints `ERR_PNPM_*` diagnostics on either stream. */ readonly output: string;
/** Builds the error with the command line and pnpm's output.
@param args - pnpm arguments that failed
@param cwd - directory pnpm ran in
@param output - pnpm's interleaved stdout and stderr
@param cause - underlying subprocess error
@example ```ts throw new PnpmCommandError({ args: ['why', 'a'], cwd: '/repo', output: '', cause }); ``` */ constructor({ args, cwd, output, cause, }: { readonly args: readonly string[]; readonly cwd: string; readonly output: string; readonly cause: unknown; },) { super( `pnpm ${args.join(' ',)} failed in ${cwd}:\n${output}`, { cause, }, ); this.name = 'PnpmCommandError'; this.output = output; }}
//endregion Errors
//region Runner
/** Default {@link RunPnpm}: spawns the `pnpm` on `PATH` with piped output.
@param args - pnpm arguments
@param cwd - working directory
@returns pnpm's stdout
@throws PnpmCommandError when pnpm exits nonzero or cannot start
@example ```ts await runPnpm({ args: ['config', 'get', 'registry'], cwd: process.cwd() }); ``` */export async function runPnpm({ args, cwd,}: { readonly args: readonly string[]; readonly cwd: string;},): Promise<string> { /** Logger tagged with this function. */ const rl = tagged({ tag: runPnpm.name, l, },); rl.debug(`pnpm ${args.join(' ',)} (cwd ${cwd})`,); try { /** Completed subprocess result. */ const result = await spawn( 'pnpm', [...args,], { cwd, }, ); return result.stdout; } catch (error) { if (!(error instanceof SubprocessError)) throw error; rl.debug(`pnpm exited ${String(error.exitCode,)}`,); throw new PnpmCommandError({ args, cwd, output: error.output, cause: error, },); }}
//endregion Runner