-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.ts
505 lines (464 loc) · 12.7 KB
/
core.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import assert from 'node:assert'
import { ChildProcess, spawn, StdioNull, StdioPipe } from 'node:child_process'
import { AsyncLocalStorage, createHook } from 'node:async_hooks'
import { Readable, Writable } from 'node:stream'
import { inspect } from 'node:util'
import { RequestInfo, RequestInit } from 'node-fetch'
import chalk, { ChalkInstance } from 'chalk'
import which from 'which'
import {
Duration,
errnoMessage,
exitCodeInfo,
formatCmd,
noop,
parseDuration,
psTree,
quote,
quotePowerShell,
} from './util.js'
export type Shell = (
pieces: TemplateStringsArray,
...args: any[]
) => ProcessPromise
const processCwd = Symbol('processCwd')
export type Options = {
[processCwd]: string
cwd?: string
verbose: boolean
env: NodeJS.ProcessEnv
shell: string | boolean
prefix: string
quote: typeof quote
spawn: typeof spawn
log: typeof log
}
const storage = new AsyncLocalStorage<Options>()
const hook = createHook({
init: syncCwd,
before: syncCwd,
promiseResolve: syncCwd,
after: syncCwd,
destroy: syncCwd,
})
hook.enable()
export const defaults: Options = {
[processCwd]: process.cwd(),
verbose: true,
env: process.env,
shell: true,
prefix: '',
quote: () => {
throw new Error('No quote function is defined: https://ï.at/no-quote-func')
},
spawn,
log,
}
try {
defaults.shell = which.sync('bash')
defaults.prefix = 'set -euo pipefail;'
defaults.quote = quote
} catch (err) {
if (process.platform == 'win32') {
defaults.shell = which.sync('powershell.exe')
defaults.quote = quotePowerShell
}
}
function getStore() {
return storage.getStore() || defaults
}
export const $ = new Proxy<Shell & Options>(
function (pieces, ...args) {
const from = new Error().stack!.split(/^\s*at\s/m)[2].trim()
if (pieces.some((p) => p == undefined)) {
throw new Error(`Malformed command at ${from}`)
}
let resolve: Resolve, reject: Resolve
const promise = new ProcessPromise((...args) => ([resolve, reject] = args))
let cmd = pieces[0],
i = 0
while (i < args.length) {
let s
if (Array.isArray(args[i])) {
s = args[i].map((x: any) => $.quote(substitute(x))).join(' ')
} else {
s = $.quote(substitute(args[i]))
}
cmd += s + pieces[++i]
}
promise._bind(cmd, from, resolve!, reject!, getStore())
// Postpone run to allow promise configuration.
setImmediate(() => promise.isHalted || promise.run())
return promise
} as Shell & Options,
{
set(_, key, value) {
const target = key in Function.prototype ? _ : getStore()
Reflect.set(target, key, value)
return true
},
get(_, key) {
const target = key in Function.prototype ? _ : getStore()
return Reflect.get(target, key)
},
}
)
function substitute(arg: ProcessPromise | any) {
if (arg?.stdout) {
return arg.stdout.replace(/\n$/, '')
}
return `${arg}`
}
type Resolve = (out: ProcessOutput) => void
type IO = StdioPipe | StdioNull
export class ProcessPromise extends Promise<ProcessOutput> {
child?: ChildProcess
private _command = ''
private _from = ''
private _resolve: Resolve = noop
private _reject: Resolve = noop
private _snapshot = getStore()
private _stdio: [IO, IO, IO] = ['inherit', 'pipe', 'pipe']
private _nothrow = false
private _quiet = false
private _timeout?: number
private _timeoutSignal?: string
private _resolved = false
private _halted = false
private _piped = false
_prerun = noop
_postrun = noop
_bind(
cmd: string,
from: string,
resolve: Resolve,
reject: Resolve,
options: Options
) {
this._command = cmd
this._from = from
this._resolve = resolve
this._reject = reject
this._snapshot = { ...options }
}
run(): ProcessPromise {
const $ = this._snapshot
if (this.child) return this // The _run() can be called from a few places.
this._prerun() // In case $1.pipe($2), the $2 returned, and on $2._run() invoke $1._run().
$.log({
kind: 'cmd',
cmd: this._command,
verbose: $.verbose && !this._quiet,
})
this.child = $.spawn($.prefix + this._command, {
cwd: $.cwd ?? $[processCwd],
shell: typeof $.shell === 'string' ? $.shell : true,
stdio: this._stdio,
windowsHide: true,
env: $.env,
})
this.child.on('close', (code, signal) => {
let message = `exit code: ${code}`
if (code != 0 || signal != null) {
message = `${stderr || '\n'} at ${this._from}`
message += `\n exit code: ${code}${
exitCodeInfo(code) ? ' (' + exitCodeInfo(code) + ')' : ''
}`
if (signal != null) {
message += `\n signal: ${signal}`
}
}
let output = new ProcessOutput(
code,
signal,
stdout,
stderr,
combined,
message
)
if (code === 0 || this._nothrow) {
this._resolve(output)
} else {
this._reject(output)
}
this._resolved = true
})
this.child.on('error', (err: NodeJS.ErrnoException) => {
const message =
`${err.message}\n` +
` errno: ${err.errno} (${errnoMessage(err.errno)})\n` +
` code: ${err.code}\n` +
` at ${this._from}`
this._reject(
new ProcessOutput(null, null, stdout, stderr, combined, message)
)
this._resolved = true
})
let stdout = '',
stderr = '',
combined = ''
let onStdout = (data: any) => {
$.log({ kind: 'stdout', data, verbose: $.verbose && !this._quiet })
stdout += data
combined += data
}
let onStderr = (data: any) => {
$.log({ kind: 'stderr', data, verbose: $.verbose && !this._quiet })
stderr += data
combined += data
}
if (!this._piped) this.child.stdout?.on('data', onStdout) // If process is piped, don't collect or print output.
this.child.stderr?.on('data', onStderr) // Stderr should be printed regardless of piping.
this._postrun() // In case $1.pipe($2), after both subprocesses are running, we can pipe $1.stdout to $2.stdin.
if (this._timeout && this._timeoutSignal) {
const t = setTimeout(() => this.kill(this._timeoutSignal), this._timeout)
this.finally(() => clearTimeout(t)).catch(noop)
}
return this
}
get stdin(): Writable {
this.stdio('pipe')
this.run()
assert(this.child)
if (this.child.stdin == null)
throw new Error('The stdin of subprocess is null.')
return this.child.stdin
}
get stdout(): Readable {
this.run()
assert(this.child)
if (this.child.stdout == null)
throw new Error('The stdout of subprocess is null.')
return this.child.stdout
}
get stderr(): Readable {
this.run()
assert(this.child)
if (this.child.stderr == null)
throw new Error('The stderr of subprocess is null.')
return this.child.stderr
}
get exitCode(): Promise<number | null> {
return this.then(
(p) => p.exitCode,
(p) => p.exitCode
)
}
then<R = ProcessOutput, E = ProcessOutput>(
onfulfilled?:
| ((value: ProcessOutput) => PromiseLike<R> | R)
| undefined
| null,
onrejected?:
| ((reason: ProcessOutput) => PromiseLike<E> | E)
| undefined
| null
): Promise<R | E> {
if (this.isHalted && !this.child) {
throw new Error('The process is halted!')
}
return super.then(onfulfilled, onrejected)
}
catch<T = ProcessOutput>(
onrejected?:
| ((reason: ProcessOutput) => PromiseLike<T> | T)
| undefined
| null
): Promise<ProcessOutput | T> {
return super.catch(onrejected)
}
pipe(dest: Writable | ProcessPromise): ProcessPromise {
if (typeof dest == 'string')
throw new Error('The pipe() method does not take strings. Forgot $?')
if (this._resolved) {
if (dest instanceof ProcessPromise) dest.stdin.end() // In case of piped stdin, we may want to close stdin of dest as well.
throw new Error(
"The pipe() method shouldn't be called after promise is already resolved!"
)
}
this._piped = true
if (dest instanceof ProcessPromise) {
dest.stdio('pipe')
dest._prerun = this.run.bind(this)
dest._postrun = () => {
if (!dest.child)
throw new Error(
'Access to stdin of pipe destination without creation a subprocess.'
)
this.stdout.pipe(dest.stdin)
}
return dest
} else {
this._postrun = () => this.stdout.pipe(dest)
return this
}
}
async kill(signal = 'SIGTERM'): Promise<void> {
if (!this.child)
throw new Error('Trying to kill a process without creating one.')
if (!this.child.pid) throw new Error('The process pid is undefined.')
let children = await psTree(this.child.pid)
for (const p of children) {
try {
process.kill(+p.PID, signal)
} catch (e) {}
}
try {
process.kill(this.child.pid, signal)
} catch (e) {}
}
stdio(stdin: IO, stdout: IO = 'pipe', stderr: IO = 'pipe'): ProcessPromise {
this._stdio = [stdin, stdout, stderr]
return this
}
nothrow(): ProcessPromise {
this._nothrow = true
return this
}
quiet(): ProcessPromise {
this._quiet = true
return this
}
timeout(d: Duration, signal = 'SIGTERM'): ProcessPromise {
this._timeout = parseDuration(d)
this._timeoutSignal = signal
return this
}
halt(): ProcessPromise {
this._halted = true
return this
}
get isHalted(): boolean {
return this._halted
}
}
export class ProcessOutput extends Error {
private readonly _code: number | null
private readonly _signal: NodeJS.Signals | null
private readonly _stdout: string
private readonly _stderr: string
private readonly _combined: string
constructor(
code: number | null,
signal: NodeJS.Signals | null,
stdout: string,
stderr: string,
combined: string,
message: string
) {
super(message)
this._code = code
this._signal = signal
this._stdout = stdout
this._stderr = stderr
this._combined = combined
}
toString() {
return this._combined
}
get stdout() {
return this._stdout
}
get stderr() {
return this._stderr
}
get exitCode() {
return this._code
}
get signal() {
return this._signal
}
[inspect.custom]() {
let stringify = (s: string, c: ChalkInstance) =>
s.length === 0 ? "''" : c(inspect(s))
return `ProcessOutput {
stdout: ${stringify(this.stdout, chalk.green)},
stderr: ${stringify(this.stderr, chalk.red)},
signal: ${inspect(this.signal)},
exitCode: ${(this.exitCode === 0 ? chalk.green : chalk.red)(this.exitCode)}${
exitCodeInfo(this.exitCode)
? chalk.grey(' (' + exitCodeInfo(this.exitCode) + ')')
: ''
}
}`
}
}
export function within<R>(callback: () => R): R {
return storage.run({ ...getStore() }, callback)
}
function syncCwd() {
if ($[processCwd] != process.cwd()) process.chdir($[processCwd])
}
export function cd(dir: string) {
$.log({ kind: 'cd', dir })
process.chdir(dir)
$[processCwd] = process.cwd()
}
export type LogEntry =
| {
kind: 'cmd'
verbose: boolean
cmd: string
}
| {
kind: 'stdout' | 'stderr'
verbose: boolean
data: Buffer
}
| {
kind: 'cd'
dir: string
}
| {
kind: 'fetch'
url: RequestInfo
init?: RequestInit
}
| {
kind: 'retry'
error: string
}
| {
kind: 'custom'
data: any
}
export function log(entry: LogEntry) {
switch (entry.kind) {
case 'cmd':
if (!entry.verbose) return
process.stderr.write(formatCmd(entry.cmd))
break
case 'stdout':
case 'stderr':
if (!entry.verbose) return
process.stderr.write(entry.data)
break
case 'cd':
if (!$.verbose) return
process.stderr.write('$ ' + chalk.greenBright('cd') + ` ${entry.dir}\n`)
break
case 'fetch':
if (!$.verbose) return
const init = entry.init ? ' ' + inspect(entry.init) : ''
process.stderr.write(
'$ ' + chalk.greenBright('fetch') + ` ${entry.url}${init}\n`
)
break
case 'retry':
if (!$.verbose) return
process.stderr.write(entry.error + '\n')
}
}