Lint your nx project commits
Shareable commitlint
config enforcing nx project and workspace names as scopes.
Use with @commitlint/cli and @commitlint/prompt-cli.
npm install --save-dev @commitlint/config-nx-scopes @commitlint/cli
echo "module.exports = {extends: ['@commitlint/config-nx-scopes']};" > commitlint.config.js
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:
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:
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')
)),
],
],
},
// . . .
};
β― cat commitlint.config.js
{
extends: ['@commitlint/config-nx-scopes']
}
β― tree packages
packages
βββ api
βββ app
βββ web
β― echo "build(api): change something in api's build" | commitlint
β§ input: build(api): change something in api's build
β found 0 problems, 0 warnings
β― echo "test(foo): this won't pass" | commitlint
β§ input: test(foo): this won't pass
β scope must be one of [api, app, web] [scope-enum]
β found 1 problems, 0 warnings
β― echo "ci: do some general maintenance" | commitlint
β§ input: ci: do some general maintenance
β found 0 problems, 0 warnings
Consult docs/rules for a list of available rules.