From b2442469afe3b11c20b0101be7656ced43fab366 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Ramos?= Date: Mon, 11 Jan 2021 07:24:32 +0000 Subject: [PATCH] feat(config-lerna-scopes): support yarn workspaces (#2149) * feat(config-lerna-scopes): support yarn workspaces * feat(config-lerna-scopes): support yarn workspaces --- .../fixtures/yarn/@packages/a/package.json | 4 ++++ .../fixtures/yarn/@packages/b/package.json | 4 ++++ .../fixtures/yarn/package.json | 10 ++++++++++ @commitlint/config-lerna-scopes/index.js | 16 +++++++++++++++- @commitlint/config-lerna-scopes/index.test.js | 8 ++++++++ @commitlint/config-lerna-scopes/package.json | 3 ++- @commitlint/config-lerna-scopes/readme.md | 2 +- 7 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 @commitlint/config-lerna-scopes/fixtures/yarn/@packages/a/package.json create mode 100644 @commitlint/config-lerna-scopes/fixtures/yarn/@packages/b/package.json create mode 100644 @commitlint/config-lerna-scopes/fixtures/yarn/package.json diff --git a/@commitlint/config-lerna-scopes/fixtures/yarn/@packages/a/package.json b/@commitlint/config-lerna-scopes/fixtures/yarn/@packages/a/package.json new file mode 100644 index 0000000000..7ba283037e --- /dev/null +++ b/@commitlint/config-lerna-scopes/fixtures/yarn/@packages/a/package.json @@ -0,0 +1,4 @@ +{ + "name": "@packages/a", + "version": "1.0.0" +} diff --git a/@commitlint/config-lerna-scopes/fixtures/yarn/@packages/b/package.json b/@commitlint/config-lerna-scopes/fixtures/yarn/@packages/b/package.json new file mode 100644 index 0000000000..3fd2cf7616 --- /dev/null +++ b/@commitlint/config-lerna-scopes/fixtures/yarn/@packages/b/package.json @@ -0,0 +1,4 @@ +{ + "name": "@packages/b", + "version": "1.0.0" +} diff --git a/@commitlint/config-lerna-scopes/fixtures/yarn/package.json b/@commitlint/config-lerna-scopes/fixtures/yarn/package.json new file mode 100644 index 0000000000..f87bb9b6ac --- /dev/null +++ b/@commitlint/config-lerna-scopes/fixtures/yarn/package.json @@ -0,0 +1,10 @@ +{ + "name": "yarn", + "version": "1.0.0", + "devDependencies": { + "lerna": "^3.0.0" + }, + "workspaces": [ + "@packages/*" + ] +} diff --git a/@commitlint/config-lerna-scopes/index.js b/@commitlint/config-lerna-scopes/index.js index 8ad84c3502..febcdadde9 100644 --- a/@commitlint/config-lerna-scopes/index.js +++ b/@commitlint/config-lerna-scopes/index.js @@ -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 = { @@ -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'); diff --git a/@commitlint/config-lerna-scopes/index.test.js b/@commitlint/config-lerna-scopes/index.test.js index c822173e52..c1214327b5 100644 --- a/@commitlint/config-lerna-scopes/index.test.js +++ b/@commitlint/config-lerna-scopes/index.test.js @@ -1,3 +1,4 @@ +import Path from 'path'; import {lerna} from '@commitlint/test'; import config from '.'; @@ -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']); +}); diff --git a/@commitlint/config-lerna-scopes/package.json b/@commitlint/config-lerna-scopes/package.json index 21c6169cf2..a5aacd252b 100644 --- a/@commitlint/config-lerna-scopes/package.json +++ b/@commitlint/config-lerna-scopes/package.json @@ -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" ], @@ -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" diff --git a/@commitlint/config-lerna-scopes/readme.md b/@commitlint/config-lerna-scopes/readme.md index 8ec93666e1..41ab597339 100644 --- a/@commitlint/config-lerna-scopes/readme.md +++ b/@commitlint/config-lerna-scopes/readme.md @@ -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