From 25af71d2922bcf3b7eb2ceb6ca22e55ed17cfa9d Mon Sep 17 00:00:00 2001 From: Daniel Stockman Date: Wed, 3 Oct 2018 16:13:02 -0700 Subject: [PATCH] fix(npm-conf): Do not overwrite defaults with undefined cli keys --- utils/npm-conf/__tests__/npm-conf.test.js | 18 ++++++++++++++++++ utils/npm-conf/lib/npm-conf.js | 14 +++++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/utils/npm-conf/__tests__/npm-conf.test.js b/utils/npm-conf/__tests__/npm-conf.test.js index 140bf95f3b..14e87b1780 100644 --- a/utils/npm-conf/__tests__/npm-conf.test.js +++ b/utils/npm-conf/__tests__/npm-conf.test.js @@ -29,4 +29,22 @@ describe("@lerna/npm-conf", () => { "//npm.example.com/some-api/npm-virtual/" ); }); + + it("defaults cli parameter to empty object", () => { + const conf = npmConf(); + + expect(conf.sources.cli.data).toEqual({}); + }); + + it("overwrites default with cli key", () => { + const conf = npmConf({ registry: "https://npm.example.com" }); + + expect(conf.get("registry")).toBe("https://npm.example.com"); + }); + + it("does not overwrite default with undefined cli key", () => { + const conf = npmConf({ registry: undefined }); + + expect(conf.get("registry")).toBe("https://registry.npmjs.org/"); + }); }); diff --git a/utils/npm-conf/lib/npm-conf.js b/utils/npm-conf/lib/npm-conf.js index 2cd01e2445..36089bbf92 100644 --- a/utils/npm-conf/lib/npm-conf.js +++ b/utils/npm-conf/lib/npm-conf.js @@ -14,7 +14,19 @@ module.exports.toNerfDart = toNerfDart; function npmConf(opts) { const conf = new Conf(Object.assign({}, defaults.defaults)); - conf.add(Object.assign({}, opts), "cli"); + // prevent keys with undefined values from obscuring defaults + const cleanOpts = opts + ? Object.keys(opts).reduce((acc, key) => { + if (opts && opts[key] !== undefined) { + // eslint-disable-next-line no-param-reassign + acc[key] = opts[key]; + } + + return acc; + }, {}) + : {}; + + conf.add(cleanOpts, "cli"); conf.addEnv(); conf.loadPrefix();