From fa4a7b404b34986b51ad9a941c1a17ac473d0d7d Mon Sep 17 00:00:00 2001 From: Anton Golub Date: Sun, 17 Mar 2024 16:42:57 +0300 Subject: [PATCH] feat: introduce `abort()` method (#734) * feat: introduce `abort()` method closes #527 * chore: apply fmt --- package-lock.json | 14 +++++++------- package.json | 2 +- src/core.ts | 9 +++++++++ test/core.test.js | 25 +++++++++++++++++++++++++ 4 files changed, 42 insertions(+), 8 deletions(-) diff --git a/package-lock.json b/package-lock.json index c5b53aa653..3ecfde83fe 100644 --- a/package-lock.json +++ b/package-lock.json @@ -37,7 +37,7 @@ "webpod": "^0", "which": "^3.0.0", "yaml": "^2.3.4", - "zurk": "^0.0.27" + "zurk": "^0.0.31" }, "engines": { "node": ">= 16.0.0" @@ -6838,9 +6838,9 @@ } }, "node_modules/zurk": { - "version": "0.0.27", - "resolved": "https://registry.npmjs.org/zurk/-/zurk-0.0.27.tgz", - "integrity": "sha512-BwvvxnPmiiI0H8ThWj7XSUrMN33X/JTb+qiA7/2M03jwwFtCGPwDxpNzKbI9aNKRkJu1v6XD73Y5aYY+eNyGSg==", + "version": "0.0.31", + "resolved": "https://registry.npmjs.org/zurk/-/zurk-0.0.31.tgz", + "integrity": "sha512-rYp8YgJFaNgTEe9/s607CqIxhJtls8Uik6hQGiPOus+5iyKphc5Xk5stLzE1bO+JnY73T/6MXI8DbnpkJQXYpA==", "dev": true } }, @@ -11678,9 +11678,9 @@ "dev": true }, "zurk": { - "version": "0.0.27", - "resolved": "https://registry.npmjs.org/zurk/-/zurk-0.0.27.tgz", - "integrity": "sha512-BwvvxnPmiiI0H8ThWj7XSUrMN33X/JTb+qiA7/2M03jwwFtCGPwDxpNzKbI9aNKRkJu1v6XD73Y5aYY+eNyGSg==", + "version": "0.0.31", + "resolved": "https://registry.npmjs.org/zurk/-/zurk-0.0.31.tgz", + "integrity": "sha512-rYp8YgJFaNgTEe9/s607CqIxhJtls8Uik6hQGiPOus+5iyKphc5Xk5stLzE1bO+JnY73T/6MXI8DbnpkJQXYpA==", "dev": true } } diff --git a/package.json b/package.json index 08fefdbcf9..d299972c72 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/src/core.ts b/src/core.ts index 7634ba0652..5d84151200 100644 --- a/src/core.ts +++ b/src/core.ts @@ -49,6 +49,7 @@ export interface Options { [processCwd]: string cwd?: string verbose: boolean + ac?: AbortController env: NodeJS.ProcessEnv shell: string | boolean nothrow: boolean @@ -200,6 +201,7 @@ export class ProcessPromise extends Promise { this.zurk = exec({ cmd: $.prefix + this._command, cwd: $.cwd ?? $[processCwd], + ac: $.ac, shell: typeof $.shell === 'string' ? $.shell : true, env: $.env, spawn: $.spawn, @@ -354,6 +356,13 @@ export class ProcessPromise extends Promise { } } + 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 { if (!this.child) throw new Error('Trying to kill a process without creating one.') diff --git a/test/core.test.js b/test/core.test.js index ef76bfd8c0..b04a5b9285 100644 --- a/test/core.test.js +++ b/test/core.test.js @@ -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(() => {