Commit be2debc

Anton Golub <antongolub@antongolub.com>
2024-09-17 09:23:51
fix: propagate rejection on pipe (#899)
* chore: apply npm pkg fix * fix: propagate rejection on pipe() closes #640
1 parent 778e4b3
Changed files (3)
src/core.ts
@@ -472,6 +472,11 @@ export class ProcessPromise extends Promise<ProcessOutput> {
     }
     this._piped = true
     if (dest instanceof ProcessPromise) {
+      const _reject = this._reject
+      this._reject = function (v) {
+        _reject(v)
+        dest._reject(v)
+      }
       dest.stdio('pipe')
       dest._prerun = this.run.bind(this)
       dest._postrun = () => {
test/core.test.js
@@ -324,6 +324,27 @@ describe('core', () => {
         assert.equal(stdout, 'HELLO WORLD\n')
       })
 
+      test('propagates rejection', async () => {
+        const p1 = $`exit 1`
+        const p2 = p1.pipe($`echo hello`)
+
+        try {
+          await p1
+        } catch (e) {
+          assert.equal(e.exitCode, 1)
+        }
+
+        try {
+          await p2
+        } catch (e) {
+          assert.equal(e.exitCode, 1)
+        }
+
+        const p3 = await $({ nothrow: true })`echo hello && exit 1`.pipe($`cat`)
+        assert.equal(p3.exitCode, 0)
+        assert.equal(p3.stdout.trim(), 'hello')
+      })
+
       test('accepts Writable', async () => {
         let contents = ''
         let stream = new Writable({
package.json
@@ -49,7 +49,7 @@
     "./package.json": "./package.json"
   },
   "bin": {
-    "zx": "./build/cli.js"
+    "zx": "build/cli.js"
   },
   "man": "./man/zx.1",
   "files": [
@@ -150,7 +150,10 @@
     "endOfLine": "lf",
     "trailingComma": "es5"
   },
-  "repository": "google/zx",
+  "repository": {
+    "type": "git",
+    "url": "git+https://github.com/google/zx.git"
+  },
   "homepage": "https://google.github.io/zx/",
   "author": "Anton Medvedev <anton@medv.io>",
   "license": "Apache-2.0"