@@ -7,7 +7,7 @@ const usage = require('./utils/npm-usage.js')
77const LogFile = require ( './utils/log-file.js' )
88const Timers = require ( './utils/timers.js' )
99const Display = require ( './utils/display.js' )
10- const { log, time } = require ( 'proc-log' )
10+ const { log, time, output } = require ( 'proc-log' )
1111const { redactLog : replaceInfo } = require ( '@npmcli/redact' )
1212const pkg = require ( '../package.json' )
1313const { deref } = require ( './utils/cmd-list.js' )
@@ -28,20 +28,14 @@ class Npm {
2828 }
2929
3030 updateNotification = null
31- loadErr = null
3231 argv = [ ]
3332
3433 #command = null
3534 #runId = new Date ( ) . toISOString ( ) . replace ( / [ . : ] / g, '_' )
36- #loadPromise = null
3735 #title = 'npm'
3836 #argvClean = [ ]
3937 #npmRoot = null
4038
41- #chalk = null
42- #logChalk = null
43- #noColorChalk = null
44-
4539 #display = null
4640 #logFile = new LogFile ( )
4741 #timers = new Timers ( { start : 'npm' } )
@@ -107,13 +101,7 @@ class Npm {
107101 }
108102
109103 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 } ) )
117105 }
118106
119107 get loaded ( ) {
@@ -160,19 +148,24 @@ class Npm {
160148
161149 await time . start ( 'npm:load:configload' , ( ) => this . config . load ( ) )
162150
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+ }
176169
177170 // mkdir this separately since the logs dir can be set to
178171 // a different location. if this fails, then we don't have
@@ -208,49 +201,29 @@ class Npm {
208201 log . verbose ( 'argv' , this . #argvClean. map ( JSON . stringify ) . join ( ' ' ) )
209202 } )
210203
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' ) ,
228207 } )
229208
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 ,
236211 } )
237212
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+ }
250217
251218 if ( this . config . get ( 'force' ) ) {
252219 log . warn ( 'using --force' , 'Recommended protections disabled.' )
253220 }
221+
222+ // npm --versions
223+ if ( this . config . get ( 'versions' , 'cli' ) ) {
224+ this . argv = [ 'version' ]
225+ this . config . set ( 'usage' , false , 'cli' )
226+ }
254227 }
255228
256229 get isShellout ( ) {
@@ -283,15 +256,15 @@ class Npm {
283256 }
284257
285258 get noColorChalk ( ) {
286- return this . #noColorChalk
259+ return this . #display . chalk . noColor
287260 }
288261
289262 get chalk ( ) {
290- return this . #chalk
263+ return this . #display . chalk . stdout
291264 }
292265
293266 get logChalk ( ) {
294- return this . #logChalk
267+ return this . #display . chalk . stderr
295268 }
296269
297270 get global ( ) {
0 commit comments