Skip to content

Commit

Permalink
feat: add support for Nx monorepos via @commitlint/config-nx-scopes (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
fguitton committed Jan 25, 2022
1 parent 78a8935 commit 11879ad
Show file tree
Hide file tree
Showing 21 changed files with 525 additions and 5 deletions.
3 changes: 3 additions & 0 deletions @alias/commitlint-config-nx-scopes/README.md
@@ -0,0 +1,3 @@
# commitlint-config-nx-scopes

> Alias of [@commitlint/config-nx-scopes](https://www.npmjs.com/package/@commitlint/config-nx-scopes)
1 change: 1 addition & 0 deletions @alias/commitlint-config-nx-scopes/index.js
@@ -0,0 +1 @@
module.exports = require('@commitlint/config-nx-scopes');
21 changes: 21 additions & 0 deletions @alias/commitlint-config-nx-scopes/license.md
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2022 - present Florian Guitton

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
39 changes: 39 additions & 0 deletions @alias/commitlint-config-nx-scopes/package.json
@@ -0,0 +1,39 @@
{
"name": "commitlint-config-nx-scopes",
"version": "16.0.0",
"description": "Shareable commitlint config enforcing nx project names as scopes",
"files": [
"index.js"
],
"scripts": {
"deps": "dep-check",
"pkg": "pkg-check"
},
"repository": {
"type": "git",
"url": "https://github.com/conventional-changelog/commitlint.git",
"directory": "@alias/commitlint-config-nx-scopes"
},
"keywords": [
"conventional-changelog",
"commitlint",
"commitlint-config",
"nx"
],
"author": "Florian Guitton <florian@secretarium.com>",
"license": "MIT",
"bugs": {
"url": "https://github.com/conventional-changelog/commitlint/issues"
},
"homepage": "https://commitlint.js.org/",
"engines": {
"node": ">=v12"
},
"dependencies": {
"@commitlint/config-nx-scopes": "^16.0.0"
},
"devDependencies": {
"@commitlint/utils": "^16.0.0"
},
"gitHead": "70f7f4688b51774e7ac5e40e896cdaa3f132b2bc"
}
40 changes: 40 additions & 0 deletions @commitlint/config-nx-scopes/fixtures/basic/nx.json
@@ -0,0 +1,40 @@
{
"extends": "@nrwl/workspace/presets/core.json",
"npmScope": "secretarium",
"affected": {
"defaultBase": "main"
},
"cli": {
"defaultCollection": "@nrwl/react"
},
"tasksRunnerOptions": {
"default": {
"runner": "@nrwl/nx-cloud",
"options": {
"cacheableOperations": [
"build",
"lint",
"test",
"e2e"
]
}
}
},
"generators": {
"@nrwl/react": {
"application": {
"style": "css",
"linter": "eslint",
"babel": true
},
"component": {
"style": "css"
},
"library": {
"style": "css",
"linter": "eslint"
}
}
},
"defaultProject": "a"
}
4 changes: 4 additions & 0 deletions @commitlint/config-nx-scopes/fixtures/basic/nx/a/package.json
@@ -0,0 +1,4 @@
{
"name": "a",
"version": "1.0.0"
}
29 changes: 29 additions & 0 deletions @commitlint/config-nx-scopes/fixtures/basic/nx/a/project.json
@@ -0,0 +1,29 @@
{
"root": "nx/a",
"sourceRoot": "nx/a/src",
"projectType": "library",
"targets": {
"lint": {
"executor": "@nrwl/linter:eslint",
"outputs": [
"{options.outputFile}"
],
"options": {
"lintFilePatterns": [
"nx/a/**/*.ts"
]
}
},
"test": {
"executor": "@nrwl/jest:jest",
"outputs": [
"coverage/nx/a"
],
"options": {
"jestConfig": "nx/a/jest.config.js",
"passWithNoTests": true
}
}
},
"tags": []
}
4 changes: 4 additions & 0 deletions @commitlint/config-nx-scopes/fixtures/basic/nx/b/package.json
@@ -0,0 +1,4 @@
{
"name": "b",
"version": "1.0.0"
}
29 changes: 29 additions & 0 deletions @commitlint/config-nx-scopes/fixtures/basic/nx/b/project.json
@@ -0,0 +1,29 @@
{
"root": "nx/b",
"sourceRoot": "nx/b/src",
"projectType": "library",
"targets": {
"lint": {
"executor": "@nrwl/linter:eslint",
"outputs": [
"{options.outputFile}"
],
"options": {
"lintFilePatterns": [
"nx/b/**/*.ts"
]
}
},
"test": {
"executor": "@nrwl/jest:jest",
"outputs": [
"coverage/nx/b"
],
"options": {
"jestConfig": "nx/b/jest.config.js",
"passWithNoTests": true
}
}
},
"tags": []
}
7 changes: 7 additions & 0 deletions @commitlint/config-nx-scopes/fixtures/basic/package.json
@@ -0,0 +1,7 @@
{
"name": "basic",
"version": "1.0.0",
"devDependencies": {
"@nrwl/tao": "^13.0.0"
}
}
7 changes: 7 additions & 0 deletions @commitlint/config-nx-scopes/fixtures/basic/workspace.json
@@ -0,0 +1,7 @@
{
"version": 2,
"projects": {
"a": "nx/a",
"b": "nx/b"
}
}
37 changes: 37 additions & 0 deletions @commitlint/config-nx-scopes/fixtures/empty/nx.json
@@ -0,0 +1,37 @@
{
"npmScope": "empty",
"affected": {
"defaultBase": "main"
},
"cli": {
"defaultCollection": "@nrwl/workspace"
},
"implicitDependencies": {
"package.json": {
"dependencies": "*",
"devDependencies": "*"
},
".eslintrc.json": "*"
},
"tasksRunnerOptions": {
"default": {
"runner": "@nrwl/workspace/tasks-runners/default",
"options": {
"cacheableOperations": [
"build",
"lint",
"test",
"e2e"
]
}
}
},
"targetDependencies": {
"build": [
{
"target": "build",
"projects": "dependencies"
}
]
}
}
7 changes: 7 additions & 0 deletions @commitlint/config-nx-scopes/fixtures/empty/package.json
@@ -0,0 +1,7 @@
{
"name": "empty",
"version": "1.0.0",
"devDependencies": {
"@nrwl/tao": "^13.0.0"
}
}
4 changes: 4 additions & 0 deletions @commitlint/config-nx-scopes/fixtures/empty/workspace.json
@@ -0,0 +1,4 @@
{
"version": 2,
"projects": {}
}
31 changes: 31 additions & 0 deletions @commitlint/config-nx-scopes/index.js
@@ -0,0 +1,31 @@
const {Workspaces} = require('@nrwl/tao/src/shared/workspace');

module.exports = {
utils: {getProjects},
rules: {
'scope-enum': (ctx) =>
getProjects(ctx).then((packages) => [2, 'always', packages]),
},
};

function getProjects(context) {
return Promise.resolve()
.then(() => {
const ctx = context || {};
const cwd = ctx.cwd || process.cwd();
const ws = new Workspaces(cwd);
const workspace = ws.readWorkspaceConfiguration();
return Object.entries(workspace.projects || {}).map(
([name, project]) => ({
name,
...project,
})
);
})
.then((projects) => {
return projects
.filter((project) => project.targets)
.map((project) => project.name)
.map((name) => (name.charAt(0) === '@' ? name.split('/')[1] : name));
});
}
60 changes: 60 additions & 0 deletions @commitlint/config-nx-scopes/index.test.js
@@ -0,0 +1,60 @@
import {npm} from '@commitlint/test';
import config from '.';

test('exports rules key', () => {
expect(config).toHaveProperty('rules');
});

test('rules hold object', () => {
expect(config).toMatchObject({
rules: expect.any(Object),
});
});

test('rules contain scope-enum', () => {
expect(config).toMatchObject({
rules: {
'scope-enum': expect.anything(),
},
});
});

test('scope-enum is function', () => {
expect(config).toMatchObject({
rules: {
'scope-enum': expect.any(Function),
},
});
});

test('scope-enum does not throw for missing context', async () => {
const {'scope-enum': fn} = config.rules;
await expect(fn()).resolves.toBeTruthy();
});

test('scope-enum has expected severity', async () => {
const {'scope-enum': fn} = config.rules;
const [severity] = await fn();
expect(severity).toBe(2);
});

test('scope-enum has expected modifier', async () => {
const {'scope-enum': fn} = config.rules;
const [, modifier] = await fn();
expect(modifier).toBe('always');
});

test('returns empty value for empty nx repository', async () => {
const {'scope-enum': fn} = config.rules;
const cwd = await npm.bootstrap('fixtures/empty', __dirname);
const [, , value] = await fn({cwd});
expect(value).toEqual([]);
});

test('returns expected value for basic nx repository', async () => {
const {'scope-enum': fn} = config.rules;
const cwd = await npm.bootstrap('fixtures/basic', __dirname);

const [, , value] = await fn({cwd});
expect(value).toEqual(['a', 'b']);
});
21 changes: 21 additions & 0 deletions @commitlint/config-nx-scopes/license.md
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2022 - present Florian Guitton

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

0 comments on commit 11879ad

Please sign in to comment.