Skip to content

Commit

Permalink
fix: propagate rejection on pipe (#899)
Browse files Browse the repository at this point in the history
* chore: apply npm pkg fix

* fix: propagate rejection on pipe()

closes #640
  • Loading branch information
antongolub authored Sep 17, 2024
1 parent 778e4b3 commit be2debc
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"./package.json": "./package.json"
},
"bin": {
"zx": "./build/cli.js"
"zx": "build/cli.js"
},
"man": "./man/zx.1",
"files": [
Expand Down Expand Up @@ -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"
Expand Down
5 changes: 5 additions & 0 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {
Expand Down
21 changes: 21 additions & 0 deletions test/core.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down

0 comments on commit be2debc

Please sign in to comment.