Skip to content

Commit

Permalink
feat(config-lerna-scopes): support yarn workspaces (#2149)
Browse files Browse the repository at this point in the history
* feat(config-lerna-scopes): support yarn workspaces

* feat(config-lerna-scopes): support yarn workspaces
  • Loading branch information
sergioramos authored Jan 11, 2021
1 parent 78f02fd commit b244246
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 3 deletions.
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"
},
"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

0 comments on commit b244246

Please sign in to comment.