Skip to content

Commit

Permalink
fix(project): Deprecate root-level config keys as well, prioritizing …
Browse files Browse the repository at this point in the history
…nested
  • Loading branch information
evocateur committed Jan 4, 2019
1 parent 5075d8c commit 7a65a87
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 16 deletions.
1 change: 1 addition & 0 deletions core/project/__fixtures__/extends-deprecated/base.json
Expand Up @@ -4,6 +4,7 @@
],
"command": {
"publish": {
"cdVersion": "major",
"loglevel": "success"
}
},
Expand Down
Expand Up @@ -3,6 +3,7 @@
"packages": [
"recursive-pkgs/*"
],
"cdVersion": "prerelease",
"commands": {
"publish": {
"ignore": [
Expand Down
25 changes: 20 additions & 5 deletions core/project/__tests__/project.test.js
Expand Up @@ -144,11 +144,26 @@ describe("Project", () => {
const cwd = await initFixture("extends-deprecated");
const project = new Project(cwd);

expect(project.config).not.toHaveProperty("commands");
expect(project.config).not.toHaveProperty("command.publish.ignore");
expect(project.config).toHaveProperty("command.publish.ignoreChanges", ["ignored-file"]);
expect(project.config).toHaveProperty("command.publish.loglevel", "success");
expect(project.config).toHaveProperty("command.bootstrap.hoist", true);
expect(project.config).toMatchInlineSnapshot(`
Object {
"command": Object {
"bootstrap": Object {
"hoist": true,
},
"publish": Object {
"bump": "prerelease",
"ignoreChanges": Array [
"ignored-file",
],
"loglevel": "success",
},
},
"packages": Array [
"recursive-pkgs/*",
],
"version": "1.0.0",
}
`);
});

it("throws an error when extend target is unresolvable", async () => {
Expand Down
33 changes: 22 additions & 11 deletions core/project/lib/deprecate-config.js
Expand Up @@ -6,7 +6,7 @@ const path = require("path");

module.exports = compose(
// add new predicates HERE
remap("command.publish.cdVersion", "command.publish.bump"),
remap("command.publish.cdVersion", "command.publish.bump", { alsoRoot: true }),
remap("command.publish.ignore", "command.publish.ignoreChanges"),
remap("commands", "command"),
(config, filepath) => ({ config, filepath })
Expand All @@ -17,21 +17,32 @@ module.exports = compose(
* The returned predicate mutates the `config` parameter.
*
* @param {String} search Path to deprecated option
* @param {String} replace Path of renamed option
* @param {String} target Path of renamed option
* @param {Object} opts Optional configuration object
* @param {Boolean} opts.alsoRoot Whether to check root config as well
* @return {Function} predicate accepting (config, filepath)
*/
function remap(search, replace) {
function remap(search, target, { alsoRoot } = {}) {
const pathsToSearch = [search];

if (alsoRoot) {
// root config is overwritten by "more specific" nested config
pathsToSearch.unshift(search.split(".").pop());
}

return obj => {
if (dotProp.has(obj.config, search)) {
const localPath = path.relative(".", obj.filepath);
for (const searchPath of pathsToSearch) {
if (dotProp.has(obj.config, searchPath)) {
const localPath = path.relative(".", obj.filepath);

log.warn(
"project",
`Deprecated key "${search}" found in ${localPath}\nPlease rename "${search}" => "${replace}"`
);
log.warn(
"project",
`Deprecated key "${searchPath}" found in ${localPath}\nPlease rename "${searchPath}" => "${target}"`
);

dotProp.set(obj.config, replace, dotProp.get(obj.config, search));
dotProp.delete(obj.config, search);
dotProp.set(obj.config, target, dotProp.get(obj.config, searchPath));
dotProp.delete(obj.config, searchPath);
}
}

return obj;
Expand Down

0 comments on commit 7a65a87

Please sign in to comment.