11import spawn from "cross-spawn" ;
22import { memoize } from "lodash" ;
3- import { SpawnOptions } from "child_process" ;
3+ import { SpawnOptions , ChildProcess } from "child_process" ;
44
55type AnyKey < T > = T & {
66 [ key : string ] : T
@@ -25,8 +25,9 @@ type ExecResult = {
2525 stderr : string ,
2626}
2727
28+ type AttachedPromise < T > = Promise < T > & { _child : ChildProcess }
2829type ExecFunction = ( ...args : ExecParameters ) => ExecResult ;
29- type ExecAsyncFunction = ( ...args : ExecParameters ) => Promise < ExecResult > ;
30+ type ExecAsyncFunction = ( ...args : ExecParameters ) => AttachedPromise < ExecResult > ;
3031
3132const memoizeSpawnSync = memoize ( spawn . sync , ( ...args ) => JSON . stringify ( args ) ) ;
3233const isObject = ( maybeObj : unknown ) => Object . prototype . toString . call ( maybeObj ) == "[object Object]" ;
@@ -46,19 +47,17 @@ const normalizeArgs = (maybeArgs: MaybeArgs, options?: ExecOpts) => {
4647 return [ finalArgs , finalOpts ] as [ string [ ] , ExecOpts ] ;
4748}
4849
49- async function execAsync (
50+ function execAsync (
5051 cmd : string ,
5152 maybeArgs : MaybeArgs ,
5253 opts ?: ExecOpts
53- ) : Promise < ExecResult > {
54+ ) : AttachedPromise < ExecResult > {
5455 const [ endArgs , execOpts ] = normalizeArgs ( maybeArgs , opts ) ;
5556
5657 let stdout = "" ;
5758 let stderr = "" ;
58-
59- return new Promise ( function ( resolve , reject ) {
60- const childProcess = spawn ( cmd , endArgs , execOpts ) ;
61-
59+ const childProcess = spawn ( cmd , endArgs , execOpts ) ;
60+ const promise = new Promise ( function ( resolve , reject ) {
6261 childProcess . stdout ?. on ( "data" , data => ( stdout += data ) ) ;
6362 childProcess . stderr ?. on ( "data" , data => ( stderr += data ) ) ;
6463
@@ -89,7 +88,10 @@ async function execAsync(
8988 stderr : `${ stderr } ` . trim ( )
9089 } ) ;
9190 } ) ;
92- } ) ;
91+ } ) as AttachedPromise < ExecResult > ;
92+
93+ promise . _child = childProcess ;
94+ return promise ;
9395}
9496
9597function exec (
0 commit comments