Commit a3205e1

Anton Golub <antongolub@antongolub.com>
2024-05-08 19:01:06
feat: let timeout be configurable via $ opts (#796)
1 parent b6420eb
Changed files (2)
src/core.ts
@@ -59,6 +59,8 @@ export interface Options {
   ac?: AbortController
   signal?: AbortSignal
   input?: string | Buffer | Readable | ProcessOutput | ProcessPromise
+  timeout?: Duration
+  timeoutSignal?: string
   stdio: StdioOptions
   verbose: boolean
   sync: boolean
@@ -246,6 +248,7 @@ export class ProcessPromise extends Promise<ProcessOutput> {
     const input = ($.input as ProcessPromise | ProcessOutput)?.stdout ?? $.input
 
     if (input) this.stdio('pipe')
+    if ($.timeout) this.timeout($.timeout, $.timeoutSignal)
 
     $.log({
       kind: 'cmd',
test/core.test.js
@@ -480,6 +480,21 @@ describe('core', () => {
     assert.equal(signal, 'SIGKILL')
   })
 
+  test('timeout is configurable via opts', async () => {
+    let exitCode, signal
+    try {
+      await $({
+        timeout: 10,
+        timeoutSignal: 'SIGKILL',
+      })`sleep 999`
+    } catch (p) {
+      exitCode = p.exitCode
+      signal = p.signal
+    }
+    assert.equal(exitCode, null)
+    assert.equal(signal, 'SIGKILL')
+  })
+
   test('timeout() expiration works', async () => {
     let exitCode, signal
     try {