1- import type { ExecException } from 'child_process' ;
1+ import type { ExecFileException } from 'child_process' ;
22import { exec , execFile } from 'child_process' ;
33import type { Stats } from 'fs' ;
44import { access , constants , existsSync , statSync } from 'fs' ;
@@ -160,7 +160,7 @@ const bufferExceededRegex = /stdout maxBuffer( length)? exceeded/;
160160
161161export class RunError extends Error {
162162 constructor (
163- private readonly original : ExecException ,
163+ private readonly original : ExecFileException ,
164164 public readonly stdout : string ,
165165 public readonly stderr : string ,
166166 ) {
@@ -179,8 +179,8 @@ export class RunError extends Error {
179179 return this . original . killed ;
180180 }
181181
182- get code ( ) : number | undefined {
183- return this . original . code ;
182+ get code ( ) : string | number | undefined {
183+ return this . original . code ?? undefined ;
184184 }
185185
186186 get signal ( ) : NodeJS . Signals | undefined {
@@ -227,14 +227,17 @@ export function run<T extends number | string | Buffer>(
227227 encoding : BufferEncoding | 'buffer' | string ,
228228 options ?: RunOptions & { exitCodeOnly ?: boolean } ,
229229) : Promise < T > {
230- const { stdin, stdinEncoding, ...opts } : RunOptions = { maxBuffer : 1000 * 1024 * 1024 , ...options } ;
230+ const { cancellation, exitCodeOnly, stdin, stdinEncoding, ...opts } : RunOptions & { exitCodeOnly ?: boolean } = {
231+ maxBuffer : 1000 * 1024 * 1024 ,
232+ ...options ,
233+ } ;
231234
232235 let killed = false ;
233236 return new Promise < T > ( ( resolve , reject ) => {
234- const proc = execFile ( command , args , opts , async ( error : ExecException | null , stdout , stderr ) => {
237+ const proc = execFile ( command , args , opts , async ( error : ExecFileException | null , stdout , stderr ) => {
235238 if ( killed ) return ;
236239
237- if ( options ?. exitCodeOnly ) {
240+ if ( exitCodeOnly ) {
238241 resolve ( ( error ?. code ?? proc . exitCode ) as T ) ;
239242
240243 return ;
@@ -273,11 +276,11 @@ export function run<T extends number | string | Buffer>(
273276 }
274277 } ) ;
275278
276- options ?. cancellation ?. onCancellationRequested ( ( ) => {
279+ cancellation ?. onCancellationRequested ( ( ) => {
277280 const success = proc . kill ( ) ;
278281 killed = true ;
279282
280- if ( options ?. exitCodeOnly ) {
283+ if ( exitCodeOnly ) {
281284 resolve ( 0 as T ) ;
282285 } else {
283286 reject ( new CancelledRunError ( command , success ) ) ;
0 commit comments