@@ -200,6 +200,13 @@ export const $: Shell & Options = new Proxy<Shell & Options>(
200
200
201
201
type Resolve = ( out : ProcessOutput ) = > void
202
202
203
+ type PipeDest = Writable | ProcessPromise | TemplateStringsArray | string
204
+ type PipeMethod = {
205
+ ( dest : TemplateStringsArray , ...args : any [ ] ) : ProcessPromise
206
+ < D extends Writable > ( dest : D ) : D & PromiseLike < ProcessOutput & D >
207
+ < D extends ProcessPromise > ( dest : D ) : D
208
+ }
209
+
203
210
export class ProcessPromise extends Promise < ProcessOutput > {
204
211
private _command = ''
205
212
private _from = ''
@@ -336,15 +343,26 @@ export class ProcessPromise extends Promise<ProcessOutput> {
336
343
}
337
344
338
345
// Essentials
339
- pipe ( dest : TemplateStringsArray , ...args : any [ ] ) : ProcessPromise
340
- pipe < D extends Writable > ( dest : D ) : D & PromiseLike < ProcessOutput & D >
341
- pipe < D extends ProcessPromise > ( dest : D ) : D
342
- pipe (
343
- dest : Writable | ProcessPromise | TemplateStringsArray | string ,
346
+ pipe ! : PipeMethod & {
347
+ stdout : PipeMethod
348
+ stderr : PipeMethod
349
+ }
350
+ // prettier-ignore
351
+ static {
352
+ Object . defineProperty ( this . prototype , 'pipe' , { get ( ) {
353
+ const self = this
354
+ const pipeStdout : PipeMethod = function ( dest : PipeDest , ...args : any [ ] ) { return self . _pipe . call ( self , 'stdout' , dest , ...args ) }
355
+ const pipeStderr : PipeMethod = function ( dest : PipeDest , ...args : any [ ] ) { return self . _pipe . call ( self , 'stderr' , dest , ...args ) }
356
+ return Object . assign ( pipeStdout , { stderr : pipeStderr , stdout : pipeStdout } )
357
+ } } )
358
+ }
359
+ private _pipe (
360
+ source : 'stdout' | 'stderr' ,
361
+ dest : PipeDest ,
344
362
...args : any [ ]
345
363
) : ( Writable & PromiseLike < ProcessPromise & Writable > ) | ProcessPromise {
346
364
if ( isStringLiteral ( dest , ...args ) )
347
- return this . pipe (
365
+ return this . pipe [ source ] (
348
366
$ ( {
349
367
halt : true ,
350
368
ac : this . _snapshot . ac ,
@@ -356,19 +374,18 @@ export class ProcessPromise extends Promise<ProcessOutput> {
356
374
const ee = this . _ee
357
375
const from = new VoidStream ( )
358
376
const fill = ( ) => {
359
- for ( const chunk of this . _zurk ! . store . stdout ) from . write ( chunk )
377
+ for ( const chunk of this . _zurk ! . store [ source ] ) from . write ( chunk )
378
+ return true
360
379
}
380
+ const fillEnd = ( ) => this . _resolved && fill ( ) && from . end ( )
361
381
362
- if ( this . _resolved ) {
363
- fill ( )
364
- from . end ( )
365
- } else {
366
- const onStdout = ( chunk : string | Buffer ) => from . write ( chunk )
367
- ee . once ( 'stdout' , ( ) => {
382
+ if ( ! this . _resolved ) {
383
+ const onData = ( chunk : string | Buffer ) => from . write ( chunk )
384
+ ee . once ( source , ( ) => {
368
385
fill ( )
369
- ee . on ( 'stdout' , onStdout )
386
+ ee . on ( source , onData )
370
387
} ) . once ( 'end' , ( ) => {
371
- ee . removeListener ( 'stdout' , onStdout )
388
+ ee . removeListener ( source , onData )
372
389
from . end ( )
373
390
} )
374
391
}
@@ -384,10 +401,12 @@ export class ProcessPromise extends Promise<ProcessOutput> {
384
401
this . catch ( ( e ) => ( dest . isNothrow ( ) ? noop : dest . _reject ( e ) ) )
385
402
from . pipe ( dest . run ( ) . _stdin )
386
403
}
404
+ fillEnd ( )
387
405
return dest
388
406
}
389
407
390
408
from . once ( 'end' , ( ) => dest . emit ( 'end-piped-from' ) ) . pipe ( dest )
409
+ fillEnd ( )
391
410
return promisifyStream ( dest , this ) as Writable &
392
411
PromiseLike < ProcessPromise & Writable >
393
412
}
0 commit comments