Skip to content

Commit

Permalink
fixup: cleaner packageEnvs
Browse files Browse the repository at this point in the history
  • Loading branch information
wraithgar committed Jan 22, 2024
1 parent 073018b commit 5af107d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
23 changes: 12 additions & 11 deletions lib/package-envs.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@

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 === null || val === false) {
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)
}
Expand All @@ -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_')
}
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

0 comments on commit 5af107d

Please sign in to comment.