Skip to content

Commit

Permalink
fix(npm-conf): Do not overwrite defaults with undefined cli keys
Browse files Browse the repository at this point in the history
  • Loading branch information
evocateur committed Oct 3, 2018
1 parent 159a0b0 commit 25af71d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
18 changes: 18 additions & 0 deletions utils/npm-conf/__tests__/npm-conf.test.js
Expand Up @@ -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/");
});
});
14 changes: 13 additions & 1 deletion utils/npm-conf/lib/npm-conf.js
Expand Up @@ -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();

Expand Down

0 comments on commit 25af71d

Please sign in to comment.