Skip to content

Commit b244246

Browse files
authored
feat(config-lerna-scopes): support yarn workspaces (#2149)
* feat(config-lerna-scopes): support yarn workspaces * feat(config-lerna-scopes): support yarn workspaces
1 parent 78f02fd commit b244246

File tree

7 files changed

+44
-3
lines changed

7 files changed

+44
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "@packages/a",
3+
"version": "1.0.0"
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "@packages/b",
3+
"version": "1.0.0"
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "yarn",
3+
"version": "1.0.0",
4+
"devDependencies": {
5+
"lerna": "^3.0.0"
6+
},
7+
"workspaces": [
8+
"@packages/*"
9+
]
10+
}

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

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const Path = require('path');
22
const importFrom = require('import-from');
33
const resolvePkg = require('resolve-pkg');
4+
const Globby = require('globby');
45
const semver = require('semver');
56

67
module.exports = {
@@ -16,8 +17,21 @@ function getPackages(context) {
1617
.then(() => {
1718
const ctx = context || {};
1819
const cwd = ctx.cwd || process.cwd();
19-
const lernaVersion = getLernaVersion(cwd);
2020

21+
const {workspaces} = require(Path.join(cwd, 'package.json'));
22+
if (Array.isArray(workspaces) && workspaces.length) {
23+
// use yarn workspaces
24+
return Globby(
25+
workspaces.map((ws) => {
26+
return Path.posix.join(ws, 'package.json');
27+
}),
28+
{cwd}
29+
).then((pJsons = []) => {
30+
return pJsons.map((pJson) => require(Path.join(cwd, pJson)));
31+
});
32+
}
33+
34+
const lernaVersion = getLernaVersion(cwd);
2135
if (semver.lt(lernaVersion, '3.0.0')) {
2236
const Repository = importFrom(cwd, 'lerna/lib/Repository');
2337
const PackageUtilities = importFrom(cwd, 'lerna/lib/PackageUtilities');

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

+8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import Path from 'path';
12
import {lerna} from '@commitlint/test';
23
import config from '.';
34

@@ -66,3 +67,10 @@ test('returns expected value for scoped lerna repository', async () => {
6667
const [, , value] = await fn({cwd});
6768
expect(value).toEqual(['a', 'b']);
6869
});
70+
71+
test('returns expected value for yarn workspaces', async () => {
72+
const {'scope-enum': fn} = config.rules;
73+
const cwd = Path.join(__dirname, 'fixtures', 'yarn');
74+
const [, , value] = await fn({cwd});
75+
expect(value.sort()).toEqual(['a', 'b']);
76+
});

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@commitlint/config-lerna-scopes",
33
"version": "11.0.0",
4-
"description": "Shareable commitlint config enforcing lerna package names as scopes",
4+
"description": "Shareable commitlint config enforcing lerna package and workspace names as scopes",
55
"files": [
66
"index.js"
77
],
@@ -32,6 +32,7 @@
3232
"node": ">=v10"
3333
},
3434
"dependencies": {
35+
"globby": "^11.0.1",
3536
"import-from": "3.0.0",
3637
"resolve-pkg": "2.0.0",
3738
"semver": "7.3.4"

@commitlint/config-lerna-scopes/readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
# @commitlint/config-lerna-scopes
44

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

88
## Getting started

0 commit comments

Comments
 (0)