Skip to content

Commit 7a7ca81

Browse files
committed
chore: use zurk response to build ProcessOutput
1 parent a1353b0 commit 7a7ca81

File tree

4 files changed

+86
-61
lines changed

4 files changed

+86
-61
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.6"
103+
"zurk": "^0.0.9"
104104
}
105105
}

src/core.ts

+76-53
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export const defaults: Options = {
8181
spawn,
8282
log,
8383
}
84-
84+
const isWin = process.platform == 'win32'
8585
try {
8686
defaults.shell = which.sync('bash')
8787
defaults.prefix = 'set -euo pipefail;'
@@ -120,6 +120,7 @@ export const $ = new Proxy<Shell & Options>(
120120
}
121121
cmd += s + pieces[++i]
122122
}
123+
isWin && console.log('cmd=', cmd)
123124
promise._bind(cmd, from, resolve!, reject!, getStore())
124125
// Postpone run to allow promise configuration.
125126
setImmediate(() => promise.isHalted || promise.run())
@@ -197,7 +198,7 @@ export class ProcessPromise extends Promise<ProcessOutput> {
197198
cmd: $.prefix + this._command,
198199
get cwd() { return $.cwd ?? $[processCwd] },
199200
get shell() {
200-
console.log('!!!shell=', $.shell)
201+
isWin && console.log('$.shell=', $.shell)
201202
return typeof $.shell === 'string' ? $.shell : true
202203
},
203204
get env() { return $.env },
@@ -206,6 +207,8 @@ export class ProcessPromise extends Promise<ProcessOutput> {
206207
stdio: this._stdio as any,
207208
sync: false,
208209
nothrow: true,
210+
nohandle: true,
211+
detached: !isWin,
209212
onStdout(data: any) { $.log({ kind: 'stdout', data, verbose: $.verbose && !self._quiet }) },
210213
onStderr(data: any) { $.log({ kind: 'stderr', data, verbose: $.verbose && !self._quiet }) },
211214
run: cb => cb(),
@@ -220,13 +223,31 @@ export class ProcessPromise extends Promise<ProcessOutput> {
220223
error,
221224
stdout,
222225
stderr,
223-
status: code,
226+
stdall,
227+
status,
224228
signal
225229
}) => {
230+
isWin && console.log('ctx=', this._zurk?._ctx)
226231
if (error) {
227-
232+
const message = ProcessOutput.getErrorMessage(error, self._from)
233+
self._reject(
234+
new ProcessOutput(null, null, stdout, stderr, stdall, message)
235+
)
228236
} else {
229-
237+
const message = ProcessOutput.getMessage(status, signal, stderr, self._from)
238+
const output = new ProcessOutput(
239+
status,
240+
signal,
241+
stdout,
242+
stderr,
243+
stdall,
244+
message,
245+
)
246+
if (status === 0 || self._nothrow) {
247+
self._resolve(output)
248+
} else {
249+
self._reject(output)
250+
}
230251
}
231252

232253
})
@@ -239,45 +260,45 @@ export class ProcessPromise extends Promise<ProcessOutput> {
239260
// env: $.env,
240261
// })
241262

242-
this.child.on('close', (code, signal) => {
243-
let message = ProcessOutput.getMessage(code, signal, stderr, this._from)
244-
let output = new ProcessOutput(
245-
code,
246-
signal,
247-
stdout,
248-
stderr,
249-
combined,
250-
message
251-
)
252-
if (code === 0 || this._nothrow) {
253-
this._resolve(output)
254-
} else {
255-
this._reject(output)
256-
}
257-
// this._resolved = true
258-
})
259-
this.child.on('error', (err: NodeJS.ErrnoException) => {
260-
const message = ProcessOutput.getErrorMessage(err, this._from)
261-
this._reject(
262-
new ProcessOutput(null, null, stdout, stderr, combined, message)
263-
)
264-
// this._resolved = true
265-
})
266-
let stdout = '',
267-
stderr = '',
268-
combined = ''
269-
let onStdout = (data: any) => {
270-
// $.log({ kind: 'stdout', data, verbose: $.verbose && !this._quiet })
271-
stdout += data
272-
combined += data
273-
}
274-
let onStderr = (data: any) => {
275-
// $.log({ kind: 'stderr', data, verbose: $.verbose && !this._quiet })
276-
stderr += data
277-
combined += data
278-
}
279-
if (!this._piped) this.child.stdout?.on('data', onStdout) // If process is piped, don't collect or print output.
280-
this.child.stderr?.on('data', onStderr) // Stderr should be printed regardless of piping.
263+
// this.child.on('close', (code, signal) => {
264+
// // let message = ProcessOutput.getMessage(code, signal, stderr, this._from)
265+
// // let output = new ProcessOutput(
266+
// // code,
267+
// // signal,
268+
// // stdout,
269+
// // stderr,
270+
// // combined,
271+
// // message
272+
// // )
273+
// // if (code === 0 || this._nothrow) {
274+
// // this._resolve(output)
275+
// // } else {
276+
// // this._reject(output)
277+
// // }
278+
// // this._resolved = true
279+
// })
280+
// this.child.on('error', (err: NodeJS.ErrnoException) => {
281+
// // const message = ProcessOutput.getErrorMessage(err, this._from)
282+
// // this._reject(
283+
// // new ProcessOutput(null, null, stdout, stderr, combined, message)
284+
// // )
285+
// // this._resolved = true
286+
// })
287+
// let stdout = '',
288+
// stderr = '',
289+
// combined = ''
290+
// let onStdout = (data: any) => {
291+
// // $.log({ kind: 'stdout', data, verbose: $.verbose && !this._quiet })
292+
// stdout += data
293+
// combined += data
294+
// }
295+
// let onStderr = (data: any) => {
296+
// // $.log({ kind: 'stderr', data, verbose: $.verbose && !this._quiet })
297+
// stderr += data
298+
// combined += data
299+
// }
300+
// if (!this._piped) this.child.stdout?.on('data', onStdout) // If process is piped, don't collect or print output.
301+
// this.child.stderr?.on('data', onStderr) // Stderr should be printed regardless of piping.
281302
this._postrun() // In case $1.pipe($2), after both subprocesses are running, we can pipe $1.stdout to $2.stdin.
282303
// if (this._timeout && this._timeoutSignal) {
283304
// const t = setTimeout(() => this.kill(this._timeoutSignal), this._timeout)
@@ -374,15 +395,17 @@ export class ProcessPromise extends Promise<ProcessOutput> {
374395
if (!this.child)
375396
throw new Error('Trying to kill a process without creating one.')
376397
if (!this.child.pid) throw new Error('The process pid is undefined.')
377-
let children = await psTree(this.child.pid)
378-
for (const p of children) {
379-
try {
380-
process.kill(+p.PID, signal)
381-
} catch (e) {}
382-
}
383-
try {
384-
process.kill(this.child.pid, signal)
385-
} catch (e) {}
398+
399+
await this._zurk?.kill(signal as NodeJS.Signals)
400+
// let children = await psTree(this.child.pid)
401+
// for (const p of children) {
402+
// try {
403+
// process.kill(+p.PID, signal)
404+
// } catch (e) {}
405+
// }
406+
// try {
407+
// process.kill(this.child.pid, signal)
408+
// } catch (e) {}
386409
}
387410

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

test/win32.test.js

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ _describe('win32', () => {
2727
const p = await $`echo $0` // Bash is first by default.
2828
assert.match(p.stdout, /bash/)
2929
await within(async () => {
30+
$.prefix = ''
3031
$.shell = which.sync('powershell.exe')
3132
$.quote = quotePowerShell
3233
const p = await $`get-host`
@@ -36,6 +37,7 @@ _describe('win32', () => {
3637

3738
test('quotePowerShell works', async () => {
3839
await within(async () => {
40+
$.prefix = ''
3941
$.shell = which.sync('powershell.exe')
4042
$.quote = quotePowerShell
4143
const p = await $`echo ${`Windows 'rulez!'`}`

0 commit comments

Comments
 (0)