@@ -7,7 +7,7 @@ const usage = require('./utils/npm-usage.js')
7
7
const LogFile = require ( './utils/log-file.js' )
8
8
const Timers = require ( './utils/timers.js' )
9
9
const Display = require ( './utils/display.js' )
10
- const { log, time } = require ( 'proc-log' )
10
+ const { log, time, output } = require ( 'proc-log' )
11
11
const { redactLog : replaceInfo } = require ( '@npmcli/redact' )
12
12
const pkg = require ( '../package.json' )
13
13
const { deref } = require ( './utils/cmd-list.js' )
@@ -28,20 +28,14 @@ class Npm {
28
28
}
29
29
30
30
updateNotification = null
31
- loadErr = null
32
31
argv = [ ]
33
32
34
33
#command = null
35
34
#runId = new Date ( ) . toISOString ( ) . replace ( / [ . : ] / g, '_' )
36
- #loadPromise = null
37
35
#title = 'npm'
38
36
#argvClean = [ ]
39
37
#npmRoot = null
40
38
41
- #chalk = null
42
- #logChalk = null
43
- #noColorChalk = null
44
-
45
39
#display = null
46
40
#logFile = new LogFile ( )
47
41
#timers = new Timers ( { start : 'npm' } )
@@ -107,13 +101,7 @@ class Npm {
107
101
}
108
102
109
103
async load ( ) {
110
- if ( ! this . #loadPromise) {
111
- this . #loadPromise = time . start ( 'npm:load' , ( ) => this . #load( ) . catch ( ( er ) => {
112
- this . loadErr = er
113
- throw er
114
- } ) )
115
- }
116
- return this . #loadPromise
104
+ return time . start ( 'npm:load' , ( ) => this . #load( ) . then ( r => r || { exec : true } ) )
117
105
}
118
106
119
107
get loaded ( ) {
@@ -160,19 +148,24 @@ class Npm {
160
148
161
149
await time . start ( 'npm:load:configload' , ( ) => this . config . load ( ) )
162
150
163
- // get createSupportsColor from chalk directly if this lands
164
- // https://github.com/chalk/chalk/pull/600
165
- const [ { Chalk } , { createSupportsColor } ] = await Promise . all ( [
166
- import ( 'chalk' ) ,
167
- import ( 'supports-color' ) ,
168
- ] )
169
- this . #noColorChalk = new Chalk ( { level : 0 } )
170
- // we get the chalk level based on a null stream meaning chalk will only use
171
- // what it knows about the environment to get color support since we already
172
- // determined in our definitions that we want to show colors.
173
- const level = Math . max ( createSupportsColor ( null ) . level , 1 )
174
- this . #chalk = this . color ? new Chalk ( { level } ) : this . #noColorChalk
175
- this . #logChalk = this . logColor ? new Chalk ( { level } ) : this . #noColorChalk
151
+ await this . #display. load ( {
152
+ loglevel : this . config . get ( 'loglevel' ) ,
153
+ stdoutColor : this . color ,
154
+ stderrColor : this . logColor ,
155
+ timing : this . config . get ( 'timing' ) ,
156
+ unicode : this . config . get ( 'unicode' ) ,
157
+ progress : this . flatOptions . progress ,
158
+ json : this . config . get ( 'json' ) ,
159
+ heading : this . config . get ( 'heading' ) ,
160
+ } )
161
+ process . env . COLOR = this . color ? '1' : '0'
162
+
163
+ // npm -v
164
+ // return from here early so we dont create any caches/logfiles/timers etc
165
+ if ( this . config . get ( 'version' , 'cli' ) ) {
166
+ output . standard ( this . version )
167
+ return { exec : false }
168
+ }
176
169
177
170
// mkdir this separately since the logs dir can be set to
178
171
// a different location. if this fails, then we don't have
@@ -208,49 +201,29 @@ class Npm {
208
201
log . verbose ( 'argv' , this . #argvClean. map ( JSON . stringify ) . join ( ' ' ) )
209
202
} )
210
203
211
- time . start ( 'npm:load:display' , ( ) => {
212
- this . #display. load ( {
213
- loglevel : this . config . get ( 'loglevel' ) ,
214
- // TODO: only pass in logColor and color and create chalk instances
215
- // in display load method. Then remove chalk getters from npm and
216
- // producers should emit chalk-templates (or something else).
217
- stdoutChalk : this . #chalk,
218
- stdoutColor : this . color ,
219
- stderrChalk : this . #logChalk,
220
- stderrColor : this . logColor ,
221
- timing : this . config . get ( 'timing' ) ,
222
- unicode : this . config . get ( 'unicode' ) ,
223
- progress : this . flatOptions . progress ,
224
- json : this . config . get ( 'json' ) ,
225
- heading : this . config . get ( 'heading' ) ,
226
- } )
227
- process . env . COLOR = this . color ? '1' : '0'
204
+ this . #logFile. load ( {
205
+ path : this . logPath ,
206
+ logsMax : this . config . get ( 'logs-max' ) ,
228
207
} )
229
208
230
- time . start ( 'npm:load:logFile' , ( ) => {
231
- this . #logFile. load ( {
232
- path : this . logPath ,
233
- logsMax : this . config . get ( 'logs-max' ) ,
234
- } )
235
- log . verbose ( 'logfile' , this . #logFile. files [ 0 ] || 'no logfile created' )
209
+ this . #timers. load ( {
210
+ path : this . config . get ( 'timing' ) ? this . logPath : null ,
236
211
} )
237
212
238
- time . start ( 'npm:load:timers' , ( ) =>
239
- this . #timers. load ( {
240
- path : this . config . get ( 'timing' ) ? this . logPath : null ,
241
- } )
242
- )
243
-
244
- time . start ( 'npm:load:configScope' , ( ) => {
245
- const configScope = this . config . get ( 'scope' )
246
- if ( configScope && ! / ^ @ / . test ( configScope ) ) {
247
- this . config . set ( 'scope' , `@${ configScope } ` , this . config . find ( 'scope' ) )
248
- }
249
- } )
213
+ const configScope = this . config . get ( 'scope' )
214
+ if ( configScope && ! / ^ @ / . test ( configScope ) ) {
215
+ this . config . set ( 'scope' , `@${ configScope } ` , this . config . find ( 'scope' ) )
216
+ }
250
217
251
218
if ( this . config . get ( 'force' ) ) {
252
219
log . warn ( 'using --force' , 'Recommended protections disabled.' )
253
220
}
221
+
222
+ // npm --versions
223
+ if ( this . config . get ( 'versions' , 'cli' ) ) {
224
+ this . argv = [ 'version' ]
225
+ this . config . set ( 'usage' , false , 'cli' )
226
+ }
254
227
}
255
228
256
229
get isShellout ( ) {
@@ -283,15 +256,15 @@ class Npm {
283
256
}
284
257
285
258
get noColorChalk ( ) {
286
- return this . #noColorChalk
259
+ return this . #display . chalk . noColor
287
260
}
288
261
289
262
get chalk ( ) {
290
- return this . #chalk
263
+ return this . #display . chalk . stdout
291
264
}
292
265
293
266
get logChalk ( ) {
294
- return this . #logChalk
267
+ return this . #display . chalk . stderr
295
268
}
296
269
297
270
get global ( ) {
0 commit comments