From 6eb540f2a044074aee4e32f8c3035728a1db2532 Mon Sep 17 00:00:00 2001 From: Anton Golub Date: Thu, 31 Oct 2024 16:22:44 +0300 Subject: [PATCH] refactor: pass stream err to pipe promise rejection (#928) continues #921 --- src/core.ts | 4 ++-- src/util.ts | 10 +++++----- test-d/core.test-d.ts | 4 +++- test/core.test.js | 5 +++-- 4 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/core.ts b/src/core.ts index da4a4e6926..53d7462ac1 100644 --- a/src/core.ts +++ b/src/core.ts @@ -319,12 +319,12 @@ export class ProcessPromise extends Promise { // Essentials pipe(dest: TemplateStringsArray, ...args: any[]): ProcessPromise - pipe(dest: D): D & PromiseLike + pipe(dest: D): D & PromiseLike pipe(dest: D): D pipe( dest: Writable | ProcessPromise | TemplateStringsArray, ...args: any[] - ): (Writable & PromiseLike) | ProcessPromise { + ): (Writable & PromiseLike) | ProcessPromise { if (isStringLiteral(dest, ...args)) return this.pipe($(dest as TemplateStringsArray, ...args)) if (isString(dest)) diff --git a/src/util.ts b/src/util.ts index 9ab79a7879..22ab4d63e8 100644 --- a/src/util.ts +++ b/src/util.ts @@ -453,21 +453,21 @@ export const once = any>(fn: T) => { export const promisifyStream = ( stream: S -): S & PromiseLike => - new Proxy(stream as S & PromiseLike, { +): S & PromiseLike => + new Proxy(stream as S & PromiseLike, { get(target, key) { if (key === 'then') { return (res: any = noop, rej: any = noop) => new Promise((_res, _rej) => target - .once('error', () => _rej(rej())) - .once('finish', () => _res(res())) + .once('error', (e) => _rej(rej(e))) + .once('finish', () => _res(res(target))) ) } const value = Reflect.get(target, key) if (key === 'pipe' && typeof value === 'function') { return function (...args: any) { - return promisifyStream(value.apply(target, args) as S) + return promisifyStream(value.apply(target, args)) } } return value diff --git a/test-d/core.test-d.ts b/test-d/core.test-d.ts index 16cbe1207c..02bf3167aa 100644 --- a/test-d/core.test-d.ts +++ b/test-d/core.test-d.ts @@ -27,7 +27,9 @@ expectType(p.nothrow()) expectType(p.quiet()) expectType(p.pipe($`cmd`)) expectType(p.pipe`cmd`) -expectType>(p.pipe(process.stdout)) +expectType>( + p.pipe(process.stdout) +) expectType(p.stdio('pipe')) expectType(p.timeout('1s')) expectType>(p.kill()) diff --git a/test/core.test.js b/test/core.test.js index 715a90e5cf..302aa76bf7 100644 --- a/test/core.test.js +++ b/test/core.test.js @@ -411,6 +411,7 @@ describe('core', () => { test('$ > stream', async () => { const file = tempfile() + const fileStream = fs.createWriteStream(file) const p = $`echo "hello"` .pipe( new Transform({ @@ -419,10 +420,10 @@ describe('core', () => { }, }) ) - .pipe(fs.createWriteStream(file)) + .pipe(fileStream) assert.ok(p instanceof WriteStream) - assert.equal(await p, undefined) + assert.equal(await p, fileStream) assert.equal((await fs.readFile(file)).toString(), 'HELLO\n') await fs.rm(file) })