Skip to content

Commit 58bb47f

Browse files
backflipmarionebl
authored andcommitted
fix: support lerna v3 (#406)
* chore: update lerna to v3.0.0-rc.0 * chore: update lerna in config-lerna-scopes to 3.0.4 * chore: update lerna to 3.0.4 * chore: fixed some async tests in config-lerna-scopes * chore: update lerna to v3.1 * fix: support lerna 2, 3 simultaneously
1 parent 031ab00 commit 58bb47f

File tree

22 files changed

+1236
-319
lines changed

22 files changed

+1236
-319
lines changed

@commitlint/config-lerna-scopes/fixtures/basic/lerna.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"lerna": "2.0.0",
2+
"lerna": "3.2.1",
33
"version": "1.0.0",
44
"packages": [
55
"packages/*"

@commitlint/config-lerna-scopes/fixtures/basic/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
"name": "basic",
33
"version": "1.0.0",
44
"devDependencies": {
5-
"lerna": "2.9.0"
5+
"lerna": "3.2.1"
66
}
77
}

@commitlint/config-lerna-scopes/fixtures/empty/lerna.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"lerna": "2.0.0",
2+
"lerna": "3.2.1",
33
"version": "1.0.0",
44
"packages": [
55
"packages/*"

@commitlint/config-lerna-scopes/fixtures/empty/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
"name": "empty",
33
"version": "1.0.0",
44
"devDependencies": {
5-
"lerna": "2.9.0"
5+
"lerna": "3.2.1"
66
}
77
}

@commitlint/config-lerna-scopes/fixtures/lerna-2.0/lerna.json

-7
This file was deleted.

@commitlint/config-lerna-scopes/fixtures/lerna-2.0/package.json

-7
This file was deleted.

@commitlint/config-lerna-scopes/fixtures/lerna-2.0/packages/a/package.json

-4
This file was deleted.

@commitlint/config-lerna-scopes/fixtures/lerna-2.0/packages/b/package.json

-4
This file was deleted.

@commitlint/config-lerna-scopes/fixtures/lerna-2.4/package.json

-7
This file was deleted.

@commitlint/config-lerna-scopes/fixtures/lerna-2.4/packages/a/package.json

-4
This file was deleted.

@commitlint/config-lerna-scopes/fixtures/lerna-2.4/packages/b/package.json

-4
This file was deleted.

@commitlint/config-lerna-scopes/fixtures/version-mismatch/package.json renamed to @commitlint/config-lerna-scopes/fixtures/lerna-two/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
"name": "version-mismatch",
33
"version": "1.0.0",
44
"devDependencies": {
5-
"lerna": "2.9.0"
5+
"lerna": "2.4.0"
66
}
77
}

@commitlint/config-lerna-scopes/fixtures/scoped/lerna.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"lerna": "2.0.0",
2+
"lerna": "3.2.1",
33
"version": "1.0.0",
44
"packages": [
55
"@packages/*"

@commitlint/config-lerna-scopes/fixtures/scoped/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
"name": "scoped",
33
"version": "1.0.0",
44
"devDependencies": {
5-
"lerna": "2.9.0"
5+
"lerna": "3.2.1"
66
}
77
}

@commitlint/config-lerna-scopes/fixtures/version-mismatch/lerna.json

-7
This file was deleted.
+32-13
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,45 @@
1+
const Path = require('path');
12
const importFrom = require('import-from');
3+
const resolvePkg = require('resolve-pkg');
4+
const semver = require('semver');
25

36
module.exports = {
47
utils: {getPackages},
58
rules: {
6-
'scope-enum': ctx => [2, 'always', getPackages(ctx)]
9+
'scope-enum': ctx =>
10+
getPackages(ctx).then(packages => [2, 'always', packages])
711
}
812
};
913

1014
function getPackages(context) {
11-
const ctx = context || {};
12-
const cwd = ctx.cwd || process.cwd();
15+
return Promise.resolve()
16+
.then(() => {
17+
const ctx = context || {};
18+
const cwd = ctx.cwd || process.cwd();
19+
const lernaVersion = getLernaVersion(cwd);
1320

14-
const Repository = importFrom(cwd, 'lerna/lib/Repository');
15-
const PackageUtilities = importFrom(cwd, 'lerna/lib/PackageUtilities');
21+
if (semver.lt(lernaVersion, '3.0.0')) {
22+
const Repository = importFrom(cwd, 'lerna/lib/Repository');
23+
const PackageUtilities = importFrom(cwd, 'lerna/lib/PackageUtilities');
1624

17-
const repository = new Repository(cwd);
18-
const packages = PackageUtilities.getPackages({
19-
packageConfigs: repository.packageConfigs,
20-
rootPath: cwd
21-
});
25+
const repository = new Repository(cwd);
26+
return PackageUtilities.getPackages({
27+
packageConfigs: repository.packageConfigs,
28+
rootPath: cwd
29+
});
30+
}
2231

23-
return packages
24-
.map(pkg => pkg.name)
25-
.map(name => (name.charAt(0) === '@' ? name.split('/')[1] : name));
32+
const Project = importFrom(cwd, '@lerna/project');
33+
const project = new Project(cwd);
34+
return project.getPackages();
35+
})
36+
.then(packages => {
37+
return packages
38+
.map(pkg => pkg.name)
39+
.map(name => (name.charAt(0) === '@' ? name.split('/')[1] : name));
40+
});
41+
}
42+
43+
function getLernaVersion(cwd) {
44+
return require(Path.join(resolvePkg('lerna', {cwd}), 'package.json')).version;
2645
}

@commitlint/config-lerna-scopes/package.json

+7-1
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,20 @@
3939
"url": "https://github.com/marionebl/commitlint/issues"
4040
},
4141
"homepage": "https://github.com/marionebl/commitlint#readme",
42+
"peerDependencies": {
43+
"lerna": "^3.0.0"
44+
},
4245
"dependencies": {
4346
"import-from": "2.1.0",
44-
"lerna": "2.9.0"
47+
"resolve-pkg": "^1.0.0",
48+
"semver": "^5.5.1"
4549
},
4650
"devDependencies": {
4751
"@commitlint/test": "^7.0.0",
4852
"@commitlint/utils": "^7.0.0",
53+
"@lerna/project": "^3.0.0",
4954
"ava": "0.22.0",
55+
"lerna": "3.1.1",
5056
"xo": "0.20.3"
5157
}
5258
}

@commitlint/config-lerna-scopes/test.js

+17-29
Original file line numberDiff line numberDiff line change
@@ -19,63 +19,51 @@ test('scope-enum is function', t => {
1919
t.is(typeof fn, 'function');
2020
});
2121

22-
test('scope-enum does not throw for missing context', t => {
22+
test('scope-enum does not throw for missing context', async t => {
2323
const {'scope-enum': fn} = config.rules;
24-
t.notThrows(() => fn());
24+
try {
25+
await fn();
26+
t.pass();
27+
} catch (err) {
28+
t.fail();
29+
}
2530
});
2631

27-
test('scope-enum has expected severity', t => {
32+
test('scope-enum has expected severity', async t => {
2833
const {'scope-enum': fn} = config.rules;
29-
const [severity] = fn();
34+
const [severity] = await fn();
3035
t.is(severity, 2);
3136
});
3237

33-
test('scope-enum has expected modifier', t => {
38+
test('scope-enum has expected modifier', async t => {
3439
const {'scope-enum': fn} = config.rules;
35-
const [, modifier] = fn();
40+
const [, modifier] = await fn();
3641
t.is(modifier, 'always');
3742
});
3843

3944
test('returns empty value for empty lerna repository', async t => {
4045
const {'scope-enum': fn} = config.rules;
4146
const cwd = await npm.bootstrap('fixtures/empty');
42-
const [, , value] = fn({cwd});
47+
const [, , value] = await fn({cwd});
4348
t.deepEqual(value, []);
4449
});
4550

4651
test('returns expected value for basic lerna repository', async t => {
4752
const {'scope-enum': fn} = config.rules;
4853
const cwd = await npm.bootstrap('fixtures/basic');
49-
const [, , value] = fn({cwd});
54+
const [, , value] = await fn({cwd});
5055
t.deepEqual(value, ['a', 'b']);
5156
});
5257

53-
test.failing(
54-
'throws for repository with .lerna vs .devDependencies.lerna mismatch',
55-
async t => {
56-
const {'scope-enum': fn} = config.rules;
57-
const cwd = await npm.bootstrap('fixtures/version-mismatch');
58-
await t.throws(() => fn({cwd}));
59-
}
60-
);
61-
6258
test('returns expected value for scoped lerna repository', async t => {
6359
const {'scope-enum': fn} = config.rules;
6460
const cwd = await npm.bootstrap('fixtures/scoped');
65-
const [, , value] = fn({cwd});
66-
t.deepEqual(value, ['a', 'b']);
67-
});
68-
69-
test('works with lerna 2.0', async t => {
70-
const {'scope-enum': fn} = config.rules;
71-
const cwd = await npm.bootstrap('fixtures/lerna-2.4');
72-
const [, , value] = fn({cwd});
61+
const [, , value] = await fn({cwd});
7362
t.deepEqual(value, ['a', 'b']);
7463
});
7564

76-
test('works with lerna 2.4', async t => {
65+
test('works with lerna version < 3', async t => {
7766
const {'scope-enum': fn} = config.rules;
78-
const cwd = await npm.bootstrap('fixtures/lerna-2.4');
79-
const [, , value] = fn({cwd});
80-
t.deepEqual(value, ['a', 'b']);
67+
const cwd = await npm.bootstrap('fixtures/lerna-two');
68+
await t.notThrows(async () => fn({cwd}));
8169
});

lerna.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"lerna": "2.5.1",
2+
"lerna": "3.1.1",
33
"npmClient": "yarn",
44
"useWorkspaces": true,
55
"version": "7.0.1"

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@
9696
"eslint": "4.18.1",
9797
"eslint-config-prettier": "2.9.0",
9898
"husky": "0.14.3",
99-
"lerna": "2.9.0",
99+
"lerna": "3.1.1",
100100
"lint-staged": "6.1.1",
101101
"npx": "9.7.1",
102102
"prettier": "1.10.2",

0 commit comments

Comments
 (0)