Skip to content

Commit 4da6318

Browse files
committed
feat(project): Map deprecated config to new namespaces
1 parent d11526a commit 4da6318

File tree

10 files changed

+119
-8
lines changed

10 files changed

+119
-8
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"packages": [
3+
"base-pkgs/*"
4+
],
5+
"command": {
6+
"publish": {
7+
"loglevel": "success"
8+
}
9+
},
10+
"version": "ignored"
11+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"extends": "./recursive.json",
3+
"commands": {
4+
"bootstrap": {
5+
"hoist": true
6+
}
7+
},
8+
"version": "1.0.0"
9+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"extends": "./base.json",
3+
"packages": [
4+
"recursive-pkgs/*"
5+
],
6+
"commands": {
7+
"publish": {
8+
"ignore": [
9+
"ignored-file"
10+
]
11+
}
12+
}
13+
}

core/project/__tests__/project.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,17 @@ describe("Project", () => {
127127
});
128128
});
129129

130+
it("renames deprecated config recursively", async () => {
131+
const cwd = await initFixture("extends-deprecated");
132+
const project = new Project(cwd);
133+
134+
expect(project.config).not.toHaveProperty("commands");
135+
expect(project.config).not.toHaveProperty("command.publish.ignore");
136+
expect(project.config).toHaveProperty("command.publish.ignoreChanges", ["ignored-file"]);
137+
expect(project.config).toHaveProperty("command.publish.loglevel", "success");
138+
expect(project.config).toHaveProperty("command.bootstrap.hoist", true);
139+
});
140+
130141
it("throws an error when extend target is unresolvable", async () => {
131142
const cwd = await initFixture("extends-unresolved");
132143

@@ -223,6 +234,16 @@ describe("Project", () => {
223234
expect(project.manifest).toBe(project.manifest);
224235
});
225236

237+
it("defaults package.json name field when absent", async () => {
238+
const cwd = await initFixture("basic");
239+
const manifestLocation = path.join(cwd, "package.json");
240+
241+
await fs.writeJSON(manifestLocation, { private: true }, { spaces: 2 });
242+
243+
const project = new Project(cwd);
244+
expect(project.manifest).toHaveProperty("name", path.basename(cwd));
245+
});
246+
226247
it("does not cache failures", async () => {
227248
const cwd = await initFixture("basic");
228249
const manifestLocation = path.join(cwd, "package.json");

core/project/index.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const writeJsonFile = require("write-json-file");
1111
const ValidationError = require("@lerna/validation-error");
1212
const Package = require("@lerna/package");
1313
const applyExtends = require("./lib/apply-extends");
14+
const deprecateConfig = require("./lib/deprecate-config");
1415

1516
class Project {
1617
constructor(cwd) {
@@ -31,11 +32,8 @@ class Project {
3132
};
3233
}
3334

34-
// normalize command-specific config namespace
35-
if (obj.config.commands) {
36-
obj.config.command = obj.config.commands;
37-
delete obj.config.commands;
38-
}
35+
// rename deprecated durable config
36+
deprecateConfig(obj.config, obj.filepath);
3937

4038
obj.config = applyExtends(obj.config, path.dirname(obj.filepath));
4139

core/project/lib/apply-extends.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const path = require("path");
44
const resolveFrom = require("resolve-from");
55
const ValidationError = require("@lerna/validation-error");
6+
const deprecateConfig = require("./deprecate-config");
67
const shallowExtend = require("./shallow-extend");
78

89
module.exports = applyExtends;
@@ -29,6 +30,8 @@ function applyExtends(config, cwd, seen = new Set()) {
2930
defaultConfig = require(pathToDefault);
3031
delete config.extends; // eslint-disable-line no-param-reassign
3132

33+
deprecateConfig(defaultConfig, pathToDefault);
34+
3235
defaultConfig = applyExtends(defaultConfig, path.dirname(pathToDefault), seen);
3336
}
3437

core/project/lib/deprecate-config.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"use strict";
2+
3+
const dotProp = require("dot-prop");
4+
const log = require("npmlog");
5+
const path = require("path");
6+
7+
module.exports = compose(
8+
// add new predicates HERE
9+
remap("command.publish.ignore", "command.publish.ignoreChanges"),
10+
remap("commands", "command"),
11+
(config, filepath) => ({ config, filepath })
12+
);
13+
14+
/**
15+
* Remap deprecated config properties, if they exist.
16+
* The returned predicate mutates the `config` parameter.
17+
*
18+
* @param {String} search Path to deprecated option
19+
* @param {String} replace Path of renamed option
20+
* @return {Function} predicate accepting (config, filepath)
21+
*/
22+
function remap(search, replace) {
23+
return obj => {
24+
if (dotProp.has(obj.config, search)) {
25+
const localPath = path.relative(".", obj.filepath);
26+
27+
log.warn(
28+
"project",
29+
`Deprecated key "${search}" found in ${localPath}\nPlease rename "${search}" => "${replace}"`
30+
);
31+
32+
dotProp.set(obj.config, replace, dotProp.get(obj.config, search));
33+
dotProp.delete(obj.config, search);
34+
}
35+
36+
return obj;
37+
};
38+
}
39+
40+
function compose(...funcs) {
41+
return funcs.reduce((a, b) => (...args) => a(b(...args)));
42+
}

core/project/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"@lerna/validation-error": "file:../validation-error",
3636
"cosmiconfig": "^4.0.0",
3737
"dedent": "^0.7.0",
38+
"dot-prop": "^4.2.0",
3839
"glob-parent": "^3.1.0",
3940
"load-json-file": "^4.0.0",
4041
"npmlog": "^4.1.2",

integration/__snapshots__/lerna-init.test.js.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ Object {
5656
5757
exports[`lerna init updates existing metadata: stderr 1`] = `
5858
lerna info version __TEST_VERSION__
59+
lerna WARN project Deprecated key "commands" found in lerna.json
60+
lerna WARN project Please rename "commands" => "command"
5961
lerna info Updating package.json
6062
lerna info Updating lerna.json
6163
lerna info Creating packages directory

package-lock.json

Lines changed: 14 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)