Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(envs): revert behavior of array in npm config #194

Merged
merged 5 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading