Skip to content

Commit

Permalink
fixup: fewer nested if statements
Browse files Browse the repository at this point in the history
  • Loading branch information
wraithgar committed Jan 22, 2024
1 parent 6ef5904 commit 073018b
Showing 1 changed file with 10 additions and 17 deletions.
27 changes: 10 additions & 17 deletions lib/package-envs.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,24 @@
// https://github.com/npm/rfcs/pull/183

const envVal = val => {
if (val === null || val === false) {
return ''
}
return String(val)
}

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

// 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,
Expand Down

0 comments on commit 073018b

Please sign in to comment.