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(config-pnpm-scopes): implement config-pnpm-scopes #3427

Merged
merged 1 commit into from Nov 21, 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
6 changes: 6 additions & 0 deletions @commitlint/config-pnpm-scopes/fixtures/basic/package.json
@@ -0,0 +1,6 @@
{
"name": "basic",
"version": "1.0.0",
"private": true,
"devDependencies": {}
}
@@ -0,0 +1,4 @@
{
"name": "a",
"version": "1.0.0"
}
@@ -0,0 +1,4 @@
{
"name": "b",
"version": "1.0.0"
}
@@ -0,0 +1,2 @@
packages:
- 'packages/*'
6 changes: 6 additions & 0 deletions @commitlint/config-pnpm-scopes/fixtures/empty/package.json
@@ -0,0 +1,6 @@
{
"name": "empty",
"version": "1.0.0",
"private": true,
"devDependencies": {}
}
@@ -0,0 +1,2 @@
packages:
- 'packages/*'
6 changes: 6 additions & 0 deletions @commitlint/config-pnpm-scopes/fixtures/scoped/package.json
@@ -0,0 +1,6 @@
{
"name": "basic",
"version": "1.0.0",
"private": true,
"devDependencies": {}
}
@@ -0,0 +1,4 @@
{
"name": "@scope/a",
"version": "1.0.0"
}
@@ -0,0 +1,4 @@
{
"name": "@scope/b",
"version": "1.0.0"
}
@@ -0,0 +1,2 @@
packages:
- 'packages/*'
26 changes: 26 additions & 0 deletions @commitlint/config-pnpm-scopes/index.js
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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.