-
-
Notifications
You must be signed in to change notification settings - Fork 936
/
reportMisc.ts
88 lines (84 loc) · 2.92 KB
/
reportMisc.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
import os from 'os'
import { Config } from '@pnpm/config'
import { Log, RegistryLog } from '@pnpm/core-loggers'
import { LogLevel } from '@pnpm/logger'
import * as Rx from 'rxjs'
import { filter, map } from 'rxjs/operators'
import reportError from '../reportError'
import formatWarn from './utils/formatWarn'
import { autozoom } from './utils/zooming'
// eslint-disable:object-literal-sort-keys
export const LOG_LEVEL_NUMBER: Record<LogLevel, number> = {
error: 0,
warn: 1,
info: 2,
debug: 3,
}
// eslint-enable:object-literal-sort-keys
const MAX_SHOWN_WARNINGS = 5
export default (
log$: {
registry: Rx.Observable<RegistryLog>
other: Rx.Observable<Log>
},
opts: {
appendOnly: boolean
cwd: string
logLevel?: LogLevel
config?: Config
zoomOutCurrent: boolean
}
) => {
const maxLogLevel = LOG_LEVEL_NUMBER[opts.logLevel ?? 'info'] ?? LOG_LEVEL_NUMBER['info']
const reportWarning = makeWarningReporter(opts)
return Rx.merge(log$.registry, log$.other).pipe(
filter((obj) => LOG_LEVEL_NUMBER[obj.level] <= maxLogLevel &&
(obj.level !== 'info' || !obj['prefix'] || obj['prefix'] === opts.cwd)),
map((obj) => {
switch (obj.level) {
case 'warn': {
return reportWarning(obj)
}
case 'error':
if (obj['prefix'] && obj['prefix'] !== opts.cwd) {
return Rx.of({
msg: `${obj['prefix'] as string}:` + os.EOL + reportError(obj, opts.config),
})
}
return Rx.of({ msg: reportError(obj, opts.config) })
default:
return Rx.of({ msg: obj['message'] })
}
})
)
}
// Sometimes, when installing new dependencies that rely on many peer dependencies,
// or when running installation on a huge monorepo, there will be hundreds or thousands of warnings.
// Printing many messages to the terminal is expensive and reduces speed,
// so pnpm will only print a few warnings and report the total number of the unprinted warnings.
function makeWarningReporter (
opts: {
appendOnly: boolean
cwd: string
zoomOutCurrent: boolean
}
) {
let warningsCounter = 0
let collapsedWarnings: Rx.Subject<{ msg: string }>
return (obj: { prefix: string, message: string }) => {
warningsCounter++
if (opts.appendOnly || warningsCounter <= MAX_SHOWN_WARNINGS) {
return Rx.of({ msg: autozoom(opts.cwd, obj.prefix, formatWarn(obj.message), opts) })
}
const warningMsg = formatWarn(`${warningsCounter - MAX_SHOWN_WARNINGS} other warnings`)
if (!collapsedWarnings) {
collapsedWarnings = new Rx.Subject()
// For some reason, without using setTimeout, the warning summary is printed above the rest of the warnings
// Even though the summary event happens last. Probably a bug in "most".
setTimeout(() => collapsedWarnings.next({ msg: warningMsg }), 0)
return Rx.from(collapsedWarnings)
}
setTimeout(() => collapsedWarnings!.next({ msg: warningMsg }), 0)
return Rx.NEVER
}
}