diff --git a/lib/package-envs.js b/lib/package-envs.js index aa935b2..612f850 100644 --- a/lib/package-envs.js +++ b/lib/package-envs.js @@ -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_') +} diff --git a/lib/run-script-pkg.js b/lib/run-script-pkg.js index 1495fa3..ea33db5 100644 --- a/lib/run-script-pkg.js +++ b/lib/run-script-pkg.js @@ -69,7 +69,7 @@ const runScriptPkg = async options => { path, scriptShell, binPaths, - env: packageEnvs(env, pkg), + env: { ...env, ...packageEnvs(pkg) }, stdio, cmd, args, diff --git a/test/make-spawn-args.js b/test/make-spawn-args.js index cc1f719..83e90a0 100644 --- a/test/make-spawn-args.js +++ b/test/make-spawn-args.js @@ -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 &&