From 5cad24118bb823e40b60fc42bba2a1c0afa10540 Mon Sep 17 00:00:00 2001
From: Anton Golub <antongolub@antongolub.com>
Date: Sat, 6 Apr 2024 09:27:01 +0300
Subject: [PATCH] feat: support `signal` opt

closes #768
relates #734
---
 package-lock.json                          | 18 ++++++++++++------
 package.json                               |  2 +-
 src/core.ts                                |  2 ++
 test/core.test.js                          | 14 ++++++++++++++
 test/fixtures/js-project/package-lock.json |  4 ++--
 5 files changed, 31 insertions(+), 9 deletions(-)

diff --git a/package-lock.json b/package-lock.json
index 850b3c1962..98f48e4996 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1,12 +1,12 @@
 {
   "name": "zx",
-  "version": "7.2.3",
+  "version": "8.0.0",
   "lockfileVersion": 3,
   "requires": true,
   "packages": {
     "": {
       "name": "zx",
-      "version": "7.2.3",
+      "version": "8.0.0",
       "license": "Apache-2.0",
       "bin": {
         "zx": "build/cli.js"
@@ -36,7 +36,7 @@
         "typescript": "^5.4.4",
         "which": "^4.0.0",
         "yaml": "^2.4.1",
-        "zurk": "^0.0.32"
+        "zurk": "^0.1.0"
       },
       "engines": {
         "node": ">= 16.0.0"
@@ -518,6 +518,12 @@
         "zurk": "^0.0.32"
       }
     },
+    "node_modules/@webpod/ps/node_modules/zurk": {
+      "version": "0.0.32",
+      "resolved": "https://registry.npmjs.org/zurk/-/zurk-0.0.32.tgz",
+      "integrity": "sha512-KouorYeuxuZORUiDoOMUCgpjgHgMP+ks9MpEPOT/JE5/df9bIr6DjqIiaQGhzH9ZLG00bWBZJazCpwO0zSqq7g==",
+      "dev": true
+    },
     "node_modules/any-promise": {
       "version": "1.3.0",
       "resolved": "https://registry.npmjs.org/any-promise/-/any-promise-1.3.0.tgz",
@@ -4378,9 +4384,9 @@
       }
     },
     "node_modules/zurk": {
-      "version": "0.0.32",
-      "resolved": "https://registry.npmjs.org/zurk/-/zurk-0.0.32.tgz",
-      "integrity": "sha512-KouorYeuxuZORUiDoOMUCgpjgHgMP+ks9MpEPOT/JE5/df9bIr6DjqIiaQGhzH9ZLG00bWBZJazCpwO0zSqq7g==",
+      "version": "0.1.0",
+      "resolved": "https://registry.npmjs.org/zurk/-/zurk-0.1.0.tgz",
+      "integrity": "sha512-lJPh6OG7eBAkAw1zlB15U0tRSb7Cq/nZhcW1/gfNYB+HUTwzlK1PvKDp6zV6jyix8xYiTnIfiHGPMVav8x3YuQ==",
       "dev": true
     }
   }
diff --git a/package.json b/package.json
index 9f0b6a04eb..bf7b13e7bc 100644
--- a/package.json
+++ b/package.json
@@ -80,7 +80,7 @@
     "typescript": "^5.4.4",
     "which": "^4.0.0",
     "yaml": "^2.4.1",
-    "zurk": "^0.0.32"
+    "zurk": "^0.1.0"
   },
   "publishConfig": {
     "registry": "https://wombat-dressing-room.appspot.com"
diff --git a/src/core.ts b/src/core.ts
index d6389f6702..9c5fabc1e7 100644
--- a/src/core.ts
+++ b/src/core.ts
@@ -57,6 +57,7 @@ export interface Options {
   [syncExec]: boolean
   cwd?: string
   ac?: AbortController
+  signal?: AbortSignal
   input?: string | Buffer | Readable | ProcessOutput | ProcessPromise
   verbose: boolean
   sync: boolean
@@ -247,6 +248,7 @@ export class ProcessPromise extends Promise<ProcessOutput> {
       cmd: $.prefix + this._command + $.postfix,
       cwd: $.cwd ?? $[processCwd],
       ac: $.ac,
+      signal: $.signal,
       shell: typeof $.shell === 'string' ? $.shell : true,
       env: $.env,
       spawn: $.spawn,
diff --git a/test/core.test.js b/test/core.test.js
index 7a2012c158..231d0b9818 100644
--- a/test/core.test.js
+++ b/test/core.test.js
@@ -342,6 +342,20 @@ describe('core', () => {
     }
   })
 
+  test('accepts AbortController `signal` separately', async () => {
+    const ac = new AbortController()
+    const signal = ac.signal
+    const p = $({ signal })`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(() => {
diff --git a/test/fixtures/js-project/package-lock.json b/test/fixtures/js-project/package-lock.json
index 9c84b635d7..62a367a192 100644
--- a/test/fixtures/js-project/package-lock.json
+++ b/test/fixtures/js-project/package-lock.json
@@ -9,7 +9,7 @@
       }
     },
     "../../..": {
-      "version": "7.2.3",
+      "version": "8.0.0",
       "license": "Apache-2.0",
       "bin": {
         "zx": "build/cli.js"
@@ -39,7 +39,7 @@
         "typescript": "^5.4.4",
         "which": "^4.0.0",
         "yaml": "^2.4.1",
-        "zurk": "^0.0.32"
+        "zurk": "^0.1.0"
       },
       "engines": {
         "node": ">= 16.0.0"