Skip to content

Commit befa677

Browse files
authored
feat(config-rush-scopes): add config for rush monorepo (#2878)
* feat(config-rush-scopes): add config for rush monorepo * fix(config-rush-scopes): support older NodeJS
1 parent 2af2f23 commit befa677

File tree

14 files changed

+1557
-0
lines changed

14 files changed

+1557
-0
lines changed

@commitlint/config-rush-scopes/CHANGELOG.md

Whitespace-only changes.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "a",
3+
"version": "1.0.0"
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "b",
3+
"version": "1.0.0"
4+
}

@commitlint/config-rush-scopes/fixtures/basic/rush.json

Lines changed: 441 additions & 0 deletions
Large diffs are not rendered by default.

@commitlint/config-rush-scopes/fixtures/empty/rush.json

Lines changed: 433 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "@packages/a",
3+
"version": "1.0.0"
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "@packages/b",
3+
"version": "1.0.0"
4+
}

@commitlint/config-rush-scopes/fixtures/scoped/rush.json

Lines changed: 441 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const Path = require('path');
2+
const util = require('util');
3+
const fs = require('fs');
4+
const jsonc = require('jsonc');
5+
6+
const readFilePromisifed = util.promisify(fs.readFile);
7+
8+
module.exports = {
9+
utils: {getPackages},
10+
rules: {
11+
'scope-enum': (ctx) =>
12+
getPackages(ctx).then((packages) => [2, 'always', packages]),
13+
},
14+
};
15+
16+
function getPackages(context) {
17+
return Promise.resolve()
18+
.then(() => {
19+
const ctx = context || {};
20+
const cwd = ctx.cwd || process.cwd();
21+
22+
return readFilePromisifed(Path.join(cwd, 'rush.json'), {encoding: 'utf8'})
23+
.then((content) => jsonc.parse(content))
24+
.then(({projects}) => projects)
25+
.catch(() => []);
26+
})
27+
.then((packages) => {
28+
return packages
29+
.map((pkg) => pkg.packageName)
30+
.filter(Boolean)
31+
.map((name) => (name.charAt(0) === '@' ? name.split('/')[1] : name));
32+
});
33+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import {npm} from '@commitlint/test';
2+
import config from '.';
3+
4+
test('exports rules key', () => {
5+
expect(config).toHaveProperty('rules');
6+
});
7+
8+
test('rules hold object', () => {
9+
expect(config).toMatchObject({
10+
rules: expect.any(Object),
11+
});
12+
});
13+
14+
test('rules contain scope-enum', () => {
15+
expect(config).toMatchObject({
16+
rules: {
17+
'scope-enum': expect.anything(),
18+
},
19+
});
20+
});
21+
22+
test('scope-enum is function', () => {
23+
expect(config).toMatchObject({
24+
rules: {
25+
'scope-enum': expect.any(Function),
26+
},
27+
});
28+
});
29+
30+
test('scope-enum does not throw for missing context', async () => {
31+
const {'scope-enum': fn} = config.rules;
32+
await expect(fn()).resolves.toBeTruthy();
33+
});
34+
35+
test('scope-enum has expected severity', async () => {
36+
const {'scope-enum': fn} = config.rules;
37+
const [severity] = await fn();
38+
expect(severity).toBe(2);
39+
});
40+
41+
test('scope-enum has expected modifier', async () => {
42+
const {'scope-enum': fn} = config.rules;
43+
const [, modifier] = await fn();
44+
expect(modifier).toBe('always');
45+
});
46+
47+
test('returns empty value for empty rush repository', async () => {
48+
const {'scope-enum': fn} = config.rules;
49+
const cwd = await npm.bootstrap('fixtures/empty', __dirname);
50+
const [, , value] = await fn({cwd});
51+
expect(value).toEqual([]);
52+
});
53+
54+
test('returns expected value for basic rush repository', async () => {
55+
const {'scope-enum': fn} = config.rules;
56+
const cwd = await npm.bootstrap('fixtures/basic', __dirname);
57+
58+
const [, , value] = await fn({cwd});
59+
expect(value).toEqual(['a', 'b']);
60+
});
61+
62+
test('returns expected value for scoped lerna repository', async () => {
63+
const {'scope-enum': fn} = config.rules;
64+
const cwd = await npm.bootstrap('fixtures/scoped', __dirname);
65+
66+
const [, , value] = await fn({cwd});
67+
68+
expect(value).toEqual(['a', 'b']);
69+
});

0 commit comments

Comments
 (0)