Skip to content

Commit

Permalink
feat: introduce abort() method (#734)
Browse files Browse the repository at this point in the history
* feat: introduce `abort()` method

closes #527

* chore: apply fmt
  • Loading branch information
antongolub authored Mar 17, 2024
1 parent aeec7ae commit fa4a7b4
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 8 deletions.
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

0 comments on commit fa4a7b4

Please sign in to comment.