|
| 1 | +// An implementation of https://socket.dev/npm/package/@npmcli/promise-spawn/overview/8.0.2 |
| 2 | +// that uses child_process.fork instead of child_process.spawn. |
| 3 | +// ISC License |
| 4 | +// Copyright (c) npm, Inc. |
| 5 | +import { fork as builtinFork } from 'node:child_process' |
| 6 | + |
| 7 | +import type { ForkOptions as BuiltinForkOptions } from 'node:child_process' |
| 8 | + |
| 9 | +type BuiltinForkResult = ReturnType<typeof builtinFork> |
| 10 | + |
| 11 | +export type ForkOptions = { |
| 12 | + cwd?: string |
| 13 | + encoding?: BufferEncoding |
| 14 | + stdioString?: boolean |
| 15 | +} & BuiltinForkOptions |
| 16 | + |
| 17 | +export type ForkResult<Output, Extra> = Promise< |
| 18 | + { |
| 19 | + cmd: string |
| 20 | + args: string[] |
| 21 | + code: number |
| 22 | + signal: NodeJS.Signals | null |
| 23 | + stdout: Output |
| 24 | + stderr: Output |
| 25 | + } & Extra |
| 26 | +> & { process: BuiltinForkResult; stdio: BuiltinForkResult['stdio'] } |
| 27 | + |
| 28 | +export function fork<O extends ForkOptions>( |
| 29 | + cmd: string, |
| 30 | + args: string[], |
| 31 | + opts?: O, |
| 32 | + extra?: Record<any, any> |
| 33 | +): ForkResult< |
| 34 | + O extends { stdioString: false } ? Buffer : string, |
| 35 | + typeof extra |
| 36 | +> { |
| 37 | + const { encoding = 'utf8', ...builtinForkOptions } = { |
| 38 | + __proto__: null, |
| 39 | + ...opts |
| 40 | + } |
| 41 | + |
| 42 | + let resolve: ((value: any) => void) | undefined |
| 43 | + let reject: ((reason?: any) => void) | undefined |
| 44 | + const promise = new Promise((res, rej) => { |
| 45 | + resolve = res |
| 46 | + reject = rej |
| 47 | + }) as ForkResult< |
| 48 | + O extends { stdioString: false } ? Buffer : string, |
| 49 | + typeof extra |
| 50 | + > |
| 51 | + |
| 52 | + const closeError = new Error('command failed') |
| 53 | + const stdout: Buffer[] = [] |
| 54 | + const stderr: Buffer[] = [] |
| 55 | + |
| 56 | + const getResult = (result: Record<string, any>) => ({ |
| 57 | + cmd, |
| 58 | + args, |
| 59 | + ...result, |
| 60 | + stdout: Buffer.concat(stdout).toString(encoding), |
| 61 | + stderr: Buffer.concat(stderr).toString(encoding), |
| 62 | + ...extra |
| 63 | + }) |
| 64 | + |
| 65 | + const rejectWithOpts = (er: Error, erOpts: Record<string, any>) => { |
| 66 | + const resultError = getResult(erOpts) |
| 67 | + reject?.(Object.assign(er, resultError)) |
| 68 | + } |
| 69 | + |
| 70 | + const proc = builtinFork(cmd, args, builtinForkOptions) |
| 71 | + .on('error', error => rejectWithOpts(error, {})) |
| 72 | + .on('close', (code, signal) => { |
| 73 | + if (code !== 0 || signal) { |
| 74 | + rejectWithOpts(closeError, { code, signal }) |
| 75 | + } else { |
| 76 | + resolve?.(getResult({ code, signal })) |
| 77 | + } |
| 78 | + }) |
| 79 | + proc.stdout |
| 80 | + ?.on('data', chunk => stdout.push(chunk)) |
| 81 | + .on('error', error => rejectWithOpts(error, {})) |
| 82 | + proc.stderr |
| 83 | + ?.on('data', chunk => stderr.push(chunk)) |
| 84 | + .on('error', error => rejectWithOpts(error, {})) |
| 85 | + ;(promise as any).stdin = proc.stdin |
| 86 | + ;(promise as any).process = proc |
| 87 | + return promise |
| 88 | +} |
0 commit comments