diff options
author | isaacs <i@izs.me> | 2021-03-16 02:55:47 +0300 |
---|---|---|
committer | isaacs <i@izs.me> | 2021-03-18 21:58:08 +0300 |
commit | f52c51db13c39cfbaed18dbd13ba7302a4b6a0d9 (patch) | |
tree | 8c0b2fbc2644a1cc8bea2b9ac2f0112afb7cf751 /node_modules/@npmcli | |
parent | 68db12440fc9e851563b2c2acd85499776a2a63b (diff) |
@npmcli/config@2.0.0
Eventually @npmcli/config will move back to the CLI under
lib/utils/config/, so this v2 will be a short-lived release family.
Diffstat (limited to 'node_modules/@npmcli')
-rw-r--r-- | node_modules/@npmcli/config/lib/get-user-agent.js | 13 | ||||
-rw-r--r-- | node_modules/@npmcli/config/lib/index.js | 115 | ||||
-rw-r--r-- | node_modules/@npmcli/config/lib/set-envs.js | 6 | ||||
-rw-r--r-- | node_modules/@npmcli/config/package.json | 2 |
4 files changed, 64 insertions, 72 deletions
diff --git a/node_modules/@npmcli/config/lib/get-user-agent.js b/node_modules/@npmcli/config/lib/get-user-agent.js deleted file mode 100644 index c3d3c7a1b..000000000 --- a/node_modules/@npmcli/config/lib/get-user-agent.js +++ /dev/null @@ -1,13 +0,0 @@ -// Accepts a config object, returns a user-agent string -const getUserAgent = (config) => { - const ciName = config.get('ci-name') - return (config.get('user-agent') || '') - .replace(/\{node-version\}/gi, config.get('node-version')) - .replace(/\{npm-version\}/gi, config.get('npm-version')) - .replace(/\{platform\}/gi, process.platform) - .replace(/\{arch\}/gi, process.arch) - .replace(/\{ci\}/gi, ciName ? `ci/${ciName}` : '') - .trim() -} - -module.exports = getUserAgent diff --git a/node_modules/@npmcli/config/lib/index.js b/node_modules/@npmcli/config/lib/index.js index e7fac96c1..21a37ded4 100644 --- a/node_modules/@npmcli/config/lib/index.js +++ b/node_modules/@npmcli/config/lib/index.js @@ -47,7 +47,6 @@ const envReplace = require('./env-replace.js') const parseField = require('./parse-field.js') const typeDescription = require('./type-description.js') const setEnvs = require('./set-envs.js') -const getUserAgent = require('./get-user-agent.js') // types that can be saved back to const confFileTypes = new Set([ @@ -69,6 +68,9 @@ const _get = Symbol('get') const _find = Symbol('find') const _loadObject = Symbol('loadObject') const _loadFile = Symbol('loadFile') +const _checkDeprecated = Symbol('checkDeprecated') +const _flatten = Symbol('flatten') +const _flatOptions = Symbol('flatOptions') class Config { static get typeDefs () { @@ -76,9 +78,9 @@ class Config { } constructor ({ - types, + definitions, shorthands, - defaults, + flatten, npmPath, // options just to override in tests, mostly @@ -89,10 +91,27 @@ class Config { execPath = process.execPath, cwd = process.cwd(), }) { - this.npmPath = npmPath + + // turn the definitions into nopt's weirdo syntax + this.definitions = definitions + const types = {} + const defaults = {} + this.deprecated = {} + for (const [key, def] of Object.entries(definitions)) { + defaults[key] = def.default + types[key] = def.type + if (def.deprecated) + this.deprecated[key] = def.deprecated.trim().replace(/\n +/, '\n') + } + + // populated the first time we flatten the object + this[_flatOptions] = null + this[_flatten] = flatten this.types = types this.shorthands = shorthands this.defaults = defaults + + this.npmPath = npmPath this.log = log this.argv = argv this.env = env @@ -183,10 +202,31 @@ class Config { if (!email) throw new Error('Cannot set _auth without first setting email') } - this.data.get(where).data[key] = val + this[_checkDeprecated](key) + const { data } = this.data.get(where) + data[key] = val // this is now dirty, the next call to this.valid will have to check it this.data.get(where)[_valid] = null + + // the flat options are invalidated, regenerate next time they're needed + this[_flatOptions] = null + } + + get flat () { + if (this[_flatOptions]) + return this[_flatOptions] + + // create the object for flat options passed to deps + process.emit('time', 'config:load:flatten') + this[_flatOptions] = {} + // walk from least priority to highest + for (const { data } of this.data.values()) { + this[_flatten](data, this[_flatOptions]) + } + process.emit('timeEnd', 'config:load:flatten') + + return this[_flatOptions] } delete (key, where = 'cli') { @@ -233,11 +273,6 @@ class Config { await this.loadGlobalConfig() process.emit('timeEnd', 'config:load:global') - // now the extras - process.emit('time', 'config:load:cafile') - await this.loadCAFile() - process.emit('timeEnd', 'config:load:cafile') - // warn if anything is not valid process.emit('time', 'config:load:validate') this.validate() @@ -250,10 +285,6 @@ class Config { // set proper globalPrefix now that everything is loaded this.globalPrefix = this.get('prefix') - process.emit('time', 'config:load:setUserAgent') - this.setUserAgent() - process.emit('timeEnd', 'config:load:setUserAgent') - process.emit('time', 'config:load:setEnvs') this.setEnvs() process.emit('timeEnd', 'config:load:setEnvs') @@ -376,13 +407,13 @@ class Config { this.data.get(where)[_valid] = false if (Array.isArray(type)) { - if (type.indexOf(typeDefs.url.type) !== -1) + if (type.includes(typeDefs.url.type)) type = typeDefs.url.type else { /* istanbul ignore if - no actual configs matching this, but * path types SHOULD be handled this way, like URLs, for the * same reason */ - if (type.indexOf(typeDefs.path.type) !== -1) + if (type.includes(typeDefs.path.type)) type = typeDefs.path.type } } @@ -428,11 +459,21 @@ class Config { for (const [key, value] of Object.entries(obj)) { const k = envReplace(key, this.env) const v = this.parseField(value, k) + if (where !== 'default') + this[_checkDeprecated](k, where, obj, [key, value]) conf.data[k] = v } } } + [_checkDeprecated] (key, where, obj, kv) { + // XXX a future npm version will make this a warning. + // An even more future npm version will make this an error. + if (this.deprecated[key]) { + this.log.verbose('config', key, this.deprecated[key]) + } + } + // Parse a field, coercing it to the best type available. parseField (f, key, listElement = false) { return parseField(f, key, this, listElement) @@ -675,48 +716,6 @@ class Config { return creds } - async loadCAFile () { - const where = this[_find]('cafile') - - /* istanbul ignore if - it'll always be set in the defaults */ - if (!where) - return - - const cafile = this[_get]('cafile', where) - const ca = this[_get]('ca', where) - - // if you have a ca, or cafile is set to null, then nothing to do here. - if (ca || !cafile) - return - - const raw = await readFile(cafile, 'utf8').catch(er => { - if (er.code !== 'ENOENT') - throw er - }) - if (!raw) - return - - const delim = '-----END CERTIFICATE-----' - const output = raw.replace(/\r\n/g, '\n').split(delim) - .filter(section => section.trim()) - .map(section => section.trimLeft() + delim) - - // make it non-enumerable so we don't save it back by accident - const { data } = this.data.get(where) - Object.defineProperty(data, 'ca', { - value: output, - enumerable: false, - configurable: true, - writable: true, - }) - } - - // the user-agent configuration is a template that gets populated - // with some variables, that takes place here - setUserAgent () { - this.set('user-agent', getUserAgent(this)) - } - // set up the environment object we have with npm_config_* environs // for all configs that are different from their default values, and // set EDITOR and HOME. diff --git a/node_modules/@npmcli/config/lib/set-envs.js b/node_modules/@npmcli/config/lib/set-envs.js index 36d371454..ffaf5ab38 100644 --- a/node_modules/@npmcli/config/lib/set-envs.js +++ b/node_modules/@npmcli/config/lib/set-envs.js @@ -50,11 +50,13 @@ const setEnvs = (config) => { platform, env, defaults, + definitions, list: [cliConf, envConf], } = config env.INIT_CWD = process.cwd() + // if the key is deprecated, skip it always. // if the key is the default value, // if the environ is NOT the default value, // set the environ @@ -65,6 +67,10 @@ const setEnvs = (config) => { const cliSet = new Set(Object.keys(cliConf)) const envSet = new Set(Object.keys(envConf)) for (const key in cliConf) { + const { deprecated } = definitions[key] || {} + if (deprecated) + continue + if (sameConfigValue(defaults[key], cliConf[key])) { // config is the default, if the env thought different, then we // have to set it BACK to the default in the environment. diff --git a/node_modules/@npmcli/config/package.json b/node_modules/@npmcli/config/package.json index 644544a49..b2b34f6af 100644 --- a/node_modules/@npmcli/config/package.json +++ b/node_modules/@npmcli/config/package.json @@ -1,6 +1,6 @@ { "name": "@npmcli/config", - "version": "1.2.9", + "version": "2.0.0", "files": [ "lib" ], |