Skip to content

Commit 6023630

Browse files
committed
fix: invoke setTimeout on process run only
fix: ignore `timeout` for sync processes
1 parent 343bda0 commit 6023630

File tree

3 files changed

+17
-9
lines changed

3 files changed

+17
-9
lines changed

.size-limit.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
{
1010
"name": "zx/index",
1111
"path": "build/*.{js,cjs}",
12-
"limit": "806 kB",
12+
"limit": "807 kB",
1313
"brotli": false,
1414
"gzip": false
1515
},

docs/api.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -75,22 +75,26 @@ interface Options {
7575
signal: AbortSignal
7676
input: string | Buffer | Readable | ProcessOutput | ProcessPromise
7777
timeout: Duration
78-
timeoutSignal: string
78+
timeoutSignal: NodeJS.Signals
7979
stdio: StdioOptions
8080
verbose: boolean
8181
sync: boolean
8282
env: NodeJS.ProcessEnv
83-
shell: string | boolean
83+
shell: string | true
8484
nothrow: boolean
8585
prefix: string
8686
postfix: string
8787
quote: typeof quote
8888
quiet: boolean
8989
detached: boolean
90+
preferLocal: boolean | string | string[]
9091
spawn: typeof spawn
9192
spawnSync: typeof spawnSync
93+
store: TSpawnStore
9294
log: typeof log
9395
kill: typeof kill
96+
killSignal: NodeJS.Signals
97+
halt: boolean
9498
}
9599
```
96100

src/core.ts

+10-6
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,10 @@ export class ProcessPromise extends Promise<ProcessOutput> {
255255

256256
const self = this
257257
const $ = this._snapshot
258+
const sync = $[SYNC]
259+
const timeout = self._timeout ?? $.timeout
260+
const timeoutSignal = self._timeoutSignal ?? $.timeoutSignal
258261

259-
if ($.timeout) this.timeout($.timeout, $.timeoutSignal)
260262
if ($.preferLocal) {
261263
const dirs =
262264
$.preferLocal === true ? [$.cwd, $[CWD]] : [$.preferLocal].flat()
@@ -265,12 +267,13 @@ export class ProcessPromise extends Promise<ProcessOutput> {
265267

266268
$.log({
267269
kind: 'cmd',
268-
cmd: this._command,
270+
cmd: self.cmd,
269271
verbose: self.isVerbose(),
270272
})
271273

272274
// prettier-ignore
273275
this._zurk = exec({
276+
sync,
274277
id: self.id,
275278
cmd: self.fullCmd,
276279
cwd: $.cwd ?? $[CWD],
@@ -284,13 +287,12 @@ export class ProcessPromise extends Promise<ProcessOutput> {
284287
store: $.store,
285288
stdin: self._stdin,
286289
stdio: self._stdio ?? $.stdio,
287-
sync: $[SYNC],
288290
detached: $.detached,
289291
ee: self._ee,
290292
run: (cb) => cb(),
291293
on: {
292294
start: () => {
293-
self._timeout && self.timeout(self._timeout, self._timeoutSignal)
295+
!sync && timeout && self.timeout(timeout, timeoutSignal)
294296
},
295297
stdout: (data) => {
296298
// If process is piped, don't print output.
@@ -360,7 +362,7 @@ export class ProcessPromise extends Promise<ProcessOutput> {
360362
}})
361363
}
362364
private _pipe(
363-
source: 'stdout' | 'stderr',
365+
source: keyof TSpawnStore,
364366
dest: PipeDest,
365367
...args: any[]
366368
): (Writable & PromiseLike<ProcessPromise & Writable>) | ProcessPromise {
@@ -513,11 +515,13 @@ export class ProcessPromise extends Promise<ProcessOutput> {
513515
}
514516

515517
timeout(d: Duration, signal = $.timeoutSignal): ProcessPromise {
518+
if (this._resolved) return this
519+
516520
this._timeout = parseDuration(d)
517521
this._timeoutSignal = signal
518522

519523
if (this._timeoutId) clearTimeout(this._timeoutId)
520-
if (this._timeout) {
524+
if (this._timeout && this._run) {
521525
this._timeoutId = setTimeout(
522526
() => this.kill(this._timeoutSignal),
523527
this._timeout

0 commit comments

Comments
 (0)