Skip to content

Commit ef81041

Browse files
committed
chore: handle timeouts via zurk
1 parent 80dff3b commit ef81041

File tree

4 files changed

+50
-29
lines changed

4 files changed

+50
-29
lines changed

package-lock.json

+7-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,6 @@
100100
"author": "Anton Medvedev <anton@medv.io>",
101101
"license": "Apache-2.0",
102102
"dependencies": {
103-
"zurk": "^0.0.3"
103+
"zurk": "^0.0.4"
104104
}
105105
}

src/core.ts

+41-20
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ export class ProcessPromise extends Promise<ProcessOutput> {
183183

184184
run(): ProcessPromise {
185185
const $ = this._snapshot
186+
const self = this
186187
if (this.child) return this // The _run() can be called from a few places.
187188
this._prerun() // In case $1.pipe($2), the $2 returned, and on $2._run() invoke $1._run().
188189

@@ -193,22 +194,38 @@ export class ProcessPromise extends Promise<ProcessOutput> {
193194
})
194195

195196
this._zurk = zurk$({
196-
get cwd() { return $.cwd ?? $[processCwd] },
197197
cmd: $.prefix + this._command,
198+
get cwd() { return $.cwd ?? $[processCwd] },
198199
get shell() { return typeof $.shell === 'string' ? $.shell : true },
199200
get env() { return $.env },
200-
stdio: this._stdio as any,
201201
get spawn() { return $.spawn },
202-
run: cb => cb(),
202+
stdio: this._stdio as any,
203203
sync: false,
204-
nothrow: true
204+
nothrow: true,
205+
onStdout(data: any) { $.log({ kind: 'stdout', data, verbose: $.verbose && !self._quiet }) },
206+
onStderr(data: any) { $.log({ kind: 'stderr', data, verbose: $.verbose && !self._quiet }) },
207+
run: cb => cb(),
208+
timeout: self._timeout,
209+
timeoutSignal: self._timeoutSignal as NodeJS.Signals,
205210
})() as TZurkShellResponse
206211

207212
this.child = this._zurk._ctx.child as ChildProcess
208213

209-
// this._zurk.then(({}) => {
210-
//
211-
// })
214+
this._zurk.finally(() => self._resolved = true)
215+
this._zurk.then(({
216+
error,
217+
stdout,
218+
stderr,
219+
status: code,
220+
signal
221+
}) => {
222+
if (error) {
223+
224+
} else {
225+
226+
}
227+
228+
})
212229

213230
// this.child = $.spawn($.prefix + this._command, {
214231
// cwd: $.cwd ?? $[processCwd],
@@ -233,39 +250,35 @@ export class ProcessPromise extends Promise<ProcessOutput> {
233250
} else {
234251
this._reject(output)
235252
}
236-
this._resolved = true
253+
// this._resolved = true
237254
})
238255
this.child.on('error', (err: NodeJS.ErrnoException) => {
239-
const message =
240-
`${err.message}\n` +
241-
` errno: ${err.errno} (${errnoMessage(err.errno)})\n` +
242-
` code: ${err.code}\n` +
243-
` at ${this._from}`
256+
const message = ProcessOutput.getErrorMessage(err, this._from)
244257
this._reject(
245258
new ProcessOutput(null, null, stdout, stderr, combined, message)
246259
)
247-
this._resolved = true
260+
// this._resolved = true
248261
})
249262
let stdout = '',
250263
stderr = '',
251264
combined = ''
252265
let onStdout = (data: any) => {
253-
$.log({ kind: 'stdout', data, verbose: $.verbose && !this._quiet })
266+
// $.log({ kind: 'stdout', data, verbose: $.verbose && !this._quiet })
254267
stdout += data
255268
combined += data
256269
}
257270
let onStderr = (data: any) => {
258-
$.log({ kind: 'stderr', data, verbose: $.verbose && !this._quiet })
271+
// $.log({ kind: 'stderr', data, verbose: $.verbose && !this._quiet })
259272
stderr += data
260273
combined += data
261274
}
262275
if (!this._piped) this.child.stdout?.on('data', onStdout) // If process is piped, don't collect or print output.
263276
this.child.stderr?.on('data', onStderr) // Stderr should be printed regardless of piping.
264277
this._postrun() // In case $1.pipe($2), after both subprocesses are running, we can pipe $1.stdout to $2.stdin.
265-
if (this._timeout && this._timeoutSignal) {
266-
const t = setTimeout(() => this.kill(this._timeoutSignal), this._timeout)
267-
this.finally(() => clearTimeout(t)).catch(noop)
268-
}
278+
// if (this._timeout && this._timeoutSignal) {
279+
// const t = setTimeout(() => this.kill(this._timeoutSignal), this._timeout)
280+
// this.finally(() => clearTimeout(t)).catch(noop)
281+
// }
269282
return this
270283
}
271284

@@ -457,6 +470,14 @@ export class ProcessOutput extends Error {
457470
return message
458471
}
459472

473+
static getErrorMessage(err: NodeJS.ErrnoException, from: string) {
474+
return ``+
475+
`${err.message}\n` +
476+
` errno: ${err.errno} (${errnoMessage(err.errno)})\n` +
477+
` code: ${err.code}\n` +
478+
` at ${from}`
479+
}
480+
460481
[inspect.custom]() {
461482
let stringify = (s: string, c: ChalkInstance) =>
462483
s.length === 0 ? "''" : c(inspect(s))

test/core.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ describe('core', () => {
378378
signal = p.signal
379379
}
380380
assert.equal(exitCode, undefined)
381-
assert.equal(signal, undefined)
381+
assert.equal(signal, 'SIGTERM')
382382
})
383383

384384
test('$ thrown as error', async () => {

0 commit comments

Comments
 (0)