-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Expand file tree
/
Copy pathdisplay.js
More file actions
215 lines (193 loc) · 6.48 KB
/
display.js
File metadata and controls
215 lines (193 loc) · 6.48 KB
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
const { inspect } = require('util')
const npmlog = require('npmlog')
const log = require('./log-shim.js')
const { explain } = require('./explain-eresolve.js')
const originalCustomInspect = Symbol('npm.display.original.util.inspect.custom')
// These are most assuredly not a mistake
// https://eslint.org/docs/latest/rules/no-control-regex
/* eslint-disable no-control-regex */
// \x00 through \x1f, \x7f through \x9f, not including \x09 \x0a \x0b \x0d
const hasC01 = /[\x00-\x08\x0c\x0e-\x1f\x7f-\x9f]/
// Allows everything up to '[38;5;255m' in 8 bit notation
const allowedSGR = /^\[[0-9;]{0,8}m/
// '[38;5;255m'.length
const sgrMaxLen = 10
// Strips all ANSI C0 and C1 control characters (except for SGR up to 8 bit)
function stripC01 (str) {
if (!hasC01.test(str)) {
return str
}
let result = ''
for (let i = 0; i < str.length; i++) {
const char = str[i]
const code = char.charCodeAt(0)
if (!hasC01.test(char)) {
// Most characters are in this set so continue early if we can
result = `${result}${char}`
} else if (code === 27 && allowedSGR.test(str.slice(i + 1, i + sgrMaxLen + 1))) {
// \x1b with allowed SGR
result = `${result}\x1b`
} else if (code <= 31) {
// escape all other C0 control characters besides \x7f
result = `${result}^${String.fromCharCode(code + 64)}`
} else {
// hasC01 ensures this is now a C1 control character or \x7f
result = `${result}^${String.fromCharCode(code - 64)}`
}
}
return result
}
class Display {
#chalk = null
constructor () {
// pause by default until config is loaded
this.on()
log.pause()
}
static clean (output) {
if (typeof output === 'string') {
// Strings are cleaned inline
return stripC01(output)
}
if (!output || typeof output !== 'object') {
// Numbers, booleans, null all end up here and don't need cleaning
return output
}
// output && typeof output === 'object'
// We can't use hasOwn et al for detecting the original but we can use it
// for detecting the properties we set via defineProperty
if (
output[inspect.custom] &&
(!Object.hasOwn(output, originalCustomInspect))
) {
// Save the old one if we didn't already do it.
Object.defineProperty(output, originalCustomInspect, {
value: output[inspect.custom],
writable: true,
})
}
if (!Object.hasOwn(output, originalCustomInspect)) {
// Put a dummy one in for when we run multiple times on the same object
Object.defineProperty(output, originalCustomInspect, {
value: function () {
return this
},
writable: true,
})
}
// Set the custom inspect to our own function
Object.defineProperty(output, inspect.custom, {
value: function () {
const toClean = this[originalCustomInspect]()
// Custom inspect can return things other than objects, check type again
if (typeof toClean === 'string') {
// Strings are cleaned inline
return stripC01(toClean)
}
if (!toClean || typeof toClean !== 'object') {
// Numbers, booleans, null all end up here and don't need cleaning
return toClean
}
return stripC01(inspect(toClean, { customInspect: false }))
},
writable: true,
})
return output
}
on () {
process.on('log', this.#logHandler)
}
off () {
process.off('log', this.#logHandler)
// Unbalanced calls to enable/disable progress
// will leave change listeners on the tracker
// This pretty much only happens in tests but
// this removes the event emitter listener warnings
log.tracker.removeAllListeners()
}
load (config) {
const {
color,
chalk,
timing,
loglevel,
unicode,
progress,
silent,
heading = 'npm',
} = config
this.#chalk = chalk
// npmlog is still going away someday, so this is a hack to dynamically
// set the loglevel of timing based on the timing flag, instead of making
// a breaking change to npmlog. The result is that timing logs are never
// shown except when the --timing flag is set. We also need to change
// the index of the silly level since otherwise it is set to -Infinity
// and we can't go any lower than that. silent is still set to Infinify
// because we DO want silent to hide timing levels. This allows for the
// special case of getting timing information while hiding all CLI output
// in order to get perf information that might be affected by writing to
// a terminal. XXX(npmlog): this will be removed along with npmlog
log.levels.silly = -10000
log.levels.timing = log.levels[loglevel] + (timing ? 1 : -1)
log.level = loglevel
log.heading = heading
if (color) {
log.enableColor()
} else {
log.disableColor()
}
if (unicode) {
log.enableUnicode()
} else {
log.disableUnicode()
}
// if it's silent, don't show progress
if (progress && !silent) {
log.enableProgress()
} else {
log.disableProgress()
}
// Resume displaying logs now that we have config
log.resume()
}
log (...args) {
this.#logHandler(...args)
}
#logHandler = (level, ...args) => {
try {
this.#log(level, ...args)
} catch (ex) {
try {
// if it crashed once, it might again!
this.#npmlog('verbose', `attempt to log ${inspect(args)} crashed`, ex)
} catch (ex2) {
// eslint-disable-next-line no-console
console.error(`attempt to log ${inspect(args)} crashed`, ex, ex2)
}
}
}
#log (...args) {
return this.#eresolveWarn(...args) || this.#npmlog(...args)
}
// Explicitly call these on npmlog and not log shim
// This is the final place we should call npmlog before removing it.
#npmlog (level, ...args) {
npmlog[level](...args.map(Display.clean))
}
// Also (and this is a really inexcusable kludge), we patch the
// log.warn() method so that when we see a peerDep override
// explanation from Arborist, we can replace the object with a
// highly abbreviated explanation of what's being overridden.
#eresolveWarn (level, heading, message, expl) {
if (level === 'warn' &&
heading === 'ERESOLVE' &&
expl && typeof expl === 'object'
) {
this.#npmlog(level, heading, message)
this.#npmlog(level, '', explain(expl, this.#chalk, 2))
// Return true to short circuit other log in chain
return true
}
}
}
module.exports = Display