@npmcli/config 1.1.6 → 1.2.1

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
@@ -2,7 +2,7 @@
2
2
 
3
3
  Configuration management for the npm cli.
4
4
 
5
- This module is the spiritual decendant of
5
+ This module is the spiritual descendant of
6
6
  [`npmconf`](http://npm.im/npmconf), and the code that once lived in npm's
7
7
  `lib/config/` folder.
8
8
 
@@ -27,7 +27,7 @@ The only exceptions:
27
27
  - A `builtin` config, read from a `npmrc` file in the root of the npm
28
28
  project itself, overrides all defaults.
29
29
 
30
- The resulting heirarchy of configs:
30
+ The resulting hierarchy of configs:
31
31
 
32
32
  - CLI switches. eg `--some-key=some-value` on the command line. These are
33
33
  parsed by [`nopt`](http://npm.im/nopt), which is not a great choice, but
@@ -3,12 +3,11 @@
3
3
  const envExpr = /(\\*)\$\{([^}]+)\}/g
4
4
 
5
5
  module.exports = (f, env) => f.replace(envExpr, (orig, esc, name) => {
6
+ const val = env[name] !== undefined ? env[name] : `\$\{${name}\}`
7
+
6
8
  // consume the escape chars that are relevant.
7
9
  if (esc.length % 2)
8
10
  return orig.substr((esc.length + 1) / 2)
9
11
 
10
- if (undefined === env[name])
11
- throw new Error('Failed to replace env in config: ' + orig)
12
-
13
- return (esc.substr(esc.length / 2)) + env[name]
12
+ return (esc.substr(esc.length / 2)) + val
14
13
  })
@@ -0,0 +1,13 @@
1
+ // Accepts a config object, returns a user-agent string
2
+ const getUserAgent = (config) => {
3
+ const ciName = config.get('ci-name')
4
+ return (config.get('user-agent') || '')
5
+ .replace(/\{node-version\}/gi, config.get('node-version'))
6
+ .replace(/\{npm-version\}/gi, config.get('npm-version'))
7
+ .replace(/\{platform\}/gi, process.platform)
8
+ .replace(/\{arch\}/gi, process.arch)
9
+ .replace(/\{ci\}/gi, ciName ? `ci/${ciName}` : '')
10
+ .trim()
11
+ }
12
+
13
+ module.exports = getUserAgent
package/lib/index.js CHANGED
@@ -47,6 +47,7 @@ const envReplace = require('./env-replace.js')
47
47
  const parseField = require('./parse-field.js')
48
48
  const typeDescription = require('./type-description.js')
49
49
  const setEnvs = require('./set-envs.js')
50
+ const getUserAgent = require('./get-user-agent.js')
50
51
 
51
52
  // types that can be saved back to
52
53
  const confFileTypes = new Set([
@@ -241,6 +242,13 @@ class Config {
241
242
  // symbols, as that module also does a bunch of get operations
242
243
  this[_loaded] = true
243
244
 
245
+ // set proper globalPrefix now that everything is loaded
246
+ this.globalPrefix = this.get('prefix')
247
+
248
+ process.emit('time', 'config:load:setUserAgent')
249
+ this.setUserAgent()
250
+ process.emit('timeEnd', 'config:load:setUserAgent')
251
+
244
252
  process.emit('time', 'config:load:setEnvs')
245
253
  this.setEnvs()
246
254
  process.emit('timeEnd', 'config:load:setEnvs')
@@ -300,7 +308,7 @@ class Config {
300
308
  const prefix = 'npm_config_'
301
309
  const conf = Object.create(null)
302
310
  for (const [envKey, envVal] of Object.entries(this.env)) {
303
- if (!/^npm_config_/i.test(envKey))
311
+ if (!/^npm_config_/i.test(envKey) || envVal === '')
304
312
  continue
305
313
  const key = envKey.substr('npm_config_'.length)
306
314
  .replace(/(?!^)_/g, '-') // don't replace _ at the start of the key
@@ -691,6 +699,12 @@ class Config {
691
699
  })
692
700
  }
693
701
 
702
+ // the user-agent configuration is a template that gets populated
703
+ // with some variables, that takes place here
704
+ setUserAgent () {
705
+ this.set('user-agent', getUserAgent(this))
706
+ }
707
+
694
708
  // set up the environment object we have with npm_config_* environs
695
709
  // for all configs that are different from their default values, and
696
710
  // set EDITOR and HOME.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@npmcli/config",
3
- "version": "1.1.6",
3
+ "version": "1.2.1",
4
4
  "files": [
5
5
  "lib"
6
6
  ],