Skip to content

Commit e595693

Browse files
authoredMay 12, 2022
feat: add ability to filter Nx projects in @commitlint/config-nx-scopes (#3155)
* feat: add ability to filter Nx projects in @commitlint/config-nx-scopes Solves: #3152 * docs: update example usage of project filtering with @commitlint/config-nx-scopes * docs: clarify filterFunc api * refactor: adjust config-nx-scopes filtering implementation * docs: correct config-nx-scopes project type filtering syntax * docs: fix typo in config-nx-scopes readme
1 parent 4209622 commit e595693

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed
 

‎@commitlint/config-nx-scopes/index.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ module.exports = {
88
},
99
};
1010

11-
function getProjects(context) {
11+
/**
12+
* @param {(params: Pick<Nx.ProjectConfiguration, 'name' | 'projectType' | 'tags'>) => boolean} selector
13+
*/
14+
function getProjects(context, selector = () => true) {
1215
return Promise.resolve()
1316
.then(() => {
1417
const ctx = context || {};
@@ -24,6 +27,13 @@ function getProjects(context) {
2427
})
2528
.then((projects) => {
2629
return projects
30+
.filter((project) =>
31+
selector({
32+
name: project.name,
33+
projectType: project.projectType,
34+
tags: project.tags,
35+
})
36+
)
2737
.filter((project) => project.targets)
2838
.map((project) => project.name)
2939
.map((name) => (name.charAt(0) === '@' ? name.split('/')[1] : name));

‎@commitlint/config-nx-scopes/readme.md

+57
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,63 @@ npm install --save-dev @commitlint/config-nx-scopes @commitlint/cli
1212
echo "module.exports = {extends: ['@commitlint/config-nx-scopes']};" > commitlint.config.js
1313
```
1414

15+
## Filtering projects
16+
17+
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.
18+
19+
As an example, the following code demonstrates how to select only applications that are not end-to-end tests.
20+
21+
In your .commitlintrc.js file:
22+
23+
```javascript
24+
const {
25+
utils: {getProjects},
26+
} = require('@commitlint/config-nx-scopes');
27+
28+
module.exports = {
29+
rules: {
30+
'scope-enum': async (ctx) => [
31+
2,
32+
'always',
33+
[
34+
...(await getProjects(
35+
ctx,
36+
({name, projectType}) =>
37+
!name.includes('e2e') && projectType == 'application'
38+
)),
39+
],
40+
],
41+
},
42+
// . . .
43+
};
44+
```
45+
46+
Here is another example where projects tagged with 'stage:end-of-life' are not allowed to be used as the scope for a commit.
47+
48+
In your .commitlintrc.js file:
49+
50+
```javascript
51+
const {
52+
utils: {getProjects},
53+
} = require('@commitlint/config-nx-scopes');
54+
55+
module.exports = {
56+
rules: {
57+
'scope-enum': async (ctx) => [
58+
2,
59+
'always',
60+
[
61+
...(await getProjects(
62+
ctx,
63+
({tags}) => !tags.includes('stage:end-of-life')
64+
)),
65+
],
66+
],
67+
},
68+
// . . .
69+
};
70+
```
71+
1572
## Examples
1673

1774
```

0 commit comments

Comments
 (0)
Please sign in to comment.