Skip to content

Commit

Permalink
Set non-default configs in the environment
Browse files Browse the repository at this point in the history
Setting up for the switch from npm-lifecycle to @npmcli/run-script.
  • Loading branch information
isaacs committed Mar 17, 2020
1 parent a012888 commit 2fb8c26
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 9 deletions.
10 changes: 7 additions & 3 deletions lib/config.js
Expand Up @@ -269,9 +269,13 @@ function list (cb) {
var defKeys = getKeys(defaults)
msg += '; default values\n'
defKeys.forEach(function (k) {
if (defaults[k] && typeof defaults[k] === 'object') return
var val = JSON.stringify(defaults[k])
if (defaults[k] !== npm.config.get(k)) {
const def = defaults[k]
// XXX remove this skip when we stop putting log in the npm.config set.
if (def && typeof def === 'object' && !Array.isArray(def)) {
return
}
var val = JSON.stringify(def)
if (def !== npm.config.get(k)) {
msg += '; ' + k + ' = ' + val + ' (overridden)\n'
} else msg += k + ' = ' + val + '\n'
})
Expand Down
4 changes: 0 additions & 4 deletions lib/config/defaults.js
Expand Up @@ -4,7 +4,6 @@ var path = require('path')
var url = require('url')
var Stream = require('stream').Stream
var semver = require('semver')
var stableFamily = semver.parse(process.version)
var nopt = require('nopt')
var os = require('os')
var osenv = require('osenv')
Expand Down Expand Up @@ -68,9 +67,6 @@ nopt.invalidHandler = function (k, val, type) {
}
}

if (!stableFamily || (+stableFamily.minor % 2)) stableFamily = null
else stableFamily = stableFamily.major + '.' + stableFamily.minor

var defaults

var temp = osenv.tmpdir()
Expand Down
2 changes: 1 addition & 1 deletion lib/config/flat-options.js
Expand Up @@ -159,7 +159,7 @@ const flatOptions = npm => npm.flatOptions || Object.freeze({
packageLockOnly: npm.config.get('package-lock-only'),
globalStyle: npm.config.get('global-style'),
legacyBundling: npm.config.get('legacy-bundling'),
scriptShell: npm.config.get('script-shell'),
scriptShell: npm.config.get('script-shell') || undefined,
omit: buildOmitList(npm),

// used to build up the appropriate {add:{...}} options to Arborist.reify
Expand Down
21 changes: 20 additions & 1 deletion lib/config/set-envs.js
Expand Up @@ -5,6 +5,9 @@

const envName = name => `npm_config_${name.replace(/-/g, '_')}`

// Return the env key if this is a thing that belongs in the env.
// Ie, if the key isn't a @scope, //nerf.dart, or _private,
// and the value is a string or array. Otherwise return false.
const envKey = (key, val) => {
return !/^[\/@_]/.test(key) &&
(typeof val === 'string' || Array.isArray(val)) &&
Expand All @@ -13,6 +16,22 @@ const envKey = (key, val) => {

const envVal = val => Array.isArray(val) ? val.join('\n\n') : val

const sameConfigValue = (def, val) =>
!Array.isArray(val) || !Array.isArray(def) ? def === val
: sameArrayValue(def, val)

const sameArrayValue = (def, val) => {
if (def.length !== val.length) {
return false
}
for (let i = 0; i < def.length; i++) {
if (def[i] !== val[i]) {
return false
}
}
return true
}

const setEnvs = npm => {
// The objects in the config.list array are arranged in
// a prototype chain, so we can just for/in over the top
Expand All @@ -23,7 +42,7 @@ const setEnvs = npm => {
for (const key in configs) {
const val = configs[key]
const environ = envKey(key, val)
if (defaults[key] !== val && environ) {
if (!sameConfigValue(defaults[key], val) && environ) {
process.env[environ] = envVal(val)
}
}
Expand Down

0 comments on commit 2fb8c26

Please sign in to comment.