Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support lerna v3 #406

Merged
merged 6 commits into from
Aug 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion @commitlint/config-lerna-scopes/fixtures/basic/lerna.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"lerna": "2.0.0",
"lerna": "3.2.1",
"version": "1.0.0",
"packages": [
"packages/*"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
"name": "basic",
"version": "1.0.0",
"devDependencies": {
"lerna": "2.9.0"
"lerna": "3.2.1"
}
}
2 changes: 1 addition & 1 deletion @commitlint/config-lerna-scopes/fixtures/empty/lerna.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"lerna": "2.0.0",
"lerna": "3.2.1",
"version": "1.0.0",
"packages": [
"packages/*"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
"name": "empty",
"version": "1.0.0",
"devDependencies": {
"lerna": "2.9.0"
"lerna": "3.2.1"
}
}
7 changes: 0 additions & 7 deletions @commitlint/config-lerna-scopes/fixtures/lerna-2.0/lerna.json

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
"name": "version-mismatch",
"version": "1.0.0",
"devDependencies": {
"lerna": "2.9.0"
"lerna": "2.4.0"
}
}
2 changes: 1 addition & 1 deletion @commitlint/config-lerna-scopes/fixtures/scoped/lerna.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"lerna": "2.0.0",
"lerna": "3.2.1",
"version": "1.0.0",
"packages": [
"@packages/*"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
"name": "scoped",
"version": "1.0.0",
"devDependencies": {
"lerna": "2.9.0"
"lerna": "3.2.1"
}
}

This file was deleted.

45 changes: 32 additions & 13 deletions @commitlint/config-lerna-scopes/index.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,45 @@
const Path = require('path');
const importFrom = require('import-from');
const resolvePkg = require('resolve-pkg');
const semver = require('semver');

module.exports = {
utils: {getPackages},
rules: {
'scope-enum': ctx => [2, 'always', getPackages(ctx)]
'scope-enum': ctx =>
getPackages(ctx).then(packages => [2, 'always', packages])
}
};

function getPackages(context) {
const ctx = context || {};
const cwd = ctx.cwd || process.cwd();
return Promise.resolve()
.then(() => {
const ctx = context || {};
const cwd = ctx.cwd || process.cwd();
const lernaVersion = getLernaVersion(cwd);

const Repository = importFrom(cwd, 'lerna/lib/Repository');
const PackageUtilities = importFrom(cwd, 'lerna/lib/PackageUtilities');
if (semver.lt(lernaVersion, '3.0.0')) {
const Repository = importFrom(cwd, 'lerna/lib/Repository');
const PackageUtilities = importFrom(cwd, 'lerna/lib/PackageUtilities');

const repository = new Repository(cwd);
const packages = PackageUtilities.getPackages({
packageConfigs: repository.packageConfigs,
rootPath: cwd
});
const repository = new Repository(cwd);
return PackageUtilities.getPackages({
packageConfigs: repository.packageConfigs,
rootPath: cwd
});
}

return packages
.map(pkg => pkg.name)
.map(name => (name.charAt(0) === '@' ? name.split('/')[1] : name));
const Project = importFrom(cwd, '@lerna/project');
const project = new Project(cwd);
return project.getPackages();
})
.then(packages => {
return packages
.map(pkg => pkg.name)
.map(name => (name.charAt(0) === '@' ? name.split('/')[1] : name));
});
}

function getLernaVersion(cwd) {
return require(Path.join(resolvePkg('lerna', {cwd}), 'package.json')).version;
}
8 changes: 7 additions & 1 deletion @commitlint/config-lerna-scopes/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,20 @@
"url": "https://github.com/marionebl/commitlint/issues"
},
"homepage": "https://github.com/marionebl/commitlint#readme",
"peerDependencies": {
"lerna": "^3.0.0"
},
"dependencies": {
"import-from": "2.1.0",
"lerna": "2.9.0"
"resolve-pkg": "^1.0.0",
"semver": "^5.5.1"
},
"devDependencies": {
"@commitlint/test": "^7.0.0",
"@commitlint/utils": "^7.0.0",
"@lerna/project": "^3.0.0",
"ava": "0.22.0",
"lerna": "3.1.1",
"xo": "0.20.3"
}
}
46 changes: 17 additions & 29 deletions @commitlint/config-lerna-scopes/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,63 +19,51 @@ test('scope-enum is function', t => {
t.is(typeof fn, 'function');
});

test('scope-enum does not throw for missing context', t => {
test('scope-enum does not throw for missing context', async t => {
const {'scope-enum': fn} = config.rules;
t.notThrows(() => fn());
try {
await fn();
t.pass();
} catch (err) {
t.fail();
}
});

test('scope-enum has expected severity', t => {
test('scope-enum has expected severity', async t => {
const {'scope-enum': fn} = config.rules;
const [severity] = fn();
const [severity] = await fn();
t.is(severity, 2);
});

test('scope-enum has expected modifier', t => {
test('scope-enum has expected modifier', async t => {
const {'scope-enum': fn} = config.rules;
const [, modifier] = fn();
const [, modifier] = await fn();
t.is(modifier, 'always');
});

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

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

test.failing(
'throws for repository with .lerna vs .devDependencies.lerna mismatch',
async t => {
const {'scope-enum': fn} = config.rules;
const cwd = await npm.bootstrap('fixtures/version-mismatch');
await t.throws(() => fn({cwd}));
}
);

test('returns expected value for scoped lerna repository', async t => {
const {'scope-enum': fn} = config.rules;
const cwd = await npm.bootstrap('fixtures/scoped');
const [, , value] = fn({cwd});
t.deepEqual(value, ['a', 'b']);
});

test('works with lerna 2.0', async t => {
const {'scope-enum': fn} = config.rules;
const cwd = await npm.bootstrap('fixtures/lerna-2.4');
const [, , value] = fn({cwd});
const [, , value] = await fn({cwd});
t.deepEqual(value, ['a', 'b']);
});

test('works with lerna 2.4', async t => {
test('works with lerna version < 3', async t => {
const {'scope-enum': fn} = config.rules;
const cwd = await npm.bootstrap('fixtures/lerna-2.4');
const [, , value] = fn({cwd});
t.deepEqual(value, ['a', 'b']);
const cwd = await npm.bootstrap('fixtures/lerna-two');
await t.notThrows(async () => fn({cwd}));
});
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"lerna": "2.5.1",
"lerna": "3.1.1",
"npmClient": "yarn",
"useWorkspaces": true,
"version": "7.0.1"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
"eslint": "4.18.1",
"eslint-config-prettier": "2.9.0",
"husky": "0.14.3",
"lerna": "2.9.0",
"lerna": "3.1.1",
"lint-staged": "6.1.1",
"npx": "9.7.1",
"prettier": "1.10.2",
Expand Down