@@ -48,7 +48,6 @@ import {
4848 once ,
4949 parseDuration ,
5050 preferLocalBin ,
51- promisifyStream ,
5251 quote ,
5352 quotePowerShell ,
5453} from './util.js'
@@ -373,7 +372,7 @@ export class ProcessPromise extends Promise<ProcessOutput> {
373372 return dest
374373 }
375374 from . once ( 'end' , ( ) => dest . emit ( 'end-piped-from' ) ) . pipe ( dest )
376- return promisifyStream ( dest )
375+ return promisifyStream ( dest , this )
377376 }
378377
379378 abort ( reason ?: string ) {
@@ -544,6 +543,32 @@ export class ProcessPromise extends Promise<ProcessOutput> {
544543 ) : Promise < ProcessOutput | T > {
545544 return super . catch ( onrejected )
546545 }
546+
547+ // Stream-like API
548+ private writable = true
549+ private emit ( event : string , ...args : any [ ] ) {
550+ return this
551+ }
552+ private on ( event : string , cb : any ) {
553+ this . _stdin . on ( event , cb )
554+ return this
555+ }
556+ private once ( event : string , cb : any ) {
557+ this . _stdin . once ( event , cb )
558+ return this
559+ }
560+ private write ( data : any , encoding : BufferEncoding , cb : any ) {
561+ this . _stdin . write ( data , encoding , cb )
562+ return this
563+ }
564+ private end ( chunk : any , cb : any ) {
565+ this . _stdin . end ( chunk , cb )
566+ return this
567+ }
568+ private removeListener ( event : string , cb : any ) {
569+ this . _stdin . removeListener ( event , cb )
570+ return this
571+ }
547572}
548573
549574type GettersRecord < T extends Record < any , any > > = { [ K in keyof T ] : ( ) => T [ K ] }
@@ -841,3 +866,33 @@ export function log(entry: LogEntry) {
841866 process . stderr . write ( entry . error + '\n' )
842867 }
843868}
869+
870+ export const promisifyStream = < S extends Writable > (
871+ 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 ) ) )
884+ )
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
897+ } ,
898+ } )
0 commit comments