Skip to content

Commit

Permalink
feat(config-pnpm-scopes): implement config-pnpm-scopes (#3427)
Browse files Browse the repository at this point in the history
shareable config enforcing pnpm workspaces names as scopes
  • Loading branch information
dangreen authored Nov 21, 2022
1 parent 8f70b59 commit ca3ae8b
Show file tree
Hide file tree
Showing 16 changed files with 2,503 additions and 59 deletions.
6 changes: 6 additions & 0 deletions @commitlint/config-pnpm-scopes/fixtures/basic/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "basic",
"version": "1.0.0",
"private": true,
"devDependencies": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "a",
"version": "1.0.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "b",
"version": "1.0.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
packages:
- 'packages/*'
6 changes: 6 additions & 0 deletions @commitlint/config-pnpm-scopes/fixtures/empty/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "empty",
"version": "1.0.0",
"private": true,
"devDependencies": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
packages:
- 'packages/*'
6 changes: 6 additions & 0 deletions @commitlint/config-pnpm-scopes/fixtures/scoped/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "basic",
"version": "1.0.0",
"private": true,
"devDependencies": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "@scope/a",
"version": "1.0.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "@scope/b",
"version": "1.0.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
packages:
- 'packages/*'
26 changes: 26 additions & 0 deletions @commitlint/config-pnpm-scopes/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const {findWorkspacePackages} = require('@pnpm/find-workspace-packages');

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

function getProjects(context) {
const ctx = context || {};
const cwd = ctx.cwd || process.cwd();

return findWorkspacePackages(cwd).then((projects) => {
return projects.reduce((projects, project) => {
const name = project.manifest.name;

if (name && project.dir !== cwd) {
projects.push(name.charAt(0) === '@' ? name.split('/')[1] : name);
}

return projects;
}, []);
});
}
69 changes: 69 additions & 0 deletions @commitlint/config-pnpm-scopes/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
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 pnpm 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 pnpm 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']);
});

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

const [, , value] = await fn({cwd});

expect(value).toEqual(['a', 'b']);
});
21 changes: 21 additions & 0 deletions @commitlint/config-pnpm-scopes/license.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2022 - present Dan Onoshko

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.
50 changes: 50 additions & 0 deletions @commitlint/config-pnpm-scopes/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"name": "@commitlint/config-pnpm-scopes",
"version": "17.2.0",
"description": "Shareable commitlint config enforcing pnpm workspaces 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": "@commitlint/config-pnpm-scopes"
},
"keywords": [
"conventional-changelog",
"commitlint",
"commitlint-config",
"pnpm"
],
"author": "Dan Onoshko <danon0404@gmail.com>",
"license": "MIT",
"bugs": {
"url": "https://github.com/conventional-changelog/commitlint/issues"
},
"homepage": "https://commitlint.js.org/",
"peerDependencies": {
"@pnpm/find-workspace-packages": "^5.0.0",
"@pnpm/logger": "^5.0.0"
},
"peerDependenciesMeta": {
"@pnpm/find-workspace-packages": {
"optional": true
},
"@pnpm/logger": {
"optional": true
}
},
"engines": {
"node": ">=v14"
},
"devDependencies": {
"@commitlint/test": "^17.2.0",
"@commitlint/utils": "^17.0.0",
"@pnpm/find-workspace-packages": "^5.0.0",
"@pnpm/logger": "^5.0.0"
}
}
44 changes: 44 additions & 0 deletions @commitlint/config-pnpm-scopes/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
> Lint your pnpm workspaces project commits
# @commitlint/config-pnpm-scopes

Shareable `commitlint` config enforcing pnpm workspaces names as scopes.
Use with [@commitlint/cli](../cli) and [@commitlint/prompt-cli](../prompt-cli).

## Getting started

```
npm install --save-dev @commitlint/config-pnpm-scopes @commitlint/cli
echo "module.exports = {extends: ['@commitlint/config-pnpm-scopes']};" > commitlint.config.js
```

## Examples

```
❯ cat commitlint.config.js
{
extends: ['@commitlint/config-pnpm-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](https://conventional-changelog.github.io/commitlint/#/reference-rules) for a list of available rules.
Loading

0 comments on commit ca3ae8b

Please sign in to comment.