From 10c0c08fc25fea3c18c7c030d4618a401963355a Mon Sep 17 00:00:00 2001 From: isaacs Date: Wed, 18 Sep 2019 10:07:56 -0700 Subject: [PATCH] fix: filter functions and undefined out of makeEnv --- index.js | 7 ++++++- test/index.js | 9 ++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index f775155..337de71 100644 --- a/index.js +++ b/index.js @@ -458,12 +458,17 @@ function makeEnv (data, opts, prefix, env) { return } var value = opts.config[i] - if (value instanceof Stream || Array.isArray(value)) return + if (value instanceof Stream || Array.isArray(value) || typeof value === 'function') return if (i.match(/umask/)) value = umask.toString(value) + if (!value) value = '' else if (typeof value === 'number') value = '' + value else if (typeof value !== 'string') value = JSON.stringify(value) + if (typeof value !== 'string') { + return + } + value = value.indexOf('\n') !== -1 ? JSON.stringify(value) : value diff --git a/test/index.js b/test/index.js index a81f340..4a77ba4 100644 --- a/test/index.js +++ b/test/index.js @@ -20,7 +20,10 @@ test('makeEnv', function (t) { 'myPackage:bar': 2, 'myPackage:foo': 3, 'myPackage@1.0.0:baz': 4, - 'myPackage@1.0.0:foo': 5 + 'myPackage@1.0.0:foo': 5, + ignoreThisFunction: () => {}, + jsonsAsUndefined: { toJSON: () => undefined }, + jsonsAsFunction: { toJSON: () => () => {} } } const env = lifecycle.makeEnv(pkg, { @@ -44,6 +47,10 @@ test('makeEnv', function (t) { 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(env.npm_package_config_ignoreThisFunction, undefined) + t.equal(env.npm_package_config_jsonsAsUndefined, undefined) + t.equal(env.npm_package_config_jsonsAsFunction, undefined) + t.equal('--inspect-brk --abort-on-uncaught-exception', env.NODE_OPTIONS, 'nodeOptions sets NODE_OPTIONS') t.end() })