Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: let nothrow be a filter #758

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 5 additions & 5 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- run: npm ci
- run: npm run build:check

coverage:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- run: npm ci
- run: npm run coverage
timeout-minutes: 1
Expand All @@ -23,21 +23,21 @@ jobs:
code-style:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- run: npm ci
- run: npm run fmt:check

types:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- run: npm ci
- run: npm run build
- run: npm run test:types

circular:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- run: npm ci
- run: npm run circular
4 changes: 2 additions & 2 deletions .github/workflows/dev-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ jobs:
publish:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 18
- run: npm ci
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
ref: gh-pages
- name: Setup Pages
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ jobs:
publish:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 16
- run: npm ci
Expand Down
12 changes: 6 additions & 6 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ jobs:
node-version: [16.x, 18.x, 20.x]

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- run: npm ci
Expand All @@ -26,9 +26,9 @@ jobs:
runs-on: windows-latest

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Use Node.js 16.x
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: 16.x
- run: npm ci
Expand All @@ -42,9 +42,9 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Use Node.js 20.x
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: 20.x
- name: Setup Bun
Expand Down
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