Socket
Book a DemoSign in
Socket

@vltpkg/promise-spawn

Package Overview
Dependencies
Maintainers
6
Versions
53
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@vltpkg/promise-spawn - npm Package Compare versions

Comparing version
1.0.0-rc.18
to
1.0.0-rc.22
+22
-18
package.json
{
"name": "@vltpkg/promise-spawn",
"description": "Spawn a process and return a promise that resolves when it finishes",
"version": "1.0.0-rc.18",
"version": "1.0.0-rc.22",
"repository": {

@@ -10,5 +10,8 @@ "type": "git",

},
"author": "vlt technology inc. <support@vlt.sh> (http://vlt.sh)",
"author": {
"name": "vlt technology inc.",
"email": "support@vlt.sh"
},
"dependencies": {
"@vltpkg/error-cause": "1.0.0-rc.18"
"@vltpkg/error-cause": "1.0.0-rc.22"
},

@@ -28,4 +31,15 @@ "devDependencies": {

"engines": {
"node": ">=22.9.0"
"node": ">=22.22.0"
},
"scripts": {
"format": "prettier --write . --log-level warn --ignore-path ../../.prettierignore --cache",
"format:check": "prettier --check . --ignore-path ../../.prettierignore --cache",
"lint": "eslint . --fix",
"lint:check": "eslint .",
"prepack": "tsc -p tsconfig.publish.json && ../../scripts/update-dist-exports.ts",
"snap": "tap",
"test": "tap",
"posttest": "tsc --noEmit",
"typecheck": "tsc --noEmit"
},
"tap": {

@@ -35,3 +49,3 @@ "extends": "../../tap-config.yaml"

"prettier": "../../.prettierrc.js",
"module": "./dist/index.js",
"module": "./src/index.ts",
"type": "module",

@@ -42,3 +56,3 @@ "exports": {

"import": {
"default": "./dist/index.js"
"default": "./src/index.ts"
}

@@ -49,13 +63,3 @@ }

"dist"
],
"scripts": {
"format": "prettier --write . --log-level warn --ignore-path ../../.prettierignore --cache",
"format:check": "prettier --check . --ignore-path ../../.prettierignore --cache",
"lint": "eslint . --fix",
"lint:check": "eslint .",
"snap": "tap",
"test": "tap",
"posttest": "tsc --noEmit",
"typecheck": "tsc --noEmit"
}
}
]
}
import type { ChildProcess, IOType, SpawnOptions, StdioOptions } from 'node:child_process';
export type IOTypeNoPipe = Exclude<IOType, IOTypePipe>;
export type IOTypePipe = 'overlapped' | 'pipe' | null | undefined;
export interface PromiseSpawnOptions extends SpawnOptions {
stdioString?: boolean;
acceptFail?: boolean;
}
export interface PromiseSpawnOptionsString extends PromiseSpawnOptions {
stdioString?: true;
}
export interface PromiseSpawnOptionsBuffer extends PromiseSpawnOptions {
stdioString: false;
}
export interface PromiseSpawnOptionsStdin extends PromiseSpawnOptions {
stdio?: Exclude<IOTypePipe, null> | [
stdin?: IOTypePipe,
stdout?: Exclude<StdioOptions, IOType>[number],
stderr?: Exclude<StdioOptions, IOType>[number]
];
}
export interface PromiseSpawnOptionsNoStdin extends PromiseSpawnOptions {
stdio: IOTypeNoPipe | IOTypeNoPipe[] | [
stdin: IOTypeNoPipe | number,
stdout?: Exclude<StdioOptions, IOType>[number],
stderr?: Exclude<StdioOptions, IOType>[number]
];
}
export interface PromiseSpawnOptionsStdout extends PromiseSpawnOptions {
stdio?: Exclude<IOTypePipe, null> | [
stdin: Exclude<StdioOptions, IOType>[number] | undefined,
stdout?: IOTypePipe,
stderr?: Exclude<StdioOptions, IOType>[number]
];
}
export interface PromiseSpawnOptionsStdoutString extends PromiseSpawnOptionsStdout {
stdioString?: true;
}
export interface PromiseSpawnOptionsStdoutBuffer extends PromiseSpawnOptionsStdout {
stdioString: false;
}
export interface PromiseSpawnOptionsNoStdout extends PromiseSpawnOptions {
stdio: IOTypeNoPipe | IOTypeNoPipe[] | [
stdin: Exclude<StdioOptions, IOType>[number] | undefined,
stdout?: IOTypeNoPipe,
stderr?: Exclude<StdioOptions, IOType>[number]
];
}
export interface PromiseSpawnOptionsStderr extends PromiseSpawnOptions {
stdio?: Exclude<IOTypePipe, null> | [
stdin: Exclude<StdioOptions, IOType>[number] | undefined,
stdout: Exclude<StdioOptions, IOType>[number] | undefined,
stderr?: IOTypePipe
];
}
export interface PromiseSpawnOptionsStderrString extends PromiseSpawnOptionsStderr {
stdioString?: true;
}
export interface PromiseSpawnOptionsStderrBuffer extends PromiseSpawnOptionsStderr {
stdioString: false;
}
export interface PromiseSpawnOptionsNoStderr extends PromiseSpawnOptions {
stdio: IOTypeNoPipe | IOTypeNoPipe[] | [
stdin: Exclude<StdioOptions, IOType>[number] | undefined,
stdout: Exclude<StdioOptions, IOType>[number] | undefined,
stderr: IOTypeNoPipe
];
}
export interface SpawnResult {
command: string;
args: string[];
cwd: string;
status: number | null;
signal: NodeJS.Signals | null;
stdout: Buffer | string | null;
stderr: Buffer | string | null;
}
export interface SpawnResultString extends SpawnResult {
stdout: string | null;
stderr: string | null;
}
export interface SpawnResultBuffer extends SpawnResult {
stdout: Buffer | null;
stderr: Buffer | null;
}
export interface SpawnResultStdout extends SpawnResult {
stdout: Buffer | string;
}
export interface SpawnResultStdoutString extends SpawnResultString {
stdout: string;
}
export interface SpawnResultStdoutBuffer extends SpawnResultBuffer {
stdout: Buffer;
}
export interface SpawnResultNoStdout extends SpawnResult {
stdout: null;
}
export interface SpawnResultStderr extends SpawnResult {
stderr: Buffer | string;
}
export interface SpawnResultStderrString extends SpawnResultString {
stderr: string;
}
export interface SpawnResultStderrBuffer extends SpawnResultBuffer {
stderr: Buffer;
}
export interface SpawnResultNoStderr extends SpawnResult {
stderr: null;
}
export interface SpawnResultNoStdio extends SpawnResult {
stderr: null;
stdout: null;
}
export interface SpawnResultStdioStrings extends SpawnResult {
stdout: string;
stderr: string;
}
export interface SpawnResultStdioBuffers extends SpawnResult {
stdout: Buffer;
stderr: Buffer;
}
export type Override<T, R> = Omit<T, keyof R> & R;
export type SpawnResultByOptions<T extends PromiseSpawnOptions> = Override<SpawnResult, {
stdout: T extends PromiseSpawnOptionsNoStdout ? null : T extends PromiseSpawnOptionsStdoutBuffer ? Buffer : T extends PromiseSpawnOptionsStdoutString ? string : T extends PromiseSpawnOptionsBuffer ? Buffer | null : T extends PromiseSpawnOptionsString ? string | null : Buffer | string | null;
stderr: T extends PromiseSpawnOptionsNoStderr ? null : T extends PromiseSpawnOptionsStderrBuffer ? Buffer : T extends PromiseSpawnOptionsStderrString ? string : T extends PromiseSpawnOptionsBuffer ? Buffer | null : T extends PromiseSpawnOptionsString ? string | null : Buffer | string | null;
}>;
export type ChildProcessByOptions<T extends PromiseSpawnOptions> = Override<ChildProcess, {
stdin: T extends PromiseSpawnOptionsNoStdin ? null : Exclude<ChildProcess['stdin'], null>;
stdout: T extends PromiseSpawnOptionsNoStdout ? null : Exclude<ChildProcess['stdout'], null>;
stderr: T extends PromiseSpawnOptionsNoStderr ? null : Exclude<ChildProcess['stderr'], null>;
}>;
/**
* Subtype of Promise returned by {@link promiseSpawn}.
*
* Resolution value is inferred from the provided options.
*/
export declare class SpawnPromise<O extends PromiseSpawnOptions, T extends object = object> extends Promise<SpawnResultByOptions<O> & T> {
[Symbol.toStringTag]: string;
/** The spawned process this promise references */
process: ChildProcessByOptions<O>;
/** Expose the child process stdin, if available */
stdin: ChildProcessByOptions<O>['stdin'];
/**
* Set static `Symbol.species` back to the base Promise class so that
* v8 doesn't get confused by the changed constructor signature.
*/
static get [Symbol.species](): PromiseConstructor;
constructor(command: string, args: string[], opts: O, extra?: T);
}
/**
* Spawn the specified command, and return a promise that resolves when
* the process closes or has an error.
*/
export declare function promiseSpawn<O extends PromiseSpawnOptions = PromiseSpawnOptionsStderrString & PromiseSpawnOptionsStdoutString, E extends object = object>(command: string, args: string[], opts?: O, extra?: E): SpawnPromise<O, E>;
//# sourceMappingURL=index.d.ts.map
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EACV,YAAY,EACZ,MAAM,EACN,YAAY,EACZ,YAAY,EACb,MAAM,oBAAoB,CAAA;AAY3B,MAAM,MAAM,YAAY,GAAG,OAAO,CAAC,MAAM,EAAE,UAAU,CAAC,CAAA;AACtD,MAAM,MAAM,UAAU,GAAG,YAAY,GAAG,MAAM,GAAG,IAAI,GAAG,SAAS,CAAA;AAEjE,MAAM,WAAW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AA2B,GAAG,IAAI,GAClD,OAAO,CAAC,YAAY,CAAC,QAAQ,CAAC,EAAE,IAAI,CAAC,CAAA;IACvC,MAAM,EAAE,CAAC,SAAS,2BAA2B,GAAG,IAAI,GAClD,OAAO,CAAC,YAAY,CAAC,QAAQ,CAAC,EAAE,IAAI,CAAC,CAAA;CACxC,CACF,CAAA;AAEH;;;;GAIG;AACH,qBAAa,YAAY,CACvB,CAAC,SAAS,mBAAmB,EAC7B,CAAC,SAAS,MAAM,GAAG,MAAM,CACzB,SAAQ,OAAO,CAAC,oBAAoB,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;IAC5C,CAAC,MAAM,CAAC,WAAW,CAAC,SAAiB;IAErC,kDAAkD;IAClD,OAAO,EAAG,qBAAqB,CAAC,CAAC,CAAC,CAAA;IAElC,mDAAmD;IACnD,KAAK,EAAG,qBAAqB,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAA;IAEzC;;;OAGG;IACH,MAAM,KAAK,CAAC,MAAM,CAAC,OAAO,CAAC,uBAE1B;gBAGC,OAAO,EAAE,MAAM,EACf,IAAI,EAAE,MAAM,EAAE,EACd,IAAI,EAAE,CAAC,EACP,KAAK,GAAE,CAAW;CAsErB;AAED;;;GAGG;AACH,wBAAgB,YAAY,CAC1B,CAAC,SAAS,mBAAmB,GAAG,+BAA+B,GAC7D,+BAA+B,EACjC,CAAC,SAAS,MAAM,GAAG,MAAM,EACzB,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,EAAE,IAAI,GAAS,CAAC,EAAE,KAAK,GAAS,CAAC,sBAEjE"}
import { error } from '@vltpkg/error-cause';
import { spawn } from 'node:child_process';
const isPipe = (stdio = 'pipe', fd) => stdio === 'pipe' || stdio === 'overlapped' || stdio === null ? true
: Array.isArray(stdio) ? isPipe(stdio[fd], fd)
: false;
function stdioResult(stdout, stderr, o) {
return {
stdout: (!isPipe(o.stdio, 1) ? null
: o.stdioString !== false ?
Buffer.concat(stdout).toString().trim()
: Buffer.concat(stdout)),
stderr: (!isPipe(o.stdio, 2) ? null
: o.stdioString !== false ?
Buffer.concat(stderr).toString().trim()
: Buffer.concat(stderr)),
};
}
/**
* Subtype of Promise returned by {@link promiseSpawn}.
*
* Resolution value is inferred from the provided options.
*/
export class SpawnPromise extends Promise {
[Symbol.toStringTag] = 'SpawnPromise';
/** The spawned process this promise references */
process;
/** Expose the child process stdin, if available */
stdin;
/**
* Set static `Symbol.species` back to the base Promise class so that
* v8 doesn't get confused by the changed constructor signature.
*/
static get [Symbol.species]() {
return Promise;
}
constructor(command, args, opts, extra = {}) {
let proc;
super((res, rej) => {
proc = spawn(command, args, opts);
const stdout = [];
const stderr = [];
// Don't use the spread operator on successResult or errorResult
// as that kills any typechecking for the known keys.
const reject = (er, status, signal) => {
const stdio = stdioResult(stdout, stderr, opts);
const errorResult = {
command,
args,
stdout: stdio.stdout,
stderr: stdio.stderr,
cwd: opts.cwd?.toString() ?? process.cwd(),
};
if (er !== undefined) {
errorResult.cause = er;
}
if (status !== undefined) {
errorResult.status = status;
}
/* c8 ignore next 3 - hard to test on win32 so coverage fails */
if (signal !== undefined) {
errorResult.signal = signal;
}
Object.assign(errorResult, extra);
rej(error('command failed', errorResult));
};
const resolve = (status, signal) => {
const stdio = stdioResult(stdout, stderr, opts);
const successResult = {
command,
args,
status,
signal,
stdout: stdio.stdout,
stderr: stdio.stderr,
cwd: opts.cwd?.toString() ?? process.cwd(),
};
Object.assign(successResult, extra);
res(successResult);
};
proc.on('error', reject);
proc.stdout
?.on('data', c => stdout.push(c))
.on('error', reject);
proc.stderr
?.on('data', c => stderr.push(c))
.on('error', reject);
proc.on('close', (status, signal) => {
if ((status || signal) && !opts.acceptFail) {
reject(undefined, status, signal);
}
else {
resolve(status, signal);
}
});
});
this.process = proc;
this.stdin = proc.stdin;
}
}
/**
* Spawn the specified command, and return a promise that resolves when
* the process closes or has an error.
*/
export function promiseSpawn(command, args, opts = {}, extra = {}) {
return new SpawnPromise(command, args,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I,CAAC,IAAI,CAAC,UAAU,EAAE,CAAC;oBAC3C,MAAM,CAAC,SAAS,EAAE,MAAM,EAAE,MAAM,CAAC,CAAA;gBACnC,CAAC;qBAAM,CAAC;oBACN,OAAO,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;gBACzB,CAAC;YACH,CAAC,CAAC,CAAA;QACJ,CAAC,CAAC,CAAA;QACF,IAAI,CAAC,OAAO,GAAG,IAAI,CAAA;QACnB,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,KAAK,CAAA;IACzB,CAAC;CACF;AAED;;;GAGG;AACH,MAAM,UAAU,YAAY,CAI1B,OAAe,EAAE,IAAc,EAAE,OAAO,EAAO,EAAE,QAAQ,EAAO;IAChE,OAAO,IAAI,YAAY,CAAO,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,CAAC,CAAA;AAC3D,CAAC","sourcesContent":["import { error } from '@vltpkg/error-cause'\nimport type { ErrorCauseOptions } from '@vltpkg/error-cause'\nimport { spawn } from 'node:child_process'\nimport type {\n ChildProcess,\n IOType,\n SpawnOptions,\n StdioOptions,\n} from 'node:child_process'\n\nconst isPipe = (\n stdio:\n | Exclude<StdioOptions, IOType>[number]\n | StdioOptions = 'pipe',\n fd: number,\n): stdio is IOTypePipe =>\n stdio === 'pipe' || stdio === 'overlapped' || stdio === null ? true\n : Array.isArray(stdio) ? isPipe(stdio[fd], fd)\n : false\n\nexport type IOTypeNoPipe = Exclude<IOType, IOTypePipe>\nexport type IOTypePipe = 'overlapped' | 'pipe' | null | undefined\n\nexport interface PromiseSpawnOptions extends SpawnOptions {\n stdioString?: boolean\n acceptFail?: boolean\n}\nexport interface PromiseSpawnOptionsString extends PromiseSpawnOptions {\n stdioString?: true\n}\nexport interface PromiseSpawnOptionsBuffer extends PromiseSpawnOptions {\n stdioString: false\n}\nexport interface PromiseSpawnOptionsStdin extends PromiseSpawnOptions {\n stdio?:\n | Exclude<IOTypePipe, null>\n | [\n stdin?: IOTypePipe,\n stdout?: Exclude<StdioOptions, IOType>[number],\n stderr?: Exclude<StdioOptions, IOType>[number],\n ]\n}\nexport interface PromiseSpawnOptionsNoStdin extends PromiseSpawnOptions {\n stdio:\n | IOTypeNoPipe\n | IOTypeNoPipe[]\n | [\n stdin: IOTypeNoPipe | number,\n stdout?: Exclude<StdioOptions, IOType>[number],\n stderr?: Exclude<StdioOptions, IOType>[number],\n ]\n}\nexport interface PromiseSpawnOptionsStdout extends PromiseSpawnOptions {\n stdio?:\n | Exclude<IOTypePipe, null>\n | [\n stdin: Exclude<StdioOptions, IOType>[number] | undefined,\n stdout?: IOTypePipe,\n stderr?: Exclude<StdioOptions, IOType>[number],\n ]\n}\nexport interface PromiseSpawnOptionsStdoutString extends PromiseSpawnOptionsStdout {\n stdioString?: true\n}\nexport interface PromiseSpawnOptionsStdoutBuffer extends PromiseSpawnOptionsStdout {\n stdioString: false\n}\nexport interface PromiseSpawnOptionsNoStdout extends PromiseSpawnOptions {\n stdio:\n | IOTypeNoPipe\n | IOTypeNoPipe[]\n | [\n stdin: Exclude<StdioOptions, IOType>[number] | undefined,\n stdout?: IOTypeNoPipe,\n stderr?: Exclude<StdioOptions, IOType>[number],\n ]\n}\nexport interface PromiseSpawnOptionsStderr extends PromiseSpawnOptions {\n stdio?:\n | Exclude<IOTypePipe, null>\n | [\n stdin: Exclude<StdioOptions, IOType>[number] | undefined,\n stdout: Exclude<StdioOptions, IOType>[number] | undefined,\n stderr?: IOTypePipe,\n ]\n}\nexport interface PromiseSpawnOptionsStderrString extends PromiseSpawnOptionsStderr {\n stdioString?: true\n}\nexport interface PromiseSpawnOptionsStderrBuffer extends PromiseSpawnOptionsStderr {\n stdioString: false\n}\nexport interface PromiseSpawnOptionsNoStderr extends PromiseSpawnOptions {\n stdio:\n | IOTypeNoPipe\n | IOTypeNoPipe[]\n | [\n stdin: Exclude<StdioOptions, IOType>[number] | undefined,\n stdout: Exclude<StdioOptions, IOType>[number] | undefined,\n stderr: IOTypeNoPipe,\n ]\n}\n\nfunction stdioResult<O extends PromiseSpawnOptions>(\n stdout: Buffer[],\n stderr: Buffer[],\n o: O,\n): {\n stdout: SpawnResultByOptions<O>['stdout']\n stderr: SpawnResultByOptions<O>['stderr']\n} {\n return {\n stdout: (!isPipe(o.stdio, 1) ? null\n : o.stdioString !== false ?\n Buffer.concat(stdout).toString().trim()\n : Buffer.concat(stdout)) as SpawnResultByOptions<O>['stdout'],\n stderr: (!isPipe(o.stdio, 2) ? null\n : o.stdioString !== false ?\n Buffer.concat(stderr).toString().trim()\n : Buffer.concat(stderr)) as SpawnResultByOptions<O>['stderr'],\n }\n}\n\nexport interface SpawnResult {\n command: string\n args: string[]\n cwd: string\n status: number | null\n signal: NodeJS.Signals | null\n stdout: Buffer | string | null\n stderr: Buffer | string | null\n}\nexport interface SpawnResultString extends SpawnResult {\n stdout: string | null\n stderr: string | null\n}\nexport interface SpawnResultBuffer extends SpawnResult {\n stdout: Buffer | null\n stderr: Buffer | null\n}\nexport interface SpawnResultStdout extends SpawnResult {\n stdout: Buffer | string\n}\nexport interface SpawnResultStdoutString extends SpawnResultString {\n stdout: string\n}\nexport interface SpawnResultStdoutBuffer extends SpawnResultBuffer {\n stdout: Buffer\n}\nexport interface SpawnResultNoStdout extends SpawnResult {\n stdout: null\n}\nexport interface SpawnResultStderr extends SpawnResult {\n stderr: Buffer | string\n}\nexport interface SpawnResultStderrString extends SpawnResultString {\n stderr: string\n}\nexport interface SpawnResultStderrBuffer extends SpawnResultBuffer {\n stderr: Buffer\n}\nexport interface SpawnResultNoStderr extends SpawnResult {\n stderr: null\n}\nexport interface SpawnResultNoStdio extends SpawnResult {\n stderr: null\n stdout: null\n}\nexport interface SpawnResultStdioStrings extends SpawnResult {\n stdout: string\n stderr: string\n}\nexport interface SpawnResultStdioBuffers extends SpawnResult {\n stdout: Buffer\n stderr: Buffer\n}\n\nexport type Override<T, R> = Omit<T, keyof R> & R\n\nexport type SpawnResultByOptions<T extends PromiseSpawnOptions> =\n Override<\n SpawnResult,\n {\n stdout: T extends PromiseSpawnOptionsNoStdout ? null\n : T extends PromiseSpawnOptionsStdoutBuffer ? Buffer\n : T extends PromiseSpawnOptionsStdoutString ? string\n : T extends PromiseSpawnOptionsBuffer ? Buffer | null\n : T extends PromiseSpawnOptionsString ? string | null\n : Buffer | string | null\n stderr: T extends PromiseSpawnOptionsNoStderr ? null\n : T extends PromiseSpawnOptionsStderrBuffer ? Buffer\n : T extends PromiseSpawnOptionsStderrString ? string\n : T extends PromiseSpawnOptionsBuffer ? Buffer | null\n : T extends PromiseSpawnOptionsString ? string | null\n : Buffer | string | null\n }\n >\n\nexport type ChildProcessByOptions<T extends PromiseSpawnOptions> =\n Override<\n ChildProcess,\n {\n stdin: T extends PromiseSpawnOptionsNoStdin ? null\n : Exclude<ChildProcess['stdin'], null>\n stdout: T extends PromiseSpawnOptionsNoStdout ? null\n : Exclude<ChildProcess['stdout'], null>\n stderr: T extends PromiseSpawnOptionsNoStderr ? null\n : Exclude<ChildProcess['stderr'], null>\n }\n >\n\n/**\n * Subtype of Promise returned by {@link promiseSpawn}.\n *\n * Resolution value is inferred from the provided options.\n */\nexport class SpawnPromise<\n O extends PromiseSpawnOptions,\n T extends object = object,\n> extends Promise<SpawnResultByOptions<O> & T> {\n [Symbol.toStringTag] = 'SpawnPromise'\n\n /** The spawned process this promise references */\n process!: ChildProcessByOptions<O>\n\n /** Expose the child process stdin, if available */\n stdin!: ChildProcessByOptions<O>['stdin']\n\n /**\n * Set static `Symbol.species` back to the base Promise class so that\n * v8 doesn't get confused by the changed constructor signature.\n */\n static get [Symbol.species]() {\n return Promise\n }\n\n constructor(\n command: string,\n args: string[],\n opts: O,\n extra: T = {} as T,\n ) {\n let proc!: ChildProcessByOptions<O>\n super((res, rej) => {\n proc = spawn(command, args, opts) as ChildProcessByOptions<O>\n const stdout: Buffer[] = []\n const stderr: Buffer[] = []\n // Don't use the spread operator on successResult or errorResult\n // as that kills any typechecking for the known keys.\n const reject = (\n er?: Error,\n status?: number | null,\n signal?: NodeJS.Signals | null,\n ) => {\n const stdio = stdioResult(stdout, stderr, opts)\n const errorResult: ErrorCauseOptions = {\n command,\n args,\n stdout: stdio.stdout,\n stderr: stdio.stderr,\n cwd: opts.cwd?.toString() ?? process.cwd(),\n }\n if (er !== undefined) {\n errorResult.cause = er\n }\n if (status !== undefined) {\n errorResult.status = status\n }\n /* c8 ignore next 3 - hard to test on win32 so coverage fails */\n if (signal !== undefined) {\n errorResult.signal = signal\n }\n Object.assign(errorResult, extra)\n rej(error('command failed', errorResult))\n }\n const resolve = (\n status: number | null,\n signal: NodeJS.Signals | null,\n ) => {\n const stdio = stdioResult(stdout, stderr, opts)\n const successResult: SpawnResultByOptions<O> = {\n command,\n args,\n status,\n signal,\n stdout: stdio.stdout,\n stderr: stdio.stderr,\n cwd: opts.cwd?.toString() ?? process.cwd(),\n }\n Object.assign(successResult, extra)\n res(successResult as SpawnResultByOptions<O> & T)\n }\n proc.on('error', reject)\n proc.stdout\n ?.on('data', c => stdout.push(c as Buffer))\n .on('error', reject)\n proc.stderr\n ?.on('data', c => stderr.push(c as Buffer))\n .on('error', reject)\n proc.on('close', (status, signal) => {\n if ((status || signal) && !opts.acceptFail) {\n reject(undefined, status, signal)\n } else {\n resolve(status, signal)\n }\n })\n })\n this.process = proc\n this.stdin = proc.stdin\n }\n}\n\n/**\n * Spawn the specified command, and return a promise that resolves when\n * the process closes or has an error.\n */\nexport function promiseSpawn<\n O extends PromiseSpawnOptions = PromiseSpawnOptionsStderrString &\n PromiseSpawnOptionsStdoutString,\n E extends object = object,\n>(command: string, args: string[], opts = {} as O, extra = {} as E) {\n return new SpawnPromise<O, E>(command, args, opts, extra)\n}\n"]}