Skip to content

Commit

Permalink
fix(envs): revert behavior of array in npm config (#194)
Browse files Browse the repository at this point in the history
Reverts environment parsing of config arrays to use indexed names
instead of joining them with double newlines.  That change appears to
have been introduced when the enviromnent values were added back into
npm, but not in the same way they were done before.

Fixes: npm/cli#3775

---------

Co-authored-by: siemhesda <[email protected]>
  • Loading branch information
wraithgar and siemhesda authored Jan 22, 2024
1 parent 155c434 commit 7ebc853
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 25 deletions.
43 changes: 20 additions & 23 deletions lib/package-envs.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,29 @@
// https://github.com/npm/rfcs/pull/183

const envVal = val => {
if (Array.isArray(val)) {
return val.map(v => envVal(v)).join('\n\n')
}
if (val === null || val === false) {
return ''
}
return String(val)
}

const packageEnvs = (env, vals, prefix) => {
const packageEnvs = (vals, prefix, env = {}) => {
for (const [key, val] of Object.entries(vals)) {
if (val === undefined) {
continue
} else if (val && !Array.isArray(val) && typeof val === 'object') {
packageEnvs(env, val, `${prefix}${key}_`)
} else if (val === null || val === false) {
env[`${prefix}${key}`] = ''
} else if (Array.isArray(val)) {
val.forEach((item, index) => {
packageEnvs({ [`${key}_${index}`]: item }, `${prefix}`, env)
})
} else if (typeof val === 'object') {
packageEnvs(val, `${prefix}${key}_`, env)
} else {
env[`${prefix}${key}`] = envVal(val)
env[`${prefix}${key}`] = String(val)
}
}
return env
}

module.exports = (env, pkg) => packageEnvs({ ...env }, {
name: pkg.name,
version: pkg.version,
config: pkg.config,
engines: pkg.engines,
bin: pkg.bin,
}, 'npm_package_')
// https://github.com/npm/rfcs/pull/183 defines which fields we put into the environment
module.exports = pkg => {
return packageEnvs({
name: pkg.name,
version: pkg.version,
config: pkg.config,
engines: pkg.engines,
bin: pkg.bin,
}, 'npm_package_')
}
2 changes: 1 addition & 1 deletion lib/run-script-pkg.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ const runScriptPkg = async options => {
path,
scriptShell,
binPaths,
env: packageEnvs(env, pkg),
env: { ...env, ...packageEnvs(pkg) },
stdio,
cmd,
args,
Expand Down
3 changes: 2 additions & 1 deletion test/make-spawn-args.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ t.test('spawn args', async t => {
e.env.npm_package_config_test_null === '' &&
e.env.npm_package_config_test_false === '' &&
e.env.npm_package_config_test_string === pkg.config.test_string &&
e.env.npm_package_config_test_array === pkg.config.test_array.join('\n\n') &&
e.env.npm_package_config_test_array_0 === pkg.config.test_array[0] &&
e.env.npm_package_config_test_array_1 === pkg.config.test_array[1] &&
e.env.npm_package_bin === pkg.bin &&
e.env.npm_package_engines_npm === pkg.engines.npm &&
e.env.npm_package_engines_node === pkg.engines.node &&
Expand Down

0 comments on commit 7ebc853

Please sign in to comment.