@@ -72,16 +72,12 @@ const confTypes = new Set([
72
72
'cli' ,
73
73
] )
74
74
75
- const _loaded = Symbol ( 'loaded' )
76
- const _get = Symbol ( 'get' )
77
- const _find = Symbol ( 'find' )
78
- const _loadObject = Symbol ( 'loadObject' )
79
- const _loadFile = Symbol ( 'loadFile' )
80
- const _checkDeprecated = Symbol ( 'checkDeprecated' )
81
- const _flatten = Symbol ( 'flatten' )
82
- const _flatOptions = Symbol ( 'flatOptions' )
83
-
84
75
class Config {
76
+ #loaded = false
77
+ #flatten
78
+ // populated the first time we flatten the object
79
+ #flatOptions = null
80
+
85
81
static get typeDefs ( ) {
86
82
return typeDefs
87
83
}
@@ -113,9 +109,7 @@ class Config {
113
109
}
114
110
}
115
111
116
- // populated the first time we flatten the object
117
- this [ _flatOptions ] = null
118
- this [ _flatten ] = flatten
112
+ this . #flatten = flatten
119
113
this . types = types
120
114
this . shorthands = shorthands
121
115
this . defaults = defaults
@@ -159,26 +153,27 @@ class Config {
159
153
}
160
154
Object . freeze ( this . list )
161
155
162
- this [ _loaded ] = false
156
+ this . #loaded = false
163
157
}
164
158
165
159
get loaded ( ) {
166
- return this [ _loaded ]
160
+ return this . #loaded
167
161
}
168
162
169
163
get prefix ( ) {
170
- return this [ _get ] ( 'global' ) ? this . globalPrefix : this . localPrefix
164
+ return this . #get ( 'global' ) ? this . globalPrefix : this . localPrefix
171
165
}
172
166
173
167
// return the location where key is found.
174
168
find ( key ) {
175
169
if ( ! this . loaded ) {
176
170
throw new Error ( 'call config.load() before reading values' )
177
171
}
178
- return this [ _find ] ( key )
172
+ // TODO single use?
173
+ return this . #find( key )
179
174
}
180
175
181
- [ _find ] ( key ) {
176
+ #find ( key ) {
182
177
// have to look in reverse order
183
178
const entries = [ ...this . data . entries ( ) ]
184
179
for ( let i = entries . length - 1 ; i > - 1 ; i -- ) {
@@ -194,12 +189,12 @@ class Config {
194
189
if ( ! this . loaded ) {
195
190
throw new Error ( 'call config.load() before reading values' )
196
191
}
197
- return this [ _get ] ( key , where )
192
+ return this . #get ( key , where )
198
193
}
199
194
200
195
// we need to get values sometimes, so use this internal one to do so
201
196
// while in the process of loading.
202
- [ _get ] ( key , where = null ) {
197
+ #get ( key , where = null ) {
203
198
if ( where !== null && ! confTypes . has ( where ) ) {
204
199
throw new Error ( 'invalid config location param: ' + where )
205
200
}
@@ -214,32 +209,32 @@ class Config {
214
209
if ( ! confTypes . has ( where ) ) {
215
210
throw new Error ( 'invalid config location param: ' + where )
216
211
}
217
- this [ _checkDeprecated ] ( key )
212
+ this . #checkDeprecated ( key )
218
213
const { data } = this . data . get ( where )
219
214
data [ key ] = val
220
215
221
216
// this is now dirty, the next call to this.valid will have to check it
222
217
this . data . get ( where ) [ _valid ] = null
223
218
224
219
// the flat options are invalidated, regenerate next time they're needed
225
- this [ _flatOptions ] = null
220
+ this . #flatOptions = null
226
221
}
227
222
228
223
get flat ( ) {
229
- if ( this [ _flatOptions ] ) {
230
- return this [ _flatOptions ]
224
+ if ( this . #flatOptions ) {
225
+ return this . #flatOptions
231
226
}
232
227
233
228
// create the object for flat options passed to deps
234
229
process . emit ( 'time' , 'config:load:flatten' )
235
- this [ _flatOptions ] = { }
230
+ this . #flatOptions = { }
236
231
// walk from least priority to highest
237
232
for ( const { data } of this . data . values ( ) ) {
238
- this [ _flatten ] ( data , this [ _flatOptions ] )
233
+ this . #flatten ( data , this . #flatOptions )
239
234
}
240
235
process . emit ( 'timeEnd' , 'config:load:flatten' )
241
236
242
- return this [ _flatOptions ]
237
+ return this . #flatOptions
243
238
}
244
239
245
240
delete ( key , where = 'cli' ) {
@@ -290,8 +285,8 @@ class Config {
290
285
process . emit ( 'timeEnd' , 'config:load:global' )
291
286
292
287
// set this before calling setEnvs, so that we don't have to share
293
- // symbols , as that module also does a bunch of get operations
294
- this [ _loaded ] = true
288
+ // private attributes , as that module also does a bunch of get operations
289
+ this . #loaded = true
295
290
296
291
// set proper globalPrefix now that everything is loaded
297
292
this . globalPrefix = this . get ( 'prefix' )
@@ -307,7 +302,7 @@ class Config {
307
302
this . loadGlobalPrefix ( )
308
303
this . loadHome ( )
309
304
310
- this [ _loadObject ] ( {
305
+ this . #loadObject ( {
311
306
...this . defaults ,
312
307
prefix : this . globalPrefix ,
313
308
} , 'default' , 'default values' )
@@ -316,13 +311,13 @@ class Config {
316
311
317
312
// the metrics-registry defaults to the current resolved value of
318
313
// the registry, unless overridden somewhere else.
319
- settableGetter ( data , 'metrics-registry' , ( ) => this [ _get ] ( 'registry' ) )
314
+ settableGetter ( data , 'metrics-registry' , ( ) => this . #get ( 'registry' ) )
320
315
321
316
// if the prefix is set on cli, env, or userconfig, then we need to
322
317
// default the globalconfig file to that location, instead of the default
323
318
// global prefix. It's weird that `npm get globalconfig --prefix=/foo`
324
319
// returns `/foo/etc/npmrc`, but better to not change it at this point.
325
- settableGetter ( data , 'globalconfig' , ( ) => resolve ( this [ _get ] ( 'prefix' ) , 'etc/npmrc' ) )
320
+ settableGetter ( data , 'globalconfig' , ( ) => resolve ( this . #get ( 'prefix' ) , 'etc/npmrc' ) )
326
321
}
327
322
328
323
loadHome ( ) {
@@ -363,7 +358,7 @@ class Config {
363
358
}
364
359
conf [ key ] = envVal
365
360
}
366
- this [ _loadObject ] ( conf , 'env' , 'environment' )
361
+ this . #loadObject ( conf , 'env' , 'environment' )
367
362
}
368
363
369
364
loadCLI ( ) {
@@ -373,7 +368,7 @@ class Config {
373
368
nopt . invalidHandler = null
374
369
this . parsedArgv = conf . argv
375
370
delete conf . argv
376
- this [ _loadObject ] ( conf , 'cli' , 'command line options' )
371
+ this . #loadObject ( conf , 'cli' , 'command line options' )
377
372
}
378
373
379
374
get valid ( ) {
@@ -541,7 +536,7 @@ class Config {
541
536
log . warn ( 'invalid config' , msg , desc )
542
537
}
543
538
544
- [ _loadObject ] ( obj , where , source , er = null ) {
539
+ #loadObject ( obj , where , source , er = null ) {
545
540
const conf = this . data . get ( where )
546
541
if ( conf . source ) {
547
542
const m = `double-loading "${ where } " configs from ${ source } , ` +
@@ -568,14 +563,14 @@ class Config {
568
563
const k = envReplace ( key , this . env )
569
564
const v = this . parseField ( value , k )
570
565
if ( where !== 'default' ) {
571
- this [ _checkDeprecated ] ( k , where , obj , [ key , value ] )
566
+ this . #checkDeprecated ( k , where , obj , [ key , value ] )
572
567
}
573
568
conf . data [ k ] = v
574
569
}
575
570
}
576
571
}
577
572
578
- [ _checkDeprecated ] ( key , where , obj , kv ) {
573
+ #checkDeprecated ( key , where , obj , kv ) {
579
574
// XXX(npm9+) make this throw an error
580
575
if ( this . deprecated [ key ] ) {
581
576
log . warn ( 'config' , key , this . deprecated [ key ] )
@@ -587,18 +582,18 @@ class Config {
587
582
return parseField ( f , key , this , listElement )
588
583
}
589
584
590
- async [ _loadFile ] ( file , type ) {
585
+ async #loadFile ( file , type ) {
591
586
process . emit ( 'time' , 'config:load:file:' + file )
592
587
// only catch the error from readFile, not from the loadObject call
593
588
await readFile ( file , 'utf8' ) . then (
594
- data => this [ _loadObject ] ( ini . parse ( data ) , type , file ) ,
595
- er => this [ _loadObject ] ( null , type , file , er )
589
+ data => this . #loadObject ( ini . parse ( data ) , type , file ) ,
590
+ er => this . #loadObject ( null , type , file , er )
596
591
)
597
592
process . emit ( 'timeEnd' , 'config:load:file:' + file )
598
593
}
599
594
600
595
loadBuiltinConfig ( ) {
601
- return this [ _loadFile ] ( resolve ( this . npmPath , 'npmrc' ) , 'builtin' )
596
+ return this . #loadFile ( resolve ( this . npmPath , 'npmrc' ) , 'builtin' )
602
597
}
603
598
604
599
async loadProjectConfig ( ) {
@@ -613,7 +608,7 @@ class Config {
613
608
this . localPackage = await fileExists ( this . localPrefix , 'package.json' )
614
609
}
615
610
616
- if ( this [ _get ] ( 'global' ) === true || this [ _get ] ( 'location' ) === 'global' ) {
611
+ if ( this . #get ( 'global' ) === true || this . #get ( 'location' ) === 'global' ) {
617
612
this . data . get ( 'project' ) . source = '(global mode enabled, ignored)'
618
613
this . sources . set ( this . data . get ( 'project' ) . source , 'project' )
619
614
return
@@ -625,23 +620,23 @@ class Config {
625
620
// up loading the "project" config where the "userconfig" will be,
626
621
// which causes some calamaties. So, we only load project config if
627
622
// it doesn't match what the userconfig will be.
628
- if ( projectFile !== this [ _get ] ( 'userconfig' ) ) {
629
- return this [ _loadFile ] ( projectFile , 'project' )
623
+ if ( projectFile !== this . #get ( 'userconfig' ) ) {
624
+ return this . #loadFile ( projectFile , 'project' )
630
625
} else {
631
626
this . data . get ( 'project' ) . source = '(same as "user" config, ignored)'
632
627
this . sources . set ( this . data . get ( 'project' ) . source , 'project' )
633
628
}
634
629
}
635
630
636
631
async loadLocalPrefix ( ) {
637
- const cliPrefix = this [ _get ] ( 'prefix' , 'cli' )
632
+ const cliPrefix = this . #get ( 'prefix' , 'cli' )
638
633
if ( cliPrefix ) {
639
634
this . localPrefix = cliPrefix
640
635
return
641
636
}
642
637
643
- const cliWorkspaces = this [ _get ] ( 'workspaces' , 'cli' )
644
- const isGlobal = this [ _get ] ( 'global' ) || this [ _get ] ( 'location' ) === 'global'
638
+ const cliWorkspaces = this . #get ( 'workspaces' , 'cli' )
639
+ const isGlobal = this . #get ( 'global' ) || this . #get ( 'location' ) === 'global'
645
640
646
641
for ( const p of walkUp ( this . cwd ) ) {
647
642
// HACK: this is an option set in tests to stop the local prefix from being set
@@ -701,11 +696,11 @@ class Config {
701
696
}
702
697
703
698
loadUserConfig ( ) {
704
- return this [ _loadFile ] ( this [ _get ] ( 'userconfig' ) , 'user' )
699
+ return this . #loadFile ( this . #get ( 'userconfig' ) , 'user' )
705
700
}
706
701
707
702
loadGlobalConfig ( ) {
708
- return this [ _loadFile ] ( this [ _get ] ( 'globalconfig' ) , 'global' )
703
+ return this . #loadFile ( this . #get ( 'globalconfig' ) , 'global' )
709
704
}
710
705
711
706
async save ( where ) {
@@ -717,7 +712,6 @@ class Config {
717
712
}
718
713
719
714
const conf = this . data . get ( where )
720
- conf [ _raw ] = { ...conf . data }
721
715
conf [ _loadError ] = null
722
716
723
717
if ( where === 'user' ) {
@@ -870,41 +864,40 @@ class Config {
870
864
}
871
865
}
872
866
873
- const _data = Symbol ( 'data' )
874
- const _raw = Symbol ( 'raw' )
875
867
const _loadError = Symbol ( 'loadError' )
876
- const _source = Symbol ( 'source' )
877
868
const _valid = Symbol ( 'valid' )
869
+
878
870
class ConfigData {
871
+ #data
872
+ #source = null
873
+ #raw = null
879
874
constructor ( parent ) {
880
- this [ _data ] = Object . create ( parent && parent . data )
881
- this [ _source ] = null
882
- this [ _loadError ] = null
883
- this [ _raw ] = null
875
+ this . #data = Object . create ( parent && parent . data )
876
+ this . #raw = null
884
877
this [ _valid ] = true
885
878
}
886
879
887
880
get data ( ) {
888
- return this [ _data ]
881
+ return this . #data
889
882
}
890
883
891
884
get valid ( ) {
892
885
return this [ _valid ]
893
886
}
894
887
895
888
set source ( s ) {
896
- if ( this [ _source ] ) {
889
+ if ( this . #source ) {
897
890
throw new Error ( 'cannot set ConfigData source more than once' )
898
891
}
899
- this [ _source ] = s
892
+ this . #source = s
900
893
}
901
894
902
895
get source ( ) {
903
- return this [ _source ]
896
+ return this . #source
904
897
}
905
898
906
899
set loadError ( e ) {
907
- if ( this [ _loadError ] || this [ _raw ] ) {
900
+ if ( this [ _loadError ] || this . #raw ) {
908
901
throw new Error ( 'cannot set ConfigData loadError after load' )
909
902
}
910
903
this [ _loadError ] = e
@@ -915,14 +908,14 @@ class ConfigData {
915
908
}
916
909
917
910
set raw ( r ) {
918
- if ( this [ _raw ] || this [ _loadError ] ) {
911
+ if ( this . #raw || this [ _loadError ] ) {
919
912
throw new Error ( 'cannot set ConfigData raw after load' )
920
913
}
921
- this [ _raw ] = r
914
+ this . #raw = r
922
915
}
923
916
924
917
get raw ( ) {
925
- return this [ _raw ]
918
+ return this . #raw
926
919
}
927
920
}
928
921
0 commit comments