Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ability to filter Nx projects in @commitlint/config-nx-scopes #3155

Merged
merged 6 commits into from May 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 11 additions & 1 deletion @commitlint/config-nx-scopes/index.js
Expand Up @@ -8,7 +8,10 @@ module.exports = {
},
};

function getProjects(context) {
/**
* @param {(params: Pick<Nx.ProjectConfiguration, 'name' | 'projectType' | 'tags'>) => boolean} selector
*/
function getProjects(context, selector = () => true) {
return Promise.resolve()
.then(() => {
const ctx = context || {};
Expand All @@ -24,6 +27,13 @@ function getProjects(context) {
})
.then((projects) => {
return projects
.filter((project) =>
selector({
name: project.name,
projectType: project.projectType,
tags: project.tags,
})
)
.filter((project) => project.targets)
.map((project) => project.name)
.map((name) => (name.charAt(0) === '@' ? name.split('/')[1] : name));
Expand Down
57 changes: 57 additions & 0 deletions @commitlint/config-nx-scopes/readme.md
Expand Up @@ -12,6 +12,63 @@ npm install --save-dev @commitlint/config-nx-scopes @commitlint/cli
echo "module.exports = {extends: ['@commitlint/config-nx-scopes']};" > commitlint.config.js
```

## 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`, `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.

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, projectType}) =>
!name.includes('e2e') && projectType == '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:

```javascript
const {
utils: {getProjects},
} = require('@commitlint/config-nx-scopes');

module.exports = {
rules: {
'scope-enum': async (ctx) => [
2,
'always',
[
...(await getProjects(
ctx,
({tags}) => !tags.includes('stage:end-of-life')
)),
],
],
},
// . . .
};
```

## Examples

```
Expand Down