@@ -48,6 +48,7 @@ import {
48
48
once ,
49
49
parseDuration ,
50
50
preferLocalBin ,
51
+ proxyOverride ,
51
52
quote ,
52
53
quotePowerShell ,
53
54
} from './util.js'
@@ -328,12 +329,12 @@ export class ProcessPromise extends Promise<ProcessOutput> {
328
329
329
330
// Essentials
330
331
pipe ( dest : TemplateStringsArray , ...args : any [ ] ) : ProcessPromise
331
- pipe < D extends Writable > ( dest : D ) : D & PromiseLike < D >
332
+ pipe < D extends Writable > ( dest : D ) : D & PromiseLike < ProcessOutput & D >
332
333
pipe < D extends ProcessPromise > ( dest : D ) : D
333
334
pipe (
334
335
dest : Writable | ProcessPromise | TemplateStringsArray ,
335
336
...args : any [ ]
336
- ) : ( Writable & PromiseLike < Writable > ) | ProcessPromise {
337
+ ) : ( Writable & PromiseLike < ProcessPromise & Writable > ) | ProcessPromise {
337
338
if ( isStringLiteral ( dest , ...args ) )
338
339
return this . pipe ( $ ( { halt : true } ) ( dest as TemplateStringsArray , ...args ) )
339
340
if ( isString ( dest ) )
@@ -372,7 +373,8 @@ export class ProcessPromise extends Promise<ProcessOutput> {
372
373
return dest
373
374
}
374
375
from . once ( 'end' , ( ) => dest . emit ( 'end-piped-from' ) ) . pipe ( dest )
375
- return promisifyStream ( dest , this )
376
+ return promisifyStream ( dest , this ) as Writable &
377
+ PromiseLike < ProcessPromise & Writable >
376
378
}
377
379
378
380
abort ( reason ?: string ) {
@@ -867,32 +869,31 @@ export function log(entry: LogEntry) {
867
869
}
868
870
}
869
871
870
- export const promisifyStream = < S extends Writable > (
872
+ const promisifyStream = < S extends Writable > (
871
873
stream : S ,
872
- from ?: ProcessPromise
873
- ) : S & PromiseLike < S > =>
874
- new Proxy ( stream as S & PromiseLike < S > , {
875
- get ( target , key ) {
876
- if ( key === 'run' ) return from ?. run . bind ( from )
877
- if ( key === 'then' ) {
878
- return ( res : any = noop , rej : any = noop ) =>
879
- new Promise ( ( _res , _rej ) =>
880
- target
881
- . once ( 'error' , ( e ) => _rej ( rej ( e ) ) )
882
- . once ( 'finish' , ( ) => _res ( res ( target ) ) )
883
- . once ( 'end-piped-from' , ( ) => _res ( res ( target ) ) )
874
+ from : ProcessPromise
875
+ ) : S & PromiseLike < ProcessOutput & S > =>
876
+ proxyOverride ( stream as S & PromiseLike < ProcessOutput & S > , {
877
+ then ( res : any = noop , rej : any = noop ) {
878
+ return new Promise ( ( _res , _rej ) =>
879
+ stream
880
+ . once ( 'error' , ( e ) => _rej ( rej ( e ) ) )
881
+ . once ( 'finish' , ( ) =>
882
+ _res ( res ( proxyOverride ( stream , ( from as any ) . _output ) ) )
884
883
)
885
- }
886
- const value = Reflect . get ( target , key )
887
- if ( key === 'pipe' && typeof value === 'function' ) {
888
- return function ( ...args : any ) {
889
- const piped = value . apply ( target , args )
890
- piped . _pipedFrom = from
891
- return piped instanceof ProcessPromise
892
- ? piped
893
- : promisifyStream ( piped , from )
894
- }
895
- }
896
- return value
884
+ . once ( 'end-piped-from' , ( ) =>
885
+ _res ( res ( proxyOverride ( stream , ( from as any ) . _output ) ) )
886
+ )
887
+ )
888
+ } ,
889
+ run ( ) {
890
+ return from . run ( )
891
+ } ,
892
+ _pipedFrom : from ,
893
+ pipe ( ...args : any ) {
894
+ const piped = stream . pipe . apply ( stream , args )
895
+ return piped instanceof ProcessPromise
896
+ ? piped
897
+ : promisifyStream ( piped as Writable , from )
897
898
} ,
898
899
} )
0 commit comments