Skip to content

Commit 631142f

Browse files
committed
1 parent fd7a498 commit 631142f

File tree

7 files changed

+83
-23
lines changed

7 files changed

+83
-23
lines changed

lib/config/set-envs.js

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@
88
// and the value is a string or array. Otherwise return false.
99
const envKey = (key, val) => {
1010
return !/^[/@_]/.test(key) &&
11-
(typeof val === 'string' || Array.isArray(val)) &&
12-
`npm_config_${key.replace(/-/g, '_').toLowerCase()}`
11+
(typeof envVal(val) === 'string') &&
12+
`npm_config_${key.replace(/-/g, '_').toLowerCase()}`
1313
}
1414

15-
const envVal = val => Array.isArray(val) ? val.join('\n\n') : val
15+
const envVal = val => Array.isArray(val) ? val.map(v => envVal(v)).join('\n\n')
16+
: val === null || val === undefined || val === false ? ''
17+
: typeof val === 'object' ? null
18+
: String(val)
1619

1720
const sameConfigValue = (def, val) =>
1821
!Array.isArray(val) || !Array.isArray(def) ? def === val
@@ -30,20 +33,48 @@ const sameArrayValue = (def, val) => {
3033
return true
3134
}
3235

36+
const setEnv = (rawKey, rawVal) => {
37+
const val = envVal(rawVal)
38+
const key = envKey(rawKey, val)
39+
if (key)
40+
process.env[key] = val
41+
}
42+
3343
const setEnvs = npm => {
34-
// The objects in the config.list array are arranged in
35-
// a prototype chain, so we can just for/in over the top
36-
// of the stack and grab any that don't match the default
37-
const { config: { list: [configs] } } = npm
44+
// This ensures that all npm config values that are not the defaults are
45+
// shared appropriately with child processes, without false positives.
46+
//
47+
// if the key is the default value,
48+
// if the environ is NOT the default value,
49+
// set the environ
50+
// else skip it, it's fine
51+
// if the key is NOT the default value,
52+
// if the env is setting it, then leave it (already set)
53+
// otherwise, set the env
54+
console.error(npm.config.list)
55+
const { config: { list: [cli, env] } } = npm
56+
const cliSet = new Set(Object.keys(cli))
57+
const envSet = new Set(Object.keys(env))
3858
const { defaults } = require('./defaults.js')
39-
for (const key in configs) {
40-
const val = configs[key]
41-
const environ = envKey(key, val)
42-
if (!sameConfigValue(defaults[key], val) && environ) {
43-
process.env[environ] = envVal(val)
59+
// the configs form a prototype chain, so we can for/in over cli to
60+
// see all the current values, and hasOwnProperty to see if it's
61+
// set therre.
62+
for (const key in cli) {
63+
if (sameConfigValue(defaults[key], cli[key])) {
64+
if (!sameConfigValue(defaults[key], env[key])) {
65+
// getting set back to the default in the cli config
66+
setEnv(key, cli[key])
67+
}
68+
} else {
69+
// config is not the default
70+
if (!(envSet.has(key) && !cliSet.has(key))) {
71+
// was not set in the env, so we have to put it there
72+
setEnv(key, cli[key])
73+
}
4474
}
4575
}
4676

77+
// also set some other common ones.
4778
process.env.npm_execpath = require.main.filename
4879
process.env.npm_node_execpath = process.execPath
4980
process.env.npm_command = npm.command

node_modules/@npmcli/run-script/README.md

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/@npmcli/run-script/lib/package-envs.js

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/@npmcli/run-script/lib/run-script-pkg.js

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/@npmcli/run-script/package.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
"dependencies": {
4545
"@npmcli/arborist": "^0.0.16",
4646
"@npmcli/ci-detect": "^1.2.0",
47-
"@npmcli/run-script": "^1.4.0",
47+
"@npmcli/run-script": "^1.5.0",
4848
"abbrev": "~1.1.1",
4949
"ansicolors": "~0.3.2",
5050
"ansistyles": "~0.1.3",

0 commit comments

Comments
 (0)