Skip to content
This repository has been archived by the owner on Aug 11, 2021. It is now read-only.

opts.env option to set script specific enviromnent variables #40

Open
wants to merge 2 commits into
base: latest
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ jump in if you'd like to, or even ask us questions if something isn't clear.
* `opts.stdio` - the [stdio](https://nodejs.org/api/child_process.html#child_process_options_stdio)
passed to the child process. `[0, 1, 2]` by default.

* `opts.env` - if is an object, all its properties of type 'number' of 'string' are added to the environment before running the script

##### Example

```javascript
Expand Down
12 changes: 12 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,18 @@ function makeEnv (data, opts, prefix, env) {
}
}

// assign script specific environnement variables (ignores non string values)
if (typeof opts.env === 'object') {
for (var k in opts.env) {
if (opts.env.hasOwnProperty(k)) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.hasOwnProperty should never be called directly off of objects; what about an env var named hasOwnProperty (which is entirely valid)? Always do this:

Suggested change
if (opts.env.hasOwnProperty(k)) {
if (Object.prototype.hasOwnProperty.call(opts.env, k)) {

var v = opts.env[k]
if (typeof v === 'string' || typeof v === 'number') {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any reason to ignore non-string/number values, instead of throwing?

env[k] = String(v)
}
}
}
}

if (prefix !== 'npm_package_') return env

prefix = 'npm_config_'
Expand Down
14 changes: 13 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,14 @@ test('makeEnv', function (t) {

const env = lifecycle.makeEnv(pkg, {
config,
nodeOptions: '--inspect-brk --abort-on-uncaught-exception'
nodeOptions: '--inspect-brk --abort-on-uncaught-exception',
env: {
npm_lifecycle_var1: '6',
npm_lifecycle_var2: 7,
npm_lifecycle_ignored_null: null,
npm_lifecycle_ignored_object: { name: '8' },
npm_lifecycle_ignored_array: [ 10, 11 ]
}
}, null, Object.assign({}, process.env))

t.equal('myPackage', env.npm_package_name, 'package data is included')
Expand All @@ -43,6 +50,11 @@ test('makeEnv', function (t) {
t.equal('2', env.npm_package_config_bar, 'package config is included')
t.equal('4', env.npm_package_config_baz, 'package@version config is included')
t.equal('5', env.npm_package_config_foo, 'package@version config overrides package config')
t.equal('6', env.npm_lifecycle_var1, 'env string option is included')
t.equal('7', env.npm_lifecycle_var2, 'env number option is included')
t.equal(undefined, env.npm_lifecycle_ignored_null, 'env null options are ignored')
t.equal(undefined, env.npm_lifecycle_ignored_object, 'env object options are ignored')
t.equal(undefined, env.npm_lifecycle_ignored_array, 'env array options are ignored')

t.equal('--inspect-brk --abort-on-uncaught-exception', env.NODE_OPTIONS, 'nodeOptions sets NODE_OPTIONS')
t.end()
Expand Down