From 5af107dcd0f6decd3697d387b74829379a7f68d4 Mon Sep 17 00:00:00 2001 From: Gar Date: Mon, 22 Jan 2024 09:03:32 -0800 Subject: [PATCH] fixup: cleaner packageEnvs --- lib/package-envs.js | 23 ++++++++++++----------- lib/run-script-pkg.js | 2 +- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/lib/package-envs.js b/lib/package-envs.js index 4fb5187..612f850 100644 --- a/lib/package-envs.js +++ b/lib/package-envs.js @@ -1,5 +1,4 @@ - -const packageEnvs = (env, vals, prefix) => { +const packageEnvs = (vals, prefix, env = {}) => { for (const [key, val] of Object.entries(vals)) { if (val === undefined) { continue @@ -7,10 +6,10 @@ const packageEnvs = (env, vals, prefix) => { env[`${prefix}${key}`] = '' } else if (Array.isArray(val)) { val.forEach((item, index) => { - packageEnvs(env, { [`${key}_${index}`]: item }, `${prefix}`) + packageEnvs({ [`${key}_${index}`]: item }, `${prefix}`, env) }) } else if (typeof val === 'object') { - packageEnvs(env, val, `${prefix}${key}_`) + packageEnvs(val, `${prefix}${key}_`, env) } else { env[`${prefix}${key}`] = String(val) } @@ -19,10 +18,12 @@ const packageEnvs = (env, vals, prefix) => { } // https://github.com/npm/rfcs/pull/183 defines which fields we put into the environment -module.exports = (env, pkg) => packageEnvs({ ...env }, { - name: pkg.name, - version: pkg.version, - config: pkg.config, - engines: pkg.engines, - bin: pkg.bin, -}, 'npm_package_') +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,