Commit 7ffb44e

Anton Golub <antongolub@antongolub.com>
2024-10-30 13:51:57
refactor: do not hack stream behavior (#923)
1 parent 5f0874e
Changed files (2)
src/core.ts
@@ -531,10 +531,10 @@ export class ProcessPromise extends Promise<ProcessOutput> {
     return super.catch(onrejected)
   }
 
-  private static promisifyStream<D extends Writable>(
-    dest: D
-  ): D & PromiseLike<void> {
-    return new Proxy(dest as D & PromiseLike<void>, {
+  private static promisifyStream<S extends Writable>(
+    stream: S
+  ): S & PromiseLike<void> {
+    return new Proxy(stream as S & PromiseLike<void>, {
       get(target, key) {
         if (key === 'then') {
           return (res: any = noop, rej: any = noop) =>
@@ -548,11 +548,9 @@ export class ProcessPromise extends Promise<ProcessOutput> {
           const pipe = Reflect.get(target, key)
           if (typeof pipe === 'function')
             return function (...args: any) {
-              return (
-                args[0] instanceof ProcessPromise
-                  ? noop
-                  : ProcessPromise.promisifyStream
-              )(pipe.apply(target, args) as Writable)
+              return ProcessPromise.promisifyStream(
+                pipe.apply(target, args) as S
+              )
             }
         }
         return Reflect.get(target, key)
test/core.test.js
@@ -397,33 +397,35 @@ describe('core', () => {
         )
       })
 
-      test('is chainable ($)', async () => {
-        let { stdout: o1 } = await $`echo "hello"`
-          .pipe($`awk '{print $1" world"}'`)
-          .pipe($`tr '[a-z]' '[A-Z]'`)
-        assert.equal(o1, 'HELLO WORLD\n')
-
-        let { stdout: o2 } = await $`echo "hello"`
-          .pipe`awk '{print $1" world"}'`.pipe`tr '[a-z]' '[A-Z]'`
-        assert.equal(o2, 'HELLO WORLD\n')
-      })
+      describe('is chainable', () => {
+        test('$ to $', async () => {
+          let { stdout: o1 } = await $`echo "hello"`
+            .pipe($`awk '{print $1" world"}'`)
+            .pipe($`tr '[a-z]' '[A-Z]'`)
+          assert.equal(o1, 'HELLO WORLD\n')
+
+          let { stdout: o2 } = await $`echo "hello"`
+            .pipe`awk '{print $1" world"}'`.pipe`tr '[a-z]' '[A-Z]'`
+          assert.equal(o2, 'HELLO WORLD\n')
+        })
 
-      test('is chainable (Transform/Duplex)', async () => {
-        const file = tempfile()
-        const p = $`echo "hello"`
-          .pipe(
-            new Transform({
-              transform(chunk, encoding, callback) {
-                callback(null, String(chunk).toUpperCase())
-              },
-            })
-          )
-          .pipe(fs.createWriteStream(file))
+        test('Transform/Duplex', async () => {
+          const file = tempfile()
+          const p = $`echo "hello"`
+            .pipe(
+              new Transform({
+                transform(chunk, encoding, callback) {
+                  callback(null, String(chunk).toUpperCase())
+                },
+              })
+            )
+            .pipe(fs.createWriteStream(file))
 
-        assert.ok(p instanceof WriteStream)
-        assert.equal(await p, undefined)
-        assert.equal((await fs.readFile(file)).toString(), 'HELLO\n')
-        await fs.rm(file)
+          assert.ok(p instanceof WriteStream)
+          assert.equal(await p, undefined)
+          assert.equal((await fs.readFile(file)).toString(), 'HELLO\n')
+          await fs.rm(file)
+        })
       })
 
       it('supports multipiping', async () => {