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

feat(config-lerna-scopes): support yarn workspaces #2149

Merged
merged 2 commits into from
Jan 11, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "@packages/a",
"version": "1.0.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "@packages/b",
"version": "1.0.0"
}
10 changes: 10 additions & 0 deletions @commitlint/config-lerna-scopes/fixtures/yarn/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "yarn",
"version": "1.0.0",
"devDependencies": {
"lerna": "^3.0.0"
},
"workspaces": [
"@packages/*"
]
}
16 changes: 15 additions & 1 deletion @commitlint/config-lerna-scopes/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const Path = require('path');
const importFrom = require('import-from');
const resolvePkg = require('resolve-pkg');
const Globby = require('globby');
const semver = require('semver');

module.exports = {
Expand All @@ -16,8 +17,21 @@ function getPackages(context) {
.then(() => {
const ctx = context || {};
const cwd = ctx.cwd || process.cwd();
const lernaVersion = getLernaVersion(cwd);

const {workspaces} = require(Path.join(cwd, 'package.json'));
if (Array.isArray(workspaces) && workspaces.length) {
// use yarn workspaces
return Globby(
workspaces.map((ws) => {
return Path.posix.join(ws, 'package.json');
}),
{cwd}
).then((pJsons = []) => {
return pJsons.map((pJson) => require(Path.join(cwd, pJson)));
});
}

const lernaVersion = getLernaVersion(cwd);
if (semver.lt(lernaVersion, '3.0.0')) {
const Repository = importFrom(cwd, 'lerna/lib/Repository');
const PackageUtilities = importFrom(cwd, 'lerna/lib/PackageUtilities');
Expand Down
8 changes: 8 additions & 0 deletions @commitlint/config-lerna-scopes/index.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Path from 'path';
import {lerna} from '@commitlint/test';
import config from '.';

Expand Down Expand Up @@ -66,3 +67,10 @@ test('returns expected value for scoped lerna repository', async () => {
const [, , value] = await fn({cwd});
expect(value).toEqual(['a', 'b']);
});

test('returns expected value for yarn workspaces', async () => {
const {'scope-enum': fn} = config.rules;
const cwd = Path.join(__dirname, 'fixtures', 'yarn');
const [, , value] = await fn({cwd});
expect(value.sort()).toEqual(['a', 'b']);
});
3 changes: 2 additions & 1 deletion @commitlint/config-lerna-scopes/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@commitlint/config-lerna-scopes",
"version": "11.0.0",
"description": "Shareable commitlint config enforcing lerna package names as scopes",
"description": "Shareable commitlint config enforcing lerna package and workspace names as scopes",
"files": [
"index.js"
],
Expand Down Expand Up @@ -32,6 +32,7 @@
"node": ">=v10.22.1"
},
"dependencies": {
"globby": "^11.0.1",
"import-from": "3.0.0",
"resolve-pkg": "2.0.0",
"semver": "7.3.4"
Expand Down
2 changes: 1 addition & 1 deletion @commitlint/config-lerna-scopes/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# @commitlint/config-lerna-scopes

Shareable `commitlint` config enforcing lerna package names as scopes.
Shareable `commitlint` config enforcing lerna package and workspace names as scopes.
Use with [@commitlint/cli](../cli) and [@commitlint/prompt-cli](../prompt-cli).

## Getting started
Expand Down