From 92e33263fcef3545ae3b2db93f5668629a38496b Mon Sep 17 00:00:00 2001 From: Jay Tavares Date: Fri, 29 Apr 2022 13:28:27 -0400 Subject: [PATCH] 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 ```