@@ -7,6 +7,7 @@ const pkgJson = require('@npmcli/package-json')
7
7
const { defaults, definitions } = require ( '@npmcli/config/lib/definitions' )
8
8
const { log, output } = require ( 'proc-log' )
9
9
const BaseCommand = require ( '../base-cmd.js' )
10
+ const { redact } = require ( '@npmcli/redact' )
10
11
11
12
// These are the configs that we can nerf-dart. Not all of them currently even
12
13
// *have* config definitions so we have to explicitly validate them here.
@@ -53,29 +54,35 @@ const keyValues = args => {
53
54
return kv
54
55
}
55
56
56
- const publicVar = k => {
57
+ const isProtected = ( k ) => {
57
58
// _password
58
59
if ( k . startsWith ( '_' ) ) {
59
- return false
60
+ return true
60
61
}
61
62
if ( protected . includes ( k ) ) {
62
- return false
63
+ return true
63
64
}
64
65
// //localhost:8080/:_password
65
66
if ( k . startsWith ( '//' ) ) {
66
67
if ( k . includes ( ':_' ) ) {
67
- return false
68
+ return true
68
69
}
69
70
// //registry:_authToken or //registry:authToken
70
71
for ( const p of protected ) {
71
72
if ( k . endsWith ( `:${ p } ` ) || k . endsWith ( `:_${ p } ` ) ) {
72
- return false
73
+ return true
73
74
}
74
75
}
75
76
}
76
- return true
77
+ return false
77
78
}
78
79
80
+ // Private fields are either protected or they can redacted info
81
+ const isPrivate = ( k , v ) => isProtected ( k ) || redact ( v ) !== v
82
+
83
+ const displayVar = ( k , v ) =>
84
+ `${ k } = ${ isProtected ( k , v ) ? '(protected)' : JSON . stringify ( redact ( v ) ) } `
85
+
79
86
class Config extends BaseCommand {
80
87
static description = 'Manage the npm configuration files'
81
88
static name = 'config'
@@ -206,12 +213,13 @@ class Config extends BaseCommand {
206
213
207
214
const out = [ ]
208
215
for ( const key of keys ) {
209
- if ( ! publicVar ( key ) ) {
216
+ const val = this . npm . config . get ( key )
217
+ if ( isPrivate ( key , val ) ) {
210
218
throw new Error ( `The ${ key } option is protected, and can not be retrieved in this way` )
211
219
}
212
220
213
221
const pref = keys . length > 1 ? `${ key } =` : ''
214
- out . push ( pref + this . npm . config . get ( key ) )
222
+ out . push ( pref + val )
215
223
}
216
224
output . standard ( out . join ( '\n' ) )
217
225
}
@@ -338,18 +346,17 @@ ${defData}
338
346
continue
339
347
}
340
348
341
- const keys = Object . keys ( data ) . sort ( localeCompare )
342
- if ( ! keys . length ) {
349
+ const entries = Object . entries ( data ) . sort ( ( [ a ] , [ b ] ) => localeCompare ( a , b ) )
350
+ if ( ! entries . length ) {
343
351
continue
344
352
}
345
353
346
354
msg . push ( `; "${ where } " config from ${ source } ` , '' )
347
- for ( const k of keys ) {
348
- const v = publicVar ( k ) ? JSON . stringify ( data [ k ] ) : '(protected)'
355
+ for ( const [ k , v ] of entries ) {
356
+ const display = displayVar ( k , v )
349
357
const src = this . npm . config . find ( k )
350
- const overridden = src !== where
351
- msg . push ( ( overridden ? '; ' : '' ) +
352
- `${ k } = ${ v } ${ overridden ? ` ; overridden by ${ src } ` : '' } ` )
358
+ msg . push ( src === where ? display : `; ${ display } ; overridden by ${ src } ` )
359
+ msg . push ( )
353
360
}
354
361
msg . push ( '' )
355
362
}
@@ -374,10 +381,10 @@ ${defData}
374
381
const pkgPath = resolve ( this . npm . prefix , 'package.json' )
375
382
msg . push ( `; "publishConfig" from ${ pkgPath } ` )
376
383
msg . push ( '; This set of config values will be used at publish-time.' , '' )
377
- const pkgKeys = Object . keys ( content . publishConfig ) . sort ( localeCompare )
378
- for ( const k of pkgKeys ) {
379
- const v = publicVar ( k ) ? JSON . stringify ( content . publishConfig [ k ] ) : '(protected)'
380
- msg . push ( ` ${ k } = ${ v } ` )
384
+ const entries = Object . entries ( content . publishConfig )
385
+ . sort ( ( [ a ] , [ b ] ) => localeCompare ( a , b ) )
386
+ for ( const [ k , value ] of entries ) {
387
+ msg . push ( displayVar ( k , value ) )
381
388
}
382
389
msg . push ( '' )
383
390
}
@@ -389,11 +396,12 @@ ${defData}
389
396
async listJson ( ) {
390
397
const publicConf = { }
391
398
for ( const key in this . npm . config . list [ 0 ] ) {
392
- if ( ! publicVar ( key ) ) {
399
+ const value = this . npm . config . get ( key )
400
+ if ( isPrivate ( key , value ) ) {
393
401
continue
394
402
}
395
403
396
- publicConf [ key ] = this . npm . config . get ( key )
404
+ publicConf [ key ] = value
397
405
}
398
406
output . standard ( JSON . stringify ( publicConf , null , 2 ) )
399
407
}
0 commit comments