File tree 3 files changed +25
-17
lines changed
3 files changed +25
-17
lines changed Original file line number Diff line number Diff line change @@ -70,6 +70,13 @@ await $`echo "Hello, stdout!"`
70
70
await $` cat /tmp/output.txt`
71
71
```
72
72
73
+ You can pass a string to ` pipe() ` to implicitly create a receiving file. The previous example is equivalent to:
74
+
75
+ ``` js
76
+ await $` echo "Hello, stdout!"`
77
+ .pipe (' /tmp/output.txt' )
78
+ ```
79
+
73
80
Pipes can be used to show a real-time output of the process:
74
81
75
82
``` js
Original file line number Diff line number Diff line change @@ -335,7 +335,7 @@ export class ProcessPromise extends Promise<ProcessOutput> {
335
335
pipe < D extends Writable > ( dest : D ) : D & PromiseLike < ProcessOutput & D >
336
336
pipe < D extends ProcessPromise > ( dest : D ) : D
337
337
pipe (
338
- dest : Writable | ProcessPromise | TemplateStringsArray ,
338
+ dest : Writable | ProcessPromise | TemplateStringsArray | string ,
339
339
...args : any [ ]
340
340
) : ( Writable & PromiseLike < ProcessPromise & Writable > ) | ProcessPromise {
341
341
if ( isStringLiteral ( dest , ...args ) )
@@ -347,9 +347,6 @@ export class ProcessPromise extends Promise<ProcessOutput> {
347
347
} ) ( dest as TemplateStringsArray , ...args )
348
348
)
349
349
350
- if ( isString ( dest ) )
351
- throw new Error ( 'The pipe() method does not take strings. Forgot $?' )
352
-
353
350
this . _piped = true
354
351
const ee = this . _ee
355
352
const from = new VoidStream ( )
@@ -371,6 +368,8 @@ export class ProcessPromise extends Promise<ProcessOutput> {
371
368
} )
372
369
}
373
370
371
+ if ( isString ( dest ) ) dest = fs . createWriteStream ( dest )
372
+
374
373
if ( dest instanceof ProcessPromise ) {
375
374
dest . _pipedFrom = this
376
375
@@ -382,6 +381,7 @@ export class ProcessPromise extends Promise<ProcessOutput> {
382
381
}
383
382
return dest
384
383
}
384
+
385
385
from . once ( 'end' , ( ) => dest . emit ( 'end-piped-from' ) ) . pipe ( dest )
386
386
return promisifyStream ( dest , this ) as Writable &
387
387
PromiseLike < ProcessPromise & Writable >
Original file line number Diff line number Diff line change @@ -421,6 +421,20 @@ describe('core', () => {
421
421
}
422
422
} )
423
423
424
+ test ( 'accepts file' , async ( ) => {
425
+ const file = tempfile ( )
426
+ try {
427
+ await $ `echo foo` . pipe ( file )
428
+ assert . equal ( ( await fs . readFile ( file ) ) . toString ( ) , 'foo\n' )
429
+
430
+ const r = $ `cat`
431
+ fs . createReadStream ( file ) . pipe ( r . stdin )
432
+ assert . equal ( ( await r ) . stdout , 'foo\n' )
433
+ } finally {
434
+ await fs . rm ( file )
435
+ }
436
+ } )
437
+
424
438
test ( 'accepts ProcessPromise' , async ( ) => {
425
439
const p = await $ `echo foo` . pipe ( $ `cat` )
426
440
assert . equal ( p . stdout . trim ( ) , 'foo' )
@@ -437,19 +451,6 @@ describe('core', () => {
437
451
assert . equal ( ( await p1 ) . stdout . trim ( ) , 'pipe-to-stdout' )
438
452
} )
439
453
440
- test ( 'checks argument type' , async ( ) => {
441
- let err
442
- try {
443
- $ `echo 'test'` . pipe ( 'str' )
444
- } catch ( p ) {
445
- err = p
446
- }
447
- assert . equal (
448
- err . message ,
449
- 'The pipe() method does not take strings. Forgot $?'
450
- )
451
- } )
452
-
453
454
describe ( 'supports chaining' , ( ) => {
454
455
const getUpperCaseTransform = ( ) =>
455
456
new Transform ( {
You can’t perform that action at this time.
0 commit comments