@npmcli/config 2.2.0 → 2.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -218,6 +218,29 @@ Note that it's usually enough (and more efficient) to just check
218
218
  `config.valid`, since each data object is marked for re-evaluation on every
219
219
  `config.set()` operation.
220
220
 
221
+ ### `config.isDefault(key)`
222
+
223
+ Returns `true` if the value is coming directly from the
224
+ default definitions, if the current value for the key config is
225
+ coming from any other source, returns `false`.
226
+
227
+ This method can be used for avoiding or tweaking default values, e.g:
228
+
229
+ > Given a global default definition of foo='foo' it's possible to read that
230
+ > value such as:
231
+ >
232
+ > ```js
233
+ > const save = config.get('foo')
234
+ > ```
235
+ >
236
+ > Now in a different place of your app it's possible to avoid using the `foo`
237
+ > default value, by checking to see if the current config value is currently
238
+ > one that was defined by the default definitions:
239
+ >
240
+ > ```js
241
+ > const save = config.isDefault('foo') ? 'bar' : config.get('foo')
242
+ > ```
243
+
221
244
  ### `config.save(where)`
222
245
 
223
246
  Save the config file specified by the `where` param. Must be one of
package/lib/index.js CHANGED
@@ -401,6 +401,20 @@ class Config {
401
401
  }
402
402
  }
403
403
 
404
+ // Returns true if the value is coming directly from the source defined
405
+ // in default definitions, if the current value for the key config is
406
+ // coming from any other different source, returns false
407
+ isDefault (key) {
408
+ const [defaultType, ...types] = [...confTypes]
409
+ const defaultData = this.data.get(defaultType).data
410
+
411
+ return hasOwnProperty(defaultData, key)
412
+ && types.every(type => {
413
+ const typeData = this.data.get(type).data
414
+ return !hasOwnProperty(typeData, key)
415
+ })
416
+ }
417
+
404
418
  invalidHandler (k, val, type, source, where) {
405
419
  this.log.warn(
406
420
  'invalid config',
@@ -498,8 +512,16 @@ class Config {
498
512
 
499
513
  async loadProjectConfig () {
500
514
  // the localPrefix can be set by the CLI config, but otherwise is
501
- // found by walking up the folder tree
515
+ // found by walking up the folder tree. either way, we load it before
516
+ // we return to make sure localPrefix is set
502
517
  await this.loadLocalPrefix()
518
+
519
+ if (this[_get]('global') === true || this[_get]('location') === 'global') {
520
+ this.data.get('project').source = '(global mode enabled, ignored)'
521
+ this.sources.set(this.data.get('project').source, 'project')
522
+ return
523
+ }
524
+
503
525
  const projectFile = resolve(this.localPrefix, '.npmrc')
504
526
  // if we're in the ~ directory, and there happens to be a node_modules
505
527
  // folder (which is not TOO uncommon, it turns out), then we can end
package/lib/set-envs.js CHANGED
@@ -86,6 +86,8 @@ const setEnvs = (config) => {
86
86
 
87
87
  // also set some other common nice envs that we want to rely on
88
88
  env.HOME = config.home
89
+ env.npm_config_global_prefix = config.globalPrefix
90
+ env.npm_config_local_prefix = config.localPrefix
89
91
  if (cliConf.editor)
90
92
  env.EDITOR = cliConf.editor
91
93
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@npmcli/config",
3
- "version": "2.2.0",
3
+ "version": "2.4.0",
4
4
  "files": [
5
5
  "lib"
6
6
  ],