From 92e33263fcef3545ae3b2db93f5668629a38496b Mon Sep 17 00:00:00 2001 From: Jay Tavares Date: Fri, 29 Apr 2022 13:28:27 -0400 Subject: [PATCH 1/6] feat: add ability to filter Nx projects in @commitlint/config-nx-scopes Solves: #3152 --- @commitlint/config-nx-scopes/index.js | 11 ++++++++++- @commitlint/config-nx-scopes/readme.md | 22 ++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/@commitlint/config-nx-scopes/index.js b/@commitlint/config-nx-scopes/index.js index 491d92ffdf..8c2fd909d7 100644 --- a/@commitlint/config-nx-scopes/index.js +++ b/@commitlint/config-nx-scopes/index.js @@ -8,7 +8,7 @@ module.exports = { }, }; -function getProjects(context) { +function getProjects(context, filterFunc) { return Promise.resolve() .then(() => { const ctx = context || {}; @@ -24,6 +24,15 @@ function getProjects(context) { }) .then((projects) => { return projects + .filter((project) => + filterFunc + ? filterFunc({ + name: project.name, + type: project.projectType, + tags: project.tags, + }) + : true + ) .filter((project) => project.targets) .map((project) => project.name) .map((name) => (name.charAt(0) === '@' ? name.split('/')[1] : name)); diff --git a/@commitlint/config-nx-scopes/readme.md b/@commitlint/config-nx-scopes/readme.md index ad3e92c762..489b09c9ff 100644 --- a/@commitlint/config-nx-scopes/readme.md +++ b/@commitlint/config-nx-scopes/readme.md @@ -12,6 +12,28 @@ npm install --save-dev @commitlint/config-nx-scopes @commitlint/cli echo "module.exports = {extends: ['@commitlint/config-nx-scopes']};" > commitlint.config.js ``` +## Filtering projects by type + +You can filter projects by type by specifying the project type parameter. + +In your .commitlintrc.js file: + +```javascript +const { + utils: {getProjects}, +} = require('@commitlint/config-nx-scopes'); + +module.exports = { + rules: { + 'scope-enum': async (ctx) => [ + 2, + 'always', + [...(await getProjects(ctx, 'application'))], // ⬅ or 'library' + ], + }, +}; +``` + ## Examples ``` From dcc7ac403248bb9dd3cdc35fb57f67d0272291e4 Mon Sep 17 00:00:00 2001 From: Jay Tavares Date: Tue, 10 May 2022 13:29:10 -0400 Subject: [PATCH 2/6] docs: update example usage of project filtering with @commitlint/config-nx-scopes --- @commitlint/config-nx-scopes/readme.md | 40 ++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/@commitlint/config-nx-scopes/readme.md b/@commitlint/config-nx-scopes/readme.md index 489b09c9ff..529cbf12c1 100644 --- a/@commitlint/config-nx-scopes/readme.md +++ b/@commitlint/config-nx-scopes/readme.md @@ -12,9 +12,37 @@ npm install --save-dev @commitlint/config-nx-scopes @commitlint/cli echo "module.exports = {extends: ['@commitlint/config-nx-scopes']};" > commitlint.config.js ``` -## Filtering projects by type +## Filtering projects -You can filter projects by type by specifying the project type parameter. +You can filter projects by providing a filter function as the second parameter to `getProjects()`. The function will be called with each projects' `name`, `type`, and `tags`. Simply return a boolean to indicate whether the project should be included or not. + +As an example, the following code demonstrates how to select only applications that are not end-to-end tests. + +In your .commitlintrc.js file: + +```javascript +const { + utils: {getProjects}, +} = require('@commitlint/config-nx-scopes'); + +module.exports = { + rules: { + 'scope-enum': async (ctx) => [ + 2, + 'always', + [ + ...(await getProjects( + ctx, + ({name, type}) => !name.includes('e2e') && type == 'application' + )), + ], + ], + }, + // . . . +}; +``` + +Here is another example where projects tagged with 'stage:end-of-life' are not allowed to be used as the scope for a commit. In your .commitlintrc.js file: @@ -28,9 +56,15 @@ module.exports = { 'scope-enum': async (ctx) => [ 2, 'always', - [...(await getProjects(ctx, 'application'))], // ⬅ or 'library' + [ + ...(await getProjects( + ctx, + ({tags}) => !tags.includes('stage:end-of-life') + )), + ], ], }, + // . . . }; ``` From c49195f482f3b58f598d872a31e968c3a49368eb Mon Sep 17 00:00:00 2001 From: Jay Tavares Date: Tue, 10 May 2022 13:51:22 -0400 Subject: [PATCH 3/6] docs: clarify filterFunc api --- @commitlint/config-nx-scopes/readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/@commitlint/config-nx-scopes/readme.md b/@commitlint/config-nx-scopes/readme.md index 529cbf12c1..65b39bd878 100644 --- a/@commitlint/config-nx-scopes/readme.md +++ b/@commitlint/config-nx-scopes/readme.md @@ -14,7 +14,7 @@ echo "module.exports = {extends: ['@commitlint/config-nx-scopes']};" > commitlin ## Filtering projects -You can filter projects by providing a filter function as the second parameter to `getProjects()`. The function will be called with each projects' `name`, `type`, and `tags`. Simply return a boolean to indicate whether the project should be included or not. +You can filter projects by providing a filter function as the second parameter to `getProjects()`. The function will be called with an object containing each projects' `name`, `type`, and `tags`. Simply return a boolean to indicate whether the project should be included or not. As an example, the following code demonstrates how to select only applications that are not end-to-end tests. From cb5745346b7e76d7955ce79f754f223ae51457c3 Mon Sep 17 00:00:00 2001 From: Jay Tavares Date: Tue, 10 May 2022 15:43:05 -0400 Subject: [PATCH 4/6] refactor: adjust config-nx-scopes filtering implementation --- @commitlint/config-nx-scopes/index.js | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/@commitlint/config-nx-scopes/index.js b/@commitlint/config-nx-scopes/index.js index 8c2fd909d7..2e8bbac110 100644 --- a/@commitlint/config-nx-scopes/index.js +++ b/@commitlint/config-nx-scopes/index.js @@ -8,7 +8,10 @@ module.exports = { }, }; -function getProjects(context, filterFunc) { +/** + * @param {(params: Pick) => boolean} selector + */ +function getProjects(context, selector = () => true) { return Promise.resolve() .then(() => { const ctx = context || {}; @@ -25,13 +28,11 @@ function getProjects(context, filterFunc) { .then((projects) => { return projects .filter((project) => - filterFunc - ? filterFunc({ - name: project.name, - type: project.projectType, - tags: project.tags, - }) - : true + selector({ + name: project.name, + projectType: project.projectType, + tags: project.tags, + }) ) .filter((project) => project.targets) .map((project) => project.name) From 4c9e1e8f8ab77c75a4cd00b40b969ba55e57ea14 Mon Sep 17 00:00:00 2001 From: Jay Tavares Date: Tue, 10 May 2022 15:48:19 -0400 Subject: [PATCH 5/6] docs: correct config-nx-scopes project type filtering syntax --- @commitlint/config-nx-scopes/readme.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/@commitlint/config-nx-scopes/readme.md b/@commitlint/config-nx-scopes/readme.md index 65b39bd878..4ab2f8be58 100644 --- a/@commitlint/config-nx-scopes/readme.md +++ b/@commitlint/config-nx-scopes/readme.md @@ -33,7 +33,8 @@ module.exports = { [ ...(await getProjects( ctx, - ({name, type}) => !name.includes('e2e') && type == 'application' + ({name, projectType}) => + !name.includes('e2e') && projectType == 'application' )), ], ], From 53251a745bd62bd4958feda6faf0786597da8813 Mon Sep 17 00:00:00 2001 From: Jay Tavares Date: Tue, 10 May 2022 22:20:31 -0400 Subject: [PATCH 6/6] docs: fix typo in config-nx-scopes readme --- @commitlint/config-nx-scopes/readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/@commitlint/config-nx-scopes/readme.md b/@commitlint/config-nx-scopes/readme.md index 4ab2f8be58..00027dd92e 100644 --- a/@commitlint/config-nx-scopes/readme.md +++ b/@commitlint/config-nx-scopes/readme.md @@ -14,7 +14,7 @@ echo "module.exports = {extends: ['@commitlint/config-nx-scopes']};" > commitlin ## Filtering projects -You can filter projects by providing a filter function as the second parameter to `getProjects()`. The function will be called with an object containing each projects' `name`, `type`, and `tags`. Simply return a boolean to indicate whether the project should be included or not. +You can filter projects by providing a filter function as the second parameter to `getProjects()`. The function will be called with an object containing each projects' `name`, `projectType`, and `tags`. Simply return a boolean to indicate whether the project should be included or not. As an example, the following code demonstrates how to select only applications that are not end-to-end tests.