Skip to content

Commit

Permalink
feat: let nothrow be a filter
Browse files Browse the repository at this point in the history
closes google#714
  • Loading branch information
antongolub committed Mar 31, 2024
1 parent 24dcf3a commit 8069d17
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
28 changes: 23 additions & 5 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export interface Options {
sync: boolean
env: NodeJS.ProcessEnv
shell: string | boolean
nothrow: boolean
nothrow: boolean | number[] | ((status: number | null) => boolean)
prefix: string
postfix: string
quote: typeof quote
Expand Down Expand Up @@ -123,6 +123,15 @@ function checkShell() {
}
}

function checkStatus(status: number | null, nothrow: Options['nothrow']) {
return (
status === 0 ||
(typeof nothrow === 'function'
? nothrow(status)
: (nothrow as number[])?.includes?.(status as number) ?? nothrow)
)
}

function getStore() {
return storage.getStore() || defaults
}
Expand Down Expand Up @@ -199,7 +208,7 @@ export class ProcessPromise extends Promise<ProcessOutput> {
private _reject: Resolve = noop
private _snapshot = getStore()
private _stdio: [IO, IO, IO] = ['inherit', 'pipe', 'pipe']
private _nothrow?: boolean
private _nothrow?: Options['nothrow']
private _quiet?: boolean
private _timeout?: number
private _timeoutSignal = 'SIGTERM'
Expand Down Expand Up @@ -306,7 +315,7 @@ export class ProcessPromise extends Promise<ProcessOutput> {
message
)
self._output = output
if (status === 0 || (self._nothrow ?? $.nothrow)) {
if (checkStatus(status, self._nothrow ?? $.nothrow)) {
self._resolve(output)
} else {
self._reject(output)
Expand Down Expand Up @@ -429,8 +438,17 @@ export class ProcessPromise extends Promise<ProcessOutput> {
return this
}

nothrow(): ProcessPromise {
this._nothrow = true
nothrow(
...codes: number[] | [(status: number | null) => boolean]
): ProcessPromise {
if (codes.length === 0) {
this._nothrow = true
} else if (typeof codes[0] === 'function') {
this._nothrow = codes[0]
} else {
this._nothrow = codes as number[]
}

return this
}

Expand Down
24 changes: 23 additions & 1 deletion test/core.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ describe('core', () => {
assert.equal(await $`[[ -f README.md ]]`.exitCode, 0)
})

test('nothrow() do not throw', async () => {
test('nothrow() does not throw', async () => {
let { exitCode } = await $`exit 42`.nothrow()
assert.equal(exitCode, 42)
{
Expand All @@ -526,6 +526,28 @@ describe('core', () => {
}
})

test('nothrow() accepts a filter', async () => {
assert.equal((await $`exit 42`.nothrow(42)).exitCode, 42)
assert.equal((await $({ nothrow: [42] })`exit 42`).exitCode, 42)
assert.equal(
(
await $({
nothrow(code) {
return code === 42
},
})`exit 42`
).exitCode,
42
)

try {
await $`exit 42`.nothrow(1)
assert.unreachable('should throw')
} catch (p) {
assert.equal(p.exitCode, 42)
}
})

test('malformed cmd error', async () => {
assert.throws(() => $`\033`, /malformed/i)
})
Expand Down

0 comments on commit 8069d17

Please sign in to comment.