/
reportLifecycleScripts.ts
286 lines (262 loc) · 7.99 KB
/
reportLifecycleScripts.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
import path from 'path'
import { LifecycleLog } from '@pnpm/core-loggers'
import * as Rx from 'rxjs'
import { buffer, filter, groupBy, map, mergeAll, mergeMap } from 'rxjs/operators'
import chalk from 'chalk'
import prettyTime from 'pretty-ms'
import stripAnsi from 'strip-ansi'
import { EOL } from '../constants'
import { formatPrefix, formatPrefixNoTrim } from './utils/formatPrefix'
import { hlValue } from './outputConstants'
const NODE_MODULES = `${path.sep}node_modules${path.sep}`
// When streaming processes are spawned, use this color for prefix
const colorWheel = ['cyan', 'magenta', 'blue', 'yellow', 'green', 'red']
const NUM_COLORS = colorWheel.length
// Ever-increasing index ensures colors are always sequential
let currentColor = 0
type ColorByPkg = Map<string, (txt: string) => string>
export function reportLifecycleScripts (
log$: {
lifecycle: Rx.Observable<LifecycleLog>
},
opts: {
appendOnly?: boolean
aggregateOutput?: boolean
cwd: string
width: number
}
) {
// When the reporter is not append-only, the length of output is limited
// in order to reduce flickering
if (opts.appendOnly) {
let lifecycle$ = log$.lifecycle
if (opts.aggregateOutput) {
lifecycle$ = lifecycle$.pipe(aggregateOutput)
}
const streamLifecycleOutput = createStreamLifecycleOutput(opts.cwd)
return lifecycle$.pipe(
map((log: LifecycleLog) => Rx.of({
msg: streamLifecycleOutput(log),
}))
)
}
const lifecycleMessages: {
[depPath: string]: {
collapsed: boolean
output: string[]
script: string
startTime: [number, number]
status: string
}
} = {}
const lifecycleStreamByDepPath: {
[depPath: string]: Rx.Subject<{ msg: string }>
} = {}
const lifecyclePushStream = new Rx.Subject<Rx.Observable<{ msg: string }>>()
// TODO: handle promise of .forEach?!
log$.lifecycle // eslint-disable-line
.forEach((log: LifecycleLog) => {
const key = `${log.stage}:${log.depPath}`
lifecycleMessages[key] = lifecycleMessages[key] || {
collapsed: log.wd.includes(NODE_MODULES),
output: [],
startTime: process.hrtime(),
status: formatIndentedStatus(chalk.magentaBright('Running...')),
}
const exit = typeof log['exitCode'] === 'number'
let msg: string
if (lifecycleMessages[key].collapsed) {
msg = renderCollapsedScriptOutput(log, lifecycleMessages[key], { cwd: opts.cwd, exit, maxWidth: opts.width })
} else {
msg = renderScriptOutput(log, lifecycleMessages[key], { cwd: opts.cwd, exit, maxWidth: opts.width })
}
if (exit) {
delete lifecycleMessages[key]
}
if (!lifecycleStreamByDepPath[key]) {
lifecycleStreamByDepPath[key] = new Rx.Subject<{ msg: string }>()
lifecyclePushStream.next(Rx.from(lifecycleStreamByDepPath[key]))
}
lifecycleStreamByDepPath[key].next({ msg })
if (exit) {
lifecycleStreamByDepPath[key].complete()
}
})
return Rx.from(lifecyclePushStream)
}
function toNano (time: [number, number]) {
return (time[0] + (time[1] / 1e9)) * 1e3
}
function renderCollapsedScriptOutput (
log: LifecycleLog,
messageCache: {
collapsed: boolean
label?: string
output: string[]
script: string
startTime: [number, number]
status: string
},
opts: {
cwd: string
exit: boolean
maxWidth: number
}
) {
messageCache.label = messageCache.label ??
`${highlightLastFolder(formatPrefixNoTrim(opts.cwd, log.wd))}: Running ${log.stage} script`
if (!opts.exit) {
updateMessageCache(log, messageCache, opts)
return `${messageCache.label}...`
}
const time = prettyTime(toNano(process.hrtime(messageCache.startTime)))
if (log['exitCode'] === 0) {
return `${messageCache.label}, done in ${time}`
}
if (log['optional'] === true) {
return `${messageCache.label}, failed in ${time} (skipped as optional)`
}
return `${messageCache.label}, failed in ${time}${EOL}${renderScriptOutput(log, messageCache, opts)}`
}
function renderScriptOutput (
log: LifecycleLog,
messageCache: {
collapsed: boolean
output: string[]
script: string
startTime: [number, number]
status: string
},
opts: {
cwd: string
exit: boolean
maxWidth: number
}
) {
updateMessageCache(log, messageCache, opts)
if (opts.exit && log['exitCode'] !== 0) {
return [
messageCache.script,
...messageCache.output,
messageCache.status,
].join(EOL)
}
if (messageCache.output.length > 10) {
return [
messageCache.script,
`[${messageCache.output.length - 10} lines collapsed]`,
...messageCache.output.slice(messageCache.output.length - 10),
messageCache.status,
].join(EOL)
}
return [
messageCache.script,
...messageCache.output,
messageCache.status,
].join(EOL)
}
function updateMessageCache (
log: LifecycleLog,
messageCache: {
collapsed: boolean
output: string[]
script: string
startTime: [number, number]
status: string
},
opts: {
cwd: string
exit: boolean
maxWidth: number
}
) {
if (log['script']) {
const prefix = `${formatPrefix(opts.cwd, log.wd)} ${hlValue(log.stage)}`
const maxLineWidth = opts.maxWidth - prefix.length - 2 + ANSI_ESCAPES_LENGTH_OF_PREFIX
messageCache.script = `${prefix}$ ${cutLine(log['script'], maxLineWidth)}`
} else if (opts.exit) {
const time = prettyTime(toNano(process.hrtime(messageCache.startTime)))
if (log['exitCode'] === 0) {
messageCache.status = formatIndentedStatus(chalk.magentaBright(`Done in ${time}`))
} else {
messageCache.status = formatIndentedStatus(chalk.red(`Failed in ${time}`))
}
} else {
messageCache.output.push(formatIndentedOutput(opts.maxWidth, log))
}
}
function formatIndentedStatus (status: string) {
return `${chalk.magentaBright('└─')} ${status}`
}
function highlightLastFolder (p: string) {
const lastSlash = p.lastIndexOf('/') + 1
return `${chalk.gray(p.slice(0, lastSlash))}${p.slice(lastSlash)}`
}
const ANSI_ESCAPES_LENGTH_OF_PREFIX = hlValue(' ').length - 1
function createStreamLifecycleOutput (cwd: string) {
currentColor = 0
const colorByPrefix: ColorByPkg = new Map()
return streamLifecycleOutput.bind(null, colorByPrefix, cwd)
}
function streamLifecycleOutput (
colorByPkg: ColorByPkg,
cwd: string,
logObj: LifecycleLog
) {
const prefix = formatLifecycleScriptPrefix(colorByPkg, cwd, logObj.wd, logObj.stage)
if (typeof logObj['exitCode'] === 'number') {
if (logObj['exitCode'] === 0) {
return `${prefix}: Done`
} else {
return `${prefix}: Failed`
}
}
if (logObj['script']) {
return `${prefix}$ ${logObj['script'] as string}`
}
const line = formatLine(Infinity, logObj)
return `${prefix}: ${line}`
}
function formatIndentedOutput (maxWidth: number, logObj: LifecycleLog) {
return `${chalk.magentaBright('│')} ${formatLine(maxWidth - 2, logObj)}`
}
function formatLifecycleScriptPrefix (
colorByPkg: ColorByPkg,
cwd: string,
wd: string,
stage: string
) {
if (!colorByPkg.has(wd)) {
const colorName = colorWheel[currentColor % NUM_COLORS]
colorByPkg.set(wd, chalk[colorName])
currentColor += 1
}
const color = colorByPkg.get(wd)!
return `${color(formatPrefix(cwd, wd))} ${hlValue(stage)}`
}
function formatLine (maxWidth: number, logObj: LifecycleLog) {
const line = cutLine(logObj['line'], maxWidth)
// TODO: strip only the non-color/style ansi escape codes
if (logObj['stdio'] === 'stderr') {
return chalk.gray(line)
}
return line
}
function cutLine (line: string, maxLength: number) {
if (!line) return '' // This actually should never happen but it is better to be safe
return stripAnsi(line).slice(0, maxLength)
}
function aggregateOutput (source: Rx.Observable<LifecycleLog>) {
return source.pipe(
groupBy(data => data.depPath),
mergeMap(group => {
return group.pipe(
buffer(
group.pipe(filter(msg => 'exitCode' in msg))
)
)
}),
map(ar => Rx.from(ar)),
mergeAll()
)
}