From 36343576c2f03278857f345891001f04cfdb1d03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Ramos?= Date: Mon, 28 Sep 2020 14:33:41 +0100 Subject: [PATCH 1/2] 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 | 1 + @commitlint/config-lerna-scopes/readme.md | 2 +- yarn.lock | 17 ++++++++++++++++- 8 files changed, 59 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..aade654f21 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) => + Path.join(ws.replace(/\//, Path.sep), '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..bd958393dc 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).toEqual(['a', 'b']); +}); diff --git a/@commitlint/config-lerna-scopes/package.json b/@commitlint/config-lerna-scopes/package.json index 9b75c0dc73..08e5ad27d9 100644 --- a/@commitlint/config-lerna-scopes/package.json +++ b/@commitlint/config-lerna-scopes/package.json @@ -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" diff --git a/@commitlint/config-lerna-scopes/readme.md b/@commitlint/config-lerna-scopes/readme.md index 7c4784cb74..a46ee87752 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/yarn package/workspace names as scopes. Use with [@commitlint/cli](../cli) and [@commitlint/prompt-cli](../prompt-cli). ## Getting started diff --git a/yarn.lock b/yarn.lock index 72984d47af..73abbfed3b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7156,11 +7156,26 @@ lodash.uniq@^4.5.0: resolved "https://registry.npmjs.org/lodash.uniq/-/lodash.uniq-4.5.0.tgz#d0225373aeb652adc1bc82e4945339a842754773" integrity sha1-0CJTc662Uq3BvILklFM5qEJ1R3M= -lodash@4.17.15, lodash@^3.3.1, lodash@^4.17.12, lodash@^4.17.13, lodash@^4.17.14, lodash@^4.17.15, lodash@^4.17.19, lodash@^4.17.20, lodash@^4.2.1, lodash@^4.5.1: +lodash@4.17.15: + version "4.17.15" + resolved "https://registry.npmjs.org/lodash/-/lodash-4.17.15.tgz#b447f6670a0455bbfeedd11392eff330ea097548" + integrity sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A== + +lodash@^3.3.1: + version "3.10.1" + resolved "https://registry.npmjs.org/lodash/-/lodash-3.10.1.tgz#5bf45e8e49ba4189e17d482789dfd15bd140b7b6" + integrity sha1-W/Rejkm6QYnhfUgnid/RW9FAt7Y= + +lodash@^4.17.12, lodash@^4.17.13, lodash@^4.17.14, lodash@^4.17.15, lodash@^4.17.19, lodash@^4.2.1, lodash@^4.5.1: version "4.17.19" resolved "https://registry.npmjs.org/lodash/-/lodash-4.17.19.tgz#e48ddedbe30b3321783c5b4301fbd353bc1e4a4b" integrity sha512-JNvd8XER9GQX0v2qJgsaN/mzFCNA5BRe/j8JN9d+tWyGLSodKQHKFicdwNYzWwI3wjRnaKPsGj1XkBjx/F96DQ== +lodash@^4.17.20: + version "4.17.20" + resolved "https://registry.npmjs.org/lodash/-/lodash-4.17.20.tgz#b44a9b6297bcb698f1c51a3545a2b3b368d59c52" + integrity sha512-PlhdFcillOINfeV7Ni6oF1TAEayyZBoZ8bcshTHqOYJYlrqzRK5hagpagky5o4HfCzzd1TRkXPMFq6cKk9rGmA== + log-symbols@^4.0.0: version "4.0.0" resolved "https://registry.npmjs.org/log-symbols/-/log-symbols-4.0.0.tgz#69b3cc46d20f448eccdb75ea1fa733d9e821c920" From 65085100313d092a826be1320dfdcfeb3af42981 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Ramos?= Date: Mon, 28 Sep 2020 14:33:41 +0100 Subject: [PATCH 2/2] feat(config-lerna-scopes): support yarn workspaces --- @commitlint/config-lerna-scopes/index.js | 6 +++--- @commitlint/config-lerna-scopes/index.test.js | 2 +- @commitlint/config-lerna-scopes/package.json | 2 +- @commitlint/config-lerna-scopes/readme.md | 2 +- yarn.lock | 17 +---------------- 5 files changed, 7 insertions(+), 22 deletions(-) diff --git a/@commitlint/config-lerna-scopes/index.js b/@commitlint/config-lerna-scopes/index.js index aade654f21..febcdadde9 100644 --- a/@commitlint/config-lerna-scopes/index.js +++ b/@commitlint/config-lerna-scopes/index.js @@ -22,9 +22,9 @@ function getPackages(context) { if (Array.isArray(workspaces) && workspaces.length) { // use yarn workspaces return Globby( - workspaces.map((ws) => - Path.join(ws.replace(/\//, Path.sep), 'package.json') - ), + workspaces.map((ws) => { + return Path.posix.join(ws, 'package.json'); + }), {cwd} ).then((pJsons = []) => { return pJsons.map((pJson) => require(Path.join(cwd, pJson))); diff --git a/@commitlint/config-lerna-scopes/index.test.js b/@commitlint/config-lerna-scopes/index.test.js index bd958393dc..c1214327b5 100644 --- a/@commitlint/config-lerna-scopes/index.test.js +++ b/@commitlint/config-lerna-scopes/index.test.js @@ -72,5 +72,5 @@ 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).toEqual(['a', 'b']); + expect(value.sort()).toEqual(['a', 'b']); }); diff --git a/@commitlint/config-lerna-scopes/package.json b/@commitlint/config-lerna-scopes/package.json index 08e5ad27d9..032308e73d 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" ], diff --git a/@commitlint/config-lerna-scopes/readme.md b/@commitlint/config-lerna-scopes/readme.md index a46ee87752..972e27ce54 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/yarn package/workspace 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 diff --git a/yarn.lock b/yarn.lock index 73abbfed3b..72984d47af 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7156,26 +7156,11 @@ lodash.uniq@^4.5.0: resolved "https://registry.npmjs.org/lodash.uniq/-/lodash.uniq-4.5.0.tgz#d0225373aeb652adc1bc82e4945339a842754773" integrity sha1-0CJTc662Uq3BvILklFM5qEJ1R3M= -lodash@4.17.15: - version "4.17.15" - resolved "https://registry.npmjs.org/lodash/-/lodash-4.17.15.tgz#b447f6670a0455bbfeedd11392eff330ea097548" - integrity sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A== - -lodash@^3.3.1: - version "3.10.1" - resolved "https://registry.npmjs.org/lodash/-/lodash-3.10.1.tgz#5bf45e8e49ba4189e17d482789dfd15bd140b7b6" - integrity sha1-W/Rejkm6QYnhfUgnid/RW9FAt7Y= - -lodash@^4.17.12, lodash@^4.17.13, lodash@^4.17.14, lodash@^4.17.15, lodash@^4.17.19, lodash@^4.2.1, lodash@^4.5.1: +lodash@4.17.15, lodash@^3.3.1, lodash@^4.17.12, lodash@^4.17.13, lodash@^4.17.14, lodash@^4.17.15, lodash@^4.17.19, lodash@^4.17.20, lodash@^4.2.1, lodash@^4.5.1: version "4.17.19" resolved "https://registry.npmjs.org/lodash/-/lodash-4.17.19.tgz#e48ddedbe30b3321783c5b4301fbd353bc1e4a4b" integrity sha512-JNvd8XER9GQX0v2qJgsaN/mzFCNA5BRe/j8JN9d+tWyGLSodKQHKFicdwNYzWwI3wjRnaKPsGj1XkBjx/F96DQ== -lodash@^4.17.20: - version "4.17.20" - resolved "https://registry.npmjs.org/lodash/-/lodash-4.17.20.tgz#b44a9b6297bcb698f1c51a3545a2b3b368d59c52" - integrity sha512-PlhdFcillOINfeV7Ni6oF1TAEayyZBoZ8bcshTHqOYJYlrqzRK5hagpagky5o4HfCzzd1TRkXPMFq6cKk9rGmA== - log-symbols@^4.0.0: version "4.0.0" resolved "https://registry.npmjs.org/log-symbols/-/log-symbols-4.0.0.tgz#69b3cc46d20f448eccdb75ea1fa733d9e821c920"