@npmcli/run-script@1.5.0 · npm/cli@631142f (original) (raw)
`@@ -8,11 +8,14 @@
`
8
8
`// and the value is a string or array. Otherwise return false.
`
9
9
`const envKey = (key, val) => {
`
10
10
`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()}
``
13
13
`}
`
14
14
``
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)
`
16
19
``
17
20
`const sameConfigValue = (def, val) =>
`
18
21
`!Array.isArray(val) || !Array.isArray(def) ? def === val
`
`@@ -30,20 +33,48 @@ const sameArrayValue = (def, val) => {
`
30
33
`return true
`
31
34
`}
`
32
35
``
``
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
+
33
43
`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))
`
38
58
`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
`+
}
`
44
74
`}
`
45
75
`}
`
46
76
``
``
77
`+
// also set some other common ones.
`
47
78
`process.env.npm_execpath = require.main.filename
`
48
79
`process.env.npm_node_execpath = process.execPath
`
49
80
`process.env.npm_command = npm.command
`