Skip to content

Commit

Permalink
feat(config-rush-scopes): add config for rush monorepo (#2878)
Browse files Browse the repository at this point in the history
* feat(config-rush-scopes): add config for rush monorepo

* fix(config-rush-scopes): support older NodeJS
  • Loading branch information
lembdev authored Dec 8, 2021
1 parent 2af2f23 commit befa677
Show file tree
Hide file tree
Showing 14 changed files with 1,557 additions and 0 deletions.
Empty file.
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"
}
441 changes: 441 additions & 0 deletions @commitlint/config-rush-scopes/fixtures/basic/rush.json

Large diffs are not rendered by default.

433 changes: 433 additions & 0 deletions @commitlint/config-rush-scopes/fixtures/empty/rush.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "@packages/a",
"version": "1.0.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "@packages/b",
"version": "1.0.0"
}
441 changes: 441 additions & 0 deletions @commitlint/config-rush-scopes/fixtures/scoped/rush.json

Large diffs are not rendered by default.

33 changes: 33 additions & 0 deletions @commitlint/config-rush-scopes/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const Path = require('path');
const util = require('util');
const fs = require('fs');
const jsonc = require('jsonc');

const readFilePromisifed = util.promisify(fs.readFile);

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

function getPackages(context) {
return Promise.resolve()
.then(() => {
const ctx = context || {};
const cwd = ctx.cwd || process.cwd();

return readFilePromisifed(Path.join(cwd, 'rush.json'), {encoding: 'utf8'})
.then((content) => jsonc.parse(content))
.then(({projects}) => projects)
.catch(() => []);
})
.then((packages) => {
return packages
.map((pkg) => pkg.packageName)
.filter(Boolean)
.map((name) => (name.charAt(0) === '@' ? name.split('/')[1] : name));
});
}
69 changes: 69 additions & 0 deletions @commitlint/config-rush-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 rush 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 rush 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 lerna 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-rush-scopes/license.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2016 - present Mario Nebl

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.
42 changes: 42 additions & 0 deletions @commitlint/config-rush-scopes/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"name": "@commitlint/config-rush-scopes",
"version": "15.0.0",
"description": "Shareable commitlint config enforcing rush package and workspace 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-rush-scopes"
},
"keywords": [
"conventional-changelog",
"commitlint",
"commitlint-config",
"rush"
],
"author": {
"name": "Alexander Vyzhanov",
"email": "lembdev@gmail.com"
},
"license": "MIT",
"bugs": {
"url": "https://github.com/conventional-changelog/commitlint/issues"
},
"homepage": "https://commitlint.js.org/",
"engines": {
"node": ">=v12"
},
"dependencies": {
"jsonc": "^2.0.0"
},
"devDependencies": {
"@commitlint/test": "^15.0.0",
"@commitlint/utils": "^15.0.0"
}
}
44 changes: 44 additions & 0 deletions @commitlint/config-rush-scopes/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
> Lint your rush project commits
# @commitlint/config-rush-scopes

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

## Getting started

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

## Examples

```
❯ cat commitlint.config.js
{
extends: ['@commitlint/config-rush-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.
17 changes: 17 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4325,6 +4325,11 @@ fast-levenshtein@^2.0.6, fast-levenshtein@~2.0.6:
resolved "https://registry.npmjs.org/fast-levenshtein/-/fast-levenshtein-2.0.6.tgz#3d8a5c66883a16a30ca8643e851f19baa7797917"
integrity sha1-PYpcZog6FqMMqGQ+hR8Zuqd5eRc=

fast-safe-stringify@^2.0.6:
version "2.1.1"
resolved "https://registry.npmjs.org/fast-safe-stringify/-/fast-safe-stringify-2.1.1.tgz#c406a83b6e70d9e35ce3b30a81141df30aeba884"
integrity sha512-W+KJc2dmILlPplD/H4K9l9LcAHAfPtP6BY84uVLXQ6Evcz9Lcg33Y2z1IVblT6xdY54PXYVHEv+0Wpq8Io6zkA==

fastq@^1.6.0:
version "1.13.0"
resolved "https://registry.npmjs.org/fastq/-/fastq-1.13.0.tgz#616760f88a7526bdfc596b7cab8c18938c36b98c"
Expand Down Expand Up @@ -6063,6 +6068,18 @@ json5@^1.0.1:
dependencies:
minimist "^1.2.0"

jsonc@^2.0.0:
version "2.0.0"
resolved "https://registry.npmjs.org/jsonc/-/jsonc-2.0.0.tgz#9e2a25100d164a9bb864c57517563717fa882551"
integrity sha512-B281bLCT2TRMQa+AQUQY5AGcqSOXBOKaYGP4wDzoA/+QswUfN8sODektbPEs9Baq7LGKun5jQbNFpzwGuVYKhw==
dependencies:
fast-safe-stringify "^2.0.6"
graceful-fs "^4.1.15"
mkdirp "^0.5.1"
parse-json "^4.0.0"
strip-bom "^4.0.0"
strip-json-comments "^3.0.1"

jsonfile@^4.0.0:
version "4.0.0"
resolved "https://registry.npmjs.org/jsonfile/-/jsonfile-4.0.0.tgz#8771aae0799b64076b76640fca058f9c10e33ecb"
Expand Down

0 comments on commit befa677

Please sign in to comment.