From 089ecafc0d2236a2c3b85db75b6d3234e1724e0e Mon Sep 17 00:00:00 2001 From: Olivier Chevet Date: Thu, 15 Aug 2019 07:40:55 +0200 Subject: [PATCH 1/2] Add support for 'env' option --- index.js | 12 ++++++++++++ test/index.js | 14 +++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index f775155..4b8bd36 100644 --- a/index.js +++ b/index.js @@ -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)) { + var v = opts.env[k] + if (typeof v === 'string' || typeof v === 'number') { + env[k] = String(v) + } + } + } + } + if (prefix !== 'npm_package_') return env prefix = 'npm_config_' diff --git a/test/index.js b/test/index.js index a81f340..115e07b 100644 --- a/test/index.js +++ b/test/index.js @@ -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') @@ -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() From 7396a6e8eebf56131b9f100dee10b80a1220b58b Mon Sep 17 00:00:00 2001 From: Olivier Chevet Date: Thu, 15 Aug 2019 07:52:36 +0200 Subject: [PATCH 2/2] Updated the docs --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index c81d3d7..2322dc9 100644 --- a/README.md +++ b/README.md @@ -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