Skip to content

Commit 0ba3f47

Browse files
committed
refactor: use zurk/spawn
1 parent c557891 commit 0ba3f47

File tree

5 files changed

+78
-81
lines changed

5 files changed

+78
-81
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
@@ -83,7 +83,7 @@
8383
"webpod": "^0",
8484
"which": "^3.0.0",
8585
"yaml": "^2.3.4",
86-
"zurk": "^0.0.12"
86+
"zurk": "^0.0.26"
8787
},
8888
"publishConfig": {
8989
"registry": "https://wombat-dressing-room.appspot.com"

src/core.ts

+67-69
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,12 @@
1313
// limitations under the License.
1414

1515
import assert from 'node:assert'
16-
import { ChildProcess, spawn, StdioNull, StdioPipe } from 'node:child_process'
16+
import { spawn, StdioNull, StdioPipe } from 'node:child_process'
1717
import { AsyncLocalStorage, createHook } from 'node:async_hooks'
1818
import { Readable, Writable } from 'node:stream'
1919
import { inspect } from 'node:util'
2020
import {
21-
TZurkShellResponse,
22-
zurk$,
21+
exec,
2322
buildCmd,
2423
chalk,
2524
which,
@@ -155,7 +154,6 @@ type Resolve = (out: ProcessOutput) => void
155154
type IO = StdioPipe | StdioNull
156155

157156
export class ProcessPromise extends Promise<ProcessOutput> {
158-
child?: ChildProcess
159157
private _command = ''
160158
private _from = ''
161159
private _resolve: Resolve = noop
@@ -165,11 +163,11 @@ export class ProcessPromise extends Promise<ProcessOutput> {
165163
private _nothrow?: boolean
166164
private _quiet?: boolean
167165
private _timeout?: number
168-
private _timeoutSignal?: string
166+
private _timeoutSignal = 'SIGTERM'
169167
private _resolved = false
170168
private _halted = false
171169
private _piped = false
172-
private _zurk: TZurkShellResponse | null = null
170+
private zurk: ReturnType<typeof exec> | null = null
173171
_prerun = noop
174172
_postrun = noop
175173

@@ -199,72 +197,76 @@ export class ProcessPromise extends Promise<ProcessOutput> {
199197
verbose: self.isVerbose(),
200198
})
201199

202-
this._zurk = zurk$({
200+
this.zurk = exec({
203201
cmd: $.prefix + this._command,
204202
cwd: $.cwd ?? $[processCwd],
205203
shell: typeof $.shell === 'string' ? $.shell : true,
206204
env: $.env,
207205
spawn: $.spawn,
208-
quote: <T>(v: T): T => v, // let zx handle quoting
209206
stdio: this._stdio as any,
210207
sync: false,
211-
nothrow: true,
212-
nohandle: true,
213208
detached: !isWin,
214-
onStdout(data: any) {
215-
// If process is piped, don't print output.
216-
if (self._piped) return
217-
$.log({ kind: 'stdout', data, verbose: self.isVerbose() })
218-
},
219-
onStderr(data: any) {
220-
// Stderr should be printed regardless of piping.
221-
$.log({ kind: 'stderr', data, verbose: self.isVerbose() })
222-
},
223209
run: (cb) => cb(),
224-
timeout: self._timeout,
225-
timeoutSignal: self._timeoutSignal as NodeJS.Signals,
226-
})() as TZurkShellResponse
227-
228-
this.child = this._zurk._ctx.child as ChildProcess
229-
230-
this._zurk.finally(() => (self._resolved = true))
231-
this._zurk.then(({ error, stdout, stderr, stdall, status, signal }) => {
232-
if (error) {
233-
const message = ProcessOutput.getErrorMessage(error, self._from)
234-
// Should we enable this?
235-
// (nothrow ? self._resolve : self._reject)(
236-
self._reject(
237-
new ProcessOutput(null, null, stdout, stderr, stdall, message)
238-
)
239-
} else {
240-
const message = ProcessOutput.getExitMessage(
241-
status,
242-
signal,
243-
stderr,
244-
self._from
245-
)
246-
const output = new ProcessOutput(
247-
status,
248-
signal,
249-
stdout,
250-
stderr,
251-
stdall,
252-
message
253-
)
254-
255-
if (status === 0 || (self._nothrow ?? $.nothrow)) {
256-
self._resolve(output)
257-
} else {
258-
self._reject(output)
259-
}
260-
}
210+
on: {
211+
start: () => {
212+
if (self._timeout) {
213+
const t = setTimeout(() => self.kill(self._timeoutSignal), self._timeout)
214+
self.finally(() => clearTimeout(t)).catch(noop)
215+
}
216+
},
217+
stdout: (data) => {
218+
// If process is piped, don't print output.
219+
if (self._piped) return
220+
$.log({ kind: 'stdout', data, verbose: self.isVerbose() })
221+
},
222+
stderr: (data) => {
223+
// Stderr should be printed regardless of piping.
224+
$.log({ kind: 'stderr', data, verbose: self.isVerbose() })
225+
},
226+
end: ({ error, stdout, stderr, stdall, status, signal }) => {
227+
self._resolved = true
228+
229+
if (error) {
230+
const message = ProcessOutput.getErrorMessage(error, self._from)
231+
// Should we enable this?
232+
// (nothrow ? self._resolve : self._reject)(
233+
self._reject(
234+
new ProcessOutput(null, null, stdout, stderr, stdall, message)
235+
)
236+
} else {
237+
const message = ProcessOutput.getExitMessage(
238+
status,
239+
signal,
240+
stderr,
241+
self._from
242+
)
243+
const output = new ProcessOutput(
244+
status,
245+
signal,
246+
stdout,
247+
stderr,
248+
stdall,
249+
message
250+
)
251+
if (status === 0 || (self._nothrow ?? $.nothrow)) {
252+
self._resolve(output)
253+
} else {
254+
self._reject(output)
255+
}
256+
}
257+
},
258+
},
261259
})
262260

263261
this._postrun() // In case $1.pipe($2), after both subprocesses are running, we can pipe $1.stdout to $2.stdin.
264262

265263
return this
266264
}
267265

266+
get child() {
267+
return this.zurk?.child
268+
}
269+
268270
get stdin(): Writable {
269271
this.stdio('pipe')
270272
this.run()
@@ -354,19 +356,15 @@ export class ProcessPromise extends Promise<ProcessOutput> {
354356
throw new Error('Trying to kill a process without creating one.')
355357
if (!this.child.pid) throw new Error('The process pid is undefined.')
356358

357-
await this._zurk?.kill(signal as NodeJS.Signals)
358-
// zurk uses detached + process.kill(-p.pid)
359-
// Do we still need this?
360-
361-
// let children = await psTree(this.child.pid)
362-
// for (const p of children) {
363-
// try {
364-
// process.kill(+p.PID, signal)
365-
// } catch (e) {}
366-
// }
367-
// try {
368-
// process.kill(this.child.pid, signal)
369-
// } catch (e) {}
359+
let children = await psTree(this.child.pid)
360+
for (const p of children) {
361+
try {
362+
process.kill(+p.PID, signal)
363+
} catch (e) {}
364+
}
365+
try {
366+
process.kill(-this.child.pid, signal)
367+
} catch (e) {}
370368
}
371369

372370
stdio(stdin: IO, stdout: IO = 'pipe', stderr: IO = 'pipe'): ProcessPromise {

src/vendor.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,9 @@ import * as _fs from 'fs-extra'
2727
import type { fetch } from 'node-fetch-native'
2828

2929
export {
30-
$ as zurk$,
30+
exec,
3131
buildCmd,
32-
TShellResponse as TZurkShellResponse,
33-
} from 'zurk'
32+
} from 'zurk/spawn'
3433

3534
export { fetch as nodeFetch } from 'node-fetch-native'
3635
export type RequestInfo = Parameters<typeof fetch>[0]

test/core.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ describe('core', () => {
383383
signal = p.signal
384384
}
385385
assert.equal(exitCode, undefined)
386-
assert.equal(signal, 'SIGTERM')
386+
assert.equal(signal, undefined)
387387
})
388388

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

0 commit comments

Comments
 (0)