Skip to content

Commit 6eb540f

Browse files
authored
refactor: pass stream err to pipe promise rejection (#928)
continues #921
1 parent d0b5c2c commit 6eb540f

File tree

4 files changed

+13
-10
lines changed

4 files changed

+13
-10
lines changed

src/core.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -319,12 +319,12 @@ export class ProcessPromise extends Promise<ProcessOutput> {
319319

320320
// Essentials
321321
pipe(dest: TemplateStringsArray, ...args: any[]): ProcessPromise
322-
pipe<D extends Writable>(dest: D): D & PromiseLike<void>
322+
pipe<D extends Writable>(dest: D): D & PromiseLike<D>
323323
pipe<D extends ProcessPromise>(dest: D): D
324324
pipe(
325325
dest: Writable | ProcessPromise | TemplateStringsArray,
326326
...args: any[]
327-
): (Writable & PromiseLike<void>) | ProcessPromise {
327+
): (Writable & PromiseLike<Writable>) | ProcessPromise {
328328
if (isStringLiteral(dest, ...args))
329329
return this.pipe($(dest as TemplateStringsArray, ...args))
330330
if (isString(dest))

src/util.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -453,21 +453,21 @@ export const once = <T extends (...args: any[]) => any>(fn: T) => {
453453

454454
export const promisifyStream = <S extends Writable>(
455455
stream: S
456-
): S & PromiseLike<void> =>
457-
new Proxy(stream as S & PromiseLike<void>, {
456+
): S & PromiseLike<S> =>
457+
new Proxy(stream as S & PromiseLike<S>, {
458458
get(target, key) {
459459
if (key === 'then') {
460460
return (res: any = noop, rej: any = noop) =>
461461
new Promise((_res, _rej) =>
462462
target
463-
.once('error', () => _rej(rej()))
464-
.once('finish', () => _res(res()))
463+
.once('error', (e) => _rej(rej(e)))
464+
.once('finish', () => _res(res(target)))
465465
)
466466
}
467467
const value = Reflect.get(target, key)
468468
if (key === 'pipe' && typeof value === 'function') {
469469
return function (...args: any) {
470-
return promisifyStream(value.apply(target, args) as S)
470+
return promisifyStream(value.apply(target, args))
471471
}
472472
}
473473
return value

test-d/core.test-d.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ expectType<ProcessPromise>(p.nothrow())
2727
expectType<ProcessPromise>(p.quiet())
2828
expectType<ProcessPromise>(p.pipe($`cmd`))
2929
expectType<ProcessPromise>(p.pipe`cmd`)
30-
expectType<typeof process.stdout & PromiseLike<void>>(p.pipe(process.stdout))
30+
expectType<typeof process.stdout & PromiseLike<typeof process.stdout>>(
31+
p.pipe(process.stdout)
32+
)
3133
expectType<ProcessPromise>(p.stdio('pipe'))
3234
expectType<ProcessPromise>(p.timeout('1s'))
3335
expectType<Promise<void>>(p.kill())

test/core.test.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,7 @@ describe('core', () => {
411411

412412
test('$ > stream', async () => {
413413
const file = tempfile()
414+
const fileStream = fs.createWriteStream(file)
414415
const p = $`echo "hello"`
415416
.pipe(
416417
new Transform({
@@ -419,10 +420,10 @@ describe('core', () => {
419420
},
420421
})
421422
)
422-
.pipe(fs.createWriteStream(file))
423+
.pipe(fileStream)
423424

424425
assert.ok(p instanceof WriteStream)
425-
assert.equal(await p, undefined)
426+
assert.equal(await p, fileStream)
426427
assert.equal((await fs.readFile(file)).toString(), 'HELLO\n')
427428
await fs.rm(file)
428429
})

0 commit comments

Comments
 (0)