@npmcli/config
Advanced tools
Comparing version 1.2.9 to 2.0.0
115
lib/index.js
@@ -50,3 +50,2 @@ // TODO: set the scope config from package.json or explicit cli config | ||
const setEnvs = require('./set-envs.js') | ||
const getUserAgent = require('./get-user-agent.js') | ||
@@ -73,2 +72,5 @@ // types that can be saved back to | ||
const _loadFile = Symbol('loadFile') | ||
const _checkDeprecated = Symbol('checkDeprecated') | ||
const _flatten = Symbol('flatten') | ||
const _flatOptions = Symbol('flatOptions') | ||
@@ -81,5 +83,5 @@ class Config { | ||
constructor ({ | ||
types, | ||
definitions, | ||
shorthands, | ||
defaults, | ||
flatten, | ||
npmPath, | ||
@@ -95,6 +97,23 @@ | ||
}) { | ||
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 | ||
@@ -190,8 +209,29 @@ this.argv = argv | ||
} | ||
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') { | ||
@@ -241,7 +281,2 @@ if (!this.loaded) | ||
// now the extras | ||
process.emit('time', 'config:load:cafile') | ||
await this.loadCAFile() | ||
process.emit('timeEnd', 'config:load:cafile') | ||
// warn if anything is not valid | ||
@@ -259,6 +294,2 @@ process.emit('time', 'config:load:validate') | ||
process.emit('time', 'config:load:setUserAgent') | ||
this.setUserAgent() | ||
process.emit('timeEnd', 'config:load:setUserAgent') | ||
process.emit('time', 'config:load:setEnvs') | ||
@@ -386,3 +417,3 @@ this.setEnvs() | ||
if (Array.isArray(type)) { | ||
if (type.indexOf(typeDefs.url.type) !== -1) | ||
if (type.includes(typeDefs.url.type)) | ||
type = typeDefs.url.type | ||
@@ -393,3 +424,3 @@ else { | ||
* same reason */ | ||
if (type.indexOf(typeDefs.path.type) !== -1) | ||
if (type.includes(typeDefs.path.type)) | ||
type = typeDefs.path.type | ||
@@ -440,2 +471,4 @@ } | ||
const v = this.parseField(value, k) | ||
if (where !== 'default') | ||
this[_checkDeprecated](k, where, obj, [key, value]) | ||
conf.data[k] = v | ||
@@ -446,2 +479,10 @@ } | ||
[_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. | ||
@@ -689,44 +730,2 @@ parseField (f, key, listElement = false) { | ||
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 | ||
@@ -733,0 +732,0 @@ // for all configs that are different from their default values, and |
@@ -53,2 +53,3 @@ // Set environment variables for any non-default configs, | ||
defaults, | ||
definitions, | ||
list: [cliConf, envConf], | ||
@@ -59,2 +60,3 @@ } = config | ||
// if the key is deprecated, skip it always. | ||
// if the key is the default value, | ||
@@ -70,2 +72,6 @@ // if the environ is NOT the default value, | ||
for (const key in cliConf) { | ||
const { deprecated } = definitions[key] || {} | ||
if (deprecated) | ||
continue | ||
if (sameConfigValue(defaults[key], cliConf[key])) { | ||
@@ -72,0 +78,0 @@ // config is the default, if the env thought different, then we |
{ | ||
"name": "@npmcli/config", | ||
"version": "1.2.9", | ||
"version": "2.0.0", | ||
"files": [ | ||
@@ -5,0 +5,0 @@ "lib" |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
0
43330
12
941