@@ -17,10 +17,7 @@ import { ChildProcess, spawn, StdioNull, StdioPipe } from 'node:child_process'
17
17
import { AsyncLocalStorage , createHook } from 'node:async_hooks'
18
18
import { Readable , Writable } from 'node:stream'
19
19
import { inspect } from 'node:util'
20
- import {
21
- $ as zurk$ ,
22
- TShellResponse as TZurkShellResponse
23
- } from 'zurk'
20
+ import { $ as zurk$ , TShellResponse as TZurkShellResponse } from 'zurk'
24
21
import {
25
22
chalk ,
26
23
which ,
87
84
defaults . prefix = 'set -euo pipefail;'
88
85
defaults . quote = quote
89
86
} catch ( err ) {
90
- if ( process . platform == 'win32' ) {
87
+ if ( isWin ) {
91
88
try {
92
89
defaults . shell = which . sync ( 'powershell.exe' )
93
90
defaults . quote = quotePowerShell
@@ -120,7 +117,6 @@ export const $ = new Proxy<Shell & Options>(
120
117
}
121
118
cmd += s + pieces [ ++ i ]
122
119
}
123
- isWin && console . log ( 'cmd=' , cmd )
124
120
promise . _bind ( cmd , from , resolve ! , reject ! , getStore ( ) )
125
121
// Postpone run to allow promise configuration.
126
122
setImmediate ( ( ) => promise . isHalted || promise . run ( ) )
@@ -196,114 +192,71 @@ export class ProcessPromise extends Promise<ProcessOutput> {
196
192
197
193
this . _zurk = zurk$ ( {
198
194
cmd : $ . prefix + this . _command ,
199
- get cwd ( ) { return $ . cwd ?? $ [ processCwd ] } ,
195
+ get cwd ( ) {
196
+ return $ . cwd ?? $ [ processCwd ]
197
+ } ,
200
198
get shell ( ) {
201
- isWin && console . log ( '$.shell=' , $ . shell )
202
199
return typeof $ . shell === 'string' ? $ . shell : true
203
200
} ,
204
- get env ( ) { return $ . env } ,
205
- get spawn ( ) { return $ . spawn } ,
201
+ get env ( ) {
202
+ return $ . env
203
+ } ,
204
+ get spawn ( ) {
205
+ return $ . spawn
206
+ } ,
206
207
quote : < T > ( v : T ) : T => v , // let zx handle quoting
207
208
stdio : this . _stdio as any ,
208
209
sync : false ,
209
210
nothrow : true ,
210
211
nohandle : true ,
211
212
detached : ! isWin ,
212
- onStdout ( data : any ) { $ . log ( { kind : 'stdout' , data, verbose : $ . verbose && ! self . _quiet } ) } ,
213
- onStderr ( data : any ) { $ . log ( { kind : 'stderr' , data, verbose : $ . verbose && ! self . _quiet } ) } ,
214
- run : cb => cb ( ) ,
213
+ onStdout ( data : any ) {
214
+ $ . log ( { kind : 'stdout' , data, verbose : $ . verbose && ! self . _quiet } )
215
+ } ,
216
+ onStderr ( data : any ) {
217
+ $ . log ( { kind : 'stderr' , data, verbose : $ . verbose && ! self . _quiet } )
218
+ } ,
219
+ run : ( cb ) => cb ( ) ,
215
220
timeout : self . _timeout ,
216
221
timeoutSignal : self . _timeoutSignal as NodeJS . Signals ,
217
222
} ) ( ) as TZurkShellResponse
218
223
219
224
this . child = this . _zurk . _ctx . child as ChildProcess
220
225
221
- this . _zurk . finally ( ( ) => self . _resolved = true )
222
- this . _zurk . then ( ( {
223
- error,
224
- stdout,
225
- stderr,
226
- stdall,
227
- status,
228
- signal
229
- } ) => {
230
- isWin && console . log ( 'ctx=' , this . _zurk ?. _ctx )
226
+ this . _zurk . finally ( ( ) => ( self . _resolved = true ) )
227
+ this . _zurk . then ( ( { error, stdout, stderr, stdall, status, signal } ) => {
231
228
if ( error ) {
232
229
const message = ProcessOutput . getErrorMessage ( error , self . _from )
233
230
self . _reject (
234
231
new ProcessOutput ( null , null , stdout , stderr , stdall , message )
235
232
)
236
233
} else {
237
- const message = ProcessOutput . getMessage ( status , signal , stderr , self . _from )
234
+ const message = ProcessOutput . getMessage (
235
+ status ,
236
+ signal ,
237
+ stderr ,
238
+ self . _from
239
+ )
238
240
const output = new ProcessOutput (
239
241
status ,
240
242
signal ,
241
243
stdout ,
242
244
stderr ,
243
245
stdall ,
244
- message ,
246
+ message
245
247
)
246
248
if ( status === 0 || self . _nothrow ) {
247
249
self . _resolve ( output )
248
250
} else {
249
251
self . _reject ( output )
250
252
}
251
253
}
252
-
253
254
} )
254
255
255
- // this.child = $.spawn($.prefix + this._command, {
256
- // cwd: $.cwd ?? $[processCwd],
257
- // shell: typeof $.shell === 'string' ? $.shell : true,
258
- // stdio: this._stdio,
259
- // windowsHide: true,
260
- // env: $.env,
261
- // })
262
-
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
256
// if (!this._piped) this.child.stdout?.on('data', onStdout) // If process is piped, don't collect or print output.
301
257
// this.child.stderr?.on('data', onStderr) // Stderr should be printed regardless of piping.
302
258
this . _postrun ( ) // In case $1.pipe($2), after both subprocesses are running, we can pipe $1.stdout to $2.stdin.
303
- // if (this._timeout && this._timeoutSignal) {
304
- // const t = setTimeout(() => this.kill(this._timeoutSignal), this._timeout)
305
- // this.finally(() => clearTimeout(t)).catch(noop)
306
- // }
259
+
307
260
return this
308
261
}
309
262
@@ -482,7 +435,12 @@ export class ProcessOutput extends Error {
482
435
return this . _signal
483
436
}
484
437
485
- static getMessage ( code : number | null , signal : NodeJS . Signals | null , stderr : string , from : string ) {
438
+ static getMessage (
439
+ code : number | null ,
440
+ signal : NodeJS . Signals | null ,
441
+ stderr : string ,
442
+ from : string
443
+ ) {
486
444
let message = `exit code: ${ code } `
487
445
if ( code != 0 || signal != null ) {
488
446
message = `${ stderr || '\n' } at ${ from } `
@@ -498,11 +456,12 @@ export class ProcessOutput extends Error {
498
456
}
499
457
500
458
static getErrorMessage ( err : NodeJS . ErrnoException , from : string ) {
501
- return `` +
459
+ return (
502
460
`${ err . message } \n` +
503
461
` errno: ${ err . errno } (${ errnoMessage ( err . errno ) } )\n` +
504
462
` code: ${ err . code } \n` +
505
463
` at ${ from } `
464
+ )
506
465
}
507
466
508
467
[ inspect . custom ] ( ) {
0 commit comments