-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
executable file
·75 lines (60 loc) · 1.85 KB
/
index.js
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
#!/usr/bin/env node
'use strict'
const { readFileSync } = require('node:fs')
const { styleText } = require('node:util')
const path = require('node:path')
const COLORS = ['yellow', 'blue', 'magenta', 'cyan']
const getColor = index => COLORS[index % COLORS.length]
const prettyMs = ms => {
const seconds = ms / 1000
if (seconds < 60) return `${Math.round(seconds)}s`
const minutes = Math.round(seconds / 60)
const remainingSeconds = seconds % 60
return `${minutes}m ${remainingSeconds.toFixed(2)}s`
}
const toStream = stream => (data, task) => {
const lines = (task.buffer += data.toString()).split('\n')
if (data.length) task.buffer = lines.pop() || ''
for (const line of lines) {
stream.write(task.name + (line ? ` ${line}` : '') + '\n')
}
}
const { _, ...flags } = require('mri')(process.argv.slice(2))
if (_.length === 0) {
console.log(readFileSync(path.resolve(__dirname, './help.txt'), 'utf8'))
process.exit(0)
}
let names = flags.names?.split(',')
if (Array.isArray(names) && names.length > 0) {
const maxNameLength = Math.max(...names.map(name => name.length))
names = names.map(name => name.padStart(maxNameLength))
}
const tasks = _.map((cmd, index) => ({
cmd,
name: styleText(getColor(index), names?.[index] ?? `[${index}]`),
buffer: ''
}))
const stdout = toStream(process.stdout)
const start = (subprocess, task) =>
stdout(`${styleText('gray', `started pid=${subprocess.pid}`)}` + '\n', task)
const exit = ({ exitCode, signalCode, duration }, task) => {
const color = exitCode === 0 ? 'gray' : 'red'
console.log(
`${task.name} ${styleText(
color,
`cmd='${
task.cmd
}' exitCode=${exitCode} signalCode=${signalCode} duration=${prettyMs(
duration
)}`
)}`
)
}
require('tinyrun')({
tasks,
stdout,
stderr: toStream(process.stderr),
start,
exit,
childOpts: { shell: true }
})