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: introduce abort() method #734

Merged
merged 2 commits into from
Mar 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
"webpod": "^0",
"which": "^3.0.0",
"yaml": "^2.3.4",
"zurk": "^0.0.27"
"zurk": "^0.0.31"
},
"publishConfig": {
"registry": "https://wombat-dressing-room.appspot.com"
Expand Down
9 changes: 9 additions & 0 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export interface Options {
[processCwd]: string
cwd?: string
verbose: boolean
ac?: AbortController
env: NodeJS.ProcessEnv
shell: string | boolean
nothrow: boolean
Expand Down Expand Up @@ -200,6 +201,7 @@ export class ProcessPromise extends Promise<ProcessOutput> {
this.zurk = exec({
cmd: $.prefix + this._command,
cwd: $.cwd ?? $[processCwd],
ac: $.ac,
shell: typeof $.shell === 'string' ? $.shell : true,
env: $.env,
spawn: $.spawn,
Expand Down Expand Up @@ -354,6 +356,13 @@ export class ProcessPromise extends Promise<ProcessOutput> {
}
}

abort(reason?: string) {
if (!this.child)
throw new Error('Trying to abort a process without creating one.')

this.zurk?.ac.abort(reason)
}

async kill(signal = 'SIGTERM'): Promise<void> {
if (!this.child)
throw new Error('Trying to kill a process without creating one.')
Expand Down
25 changes: 25 additions & 0 deletions test/core.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,31 @@ describe('core', () => {
})
})

test('abort() method works', async () => {
const p = $`sleep 9999`
setTimeout(() => p.abort(), 100)

try {
await p
assert.unreachable('should have thrown')
} catch ({ message }) {
assert.match(message, /The operation was aborted/)
}
})

test('accepts optional AbortController', async () => {
const ac = new AbortController()
const p = $({ ac })`sleep 9999`
setTimeout(() => ac.abort(), 100)

try {
await p
assert.unreachable('should have thrown')
} catch ({ message }) {
assert.match(message, /The operation was aborted/)
}
})

test('kill() method works', async () => {
let p = $`sleep 9999`.nothrow()
setTimeout(() => {
Expand Down
Loading