Skip to content

Commit bff709a

Browse files
committed
refactor(core): read config from commitlint.config.js
1 parent 9a7d100 commit bff709a

39 files changed

+96
-88
lines changed

.commitlintrc

-3
This file was deleted.

@commitlint/cli/cli.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ test('should fail for input from stdin with rule from rc', async t => {
5151
});
5252

5353
test('should fail for input from stdin with rule from js', async t => {
54-
const actual = await t.throws(cli('foo: bar', ['--extends', './commitlint'], {cwd: EXTENDS_ROOT}));
54+
const actual = await t.throws(cli('foo: bar', ['--extends', './extended'], {cwd: EXTENDS_ROOT}));
5555
t.true(actual.stdout.includes('scope must not be one of [foo]'));
5656
t.is(actual.code, 1);
5757
});

@commitlint/cli/fixtures/empty/.commitlintrc

-2
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = {};

@commitlint/cli/fixtures/extends-root/.commitlintrc

-1
This file was deleted.

@commitlint/cli/fixtures/simple/.commitlintrc

-5
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
rules: {
3+
'type-enum': [2, 'never', ['foo']]
4+
}
5+
};

@commitlint/cli/readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
```shell
88
npm install --save-dev @commitlint/cli @commitlint/config-angular
9-
echo '{"extends": ["@commitlint/config-angular"]}' > .commitlintrc
9+
echo "module.exports = {extends: ['@commitlint/config-angular']};" > .commitlint.config.js
1010
```
1111

1212
Consult [docs/cli](../../docs/cli) for comprehensive documentation.

@commitlint/config-angular/readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Shareable `commitlint` config enforcing the angular commit convention.
77

88
```sh
99
npm install --save-dev @commitlint/config-angular @commitlint/cli
10-
echo '{"extends": ["@commitlint/config-angular"]}' > .commitlintrc
10+
echo "module.exports = {extends: ['@commitlint/config-angular']};" > .commitlint.config.js
1111
```
1212

1313
## Rules

@commitlint/config-lerna-scopes/readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Shareable `commitlint` config enforcing lerna package names as scopes.
66
## Getting started
77
```sh
88
npm install --save-dev @commitlint/config-lerna-scopes @commitlint/cli
9-
echo '{"extends": ["@commitlint/config-lerna-scopes"]}' > .commitlintrc
9+
echo "module.exports = {extends: ['@commitlint/config-lerna-scopes']};" > .commitlint.config.js
1010
```
1111

1212
## Examples

@commitlint/config-patternplate/readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Shareable `commitlint` config enforcing the patternplate commit convention.
66
## Getting started
77
```sh
88
npm install --save-dev @commitlint/config-patternplate @commitlint/cli
9-
echo '{"extends": ["@commitlint/config-patternplate"]}' > .commitlintrc
9+
echo "module.exports = {extends: ['@commitlint/config-patternplate']};" > .commitlint.config.js
1010
```
1111

1212
## Rules

@commitlint/core/fixtures/empty-object-file/.commitlintrc

-1
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = {};

@commitlint/core/fixtures/extends-empty/.commitlintrc

-3
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
extends: []
3+
};

@commitlint/core/fixtures/extends-invalid/.commitlintrc

-3
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
extends: ['____foooooo']
3+
};

@commitlint/core/fixtures/overridden-type-enums/.commitlintrc

-6
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
extends: ['./extended'],
3+
rules: {
4+
'type-enum': [2, 'always', ['a', 'b', 'c', 'd']]
5+
}
6+
};

@commitlint/core/fixtures/overriden-legacy/.commitlintrc

-5
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
rules: {
3+
legacy: false
4+
}
5+
};

@commitlint/core/fixtures/recursive-extends/.commitlintrc

-6
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
extends: ['./first-extended'],
3+
rules: {
4+
zero: 0
5+
}
6+
};

@commitlint/core/fixtures/trash-extend/.commitlintrc

-7
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = {
2+
extends: ['./one'],
3+
zero: '0',
4+
rules: {
5+
zero: 0
6+
}
7+
};

@commitlint/core/fixtures/trash-file/.commitlintrc

-8
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = {
2+
foo: 'bar',
3+
baz: 'bar',
4+
rules: {
5+
foo: 'bar',
6+
baz: 'bar'
7+
}
8+
};

@commitlint/core/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@
136136
"franc": "2.0.0",
137137
"git-raw-commits": "1.1.2",
138138
"git-toplevel": "1.1.1",
139+
"import-from": "^2.1.0",
139140
"lodash": "4.17.4",
140141
"mz": "2.6.0",
141142
"path-exists": "^3.0.0",

@commitlint/core/src/load.js

+20-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import path from 'path';
2+
import importFrom from 'import-from';
23
import {entries, merge, mergeWith, pick} from 'lodash';
34
import rc from 'rc';
5+
import resolveFrom from 'resolve-from';
46

57
import resolveExtends from './library/resolve-extends';
68
import executeRule from './library/execute-rule';
@@ -11,20 +13,15 @@ const valid = input => pick(input, 'extends', 'rules');
1113
export default async (seed = {}) => {
1214
// Obtain config from .rc files
1315
const raw = file();
14-
const found = typeof raw.config === 'string';
15-
16-
// Use the default extends config if there is no userConfig file found
17-
// See https://git.io/vwT1C for reference
18-
const applicable = found ? {} : {extends: []};
1916

2017
// Merge passed config with file based options
2118
const config = valid(merge(raw, seed));
22-
const opts = merge({}, applicable, pick(config, 'extends'));
19+
const opts = merge({extends: [], rules: {}}, pick(config, 'extends'));
2320

2421
// Resolve extends key
2522
const extended = resolveExtends(opts, {
2623
prefix: 'commitlint-config',
27-
cwd: found ? path.dirname(raw.config) : undefined
24+
cwd: raw.config ? path.dirname(raw.config) : process.cwd()
2825
});
2926

3027
const preset = valid(mergeWith({}, extended, config, w));
@@ -63,20 +60,29 @@ function file() {
6360
const legacy = rc('conventional-changelog-lint');
6461
const legacyFound = typeof legacy.config === 'string';
6562

66-
const raw = rc('commitlint');
67-
const rawFound = typeof raw.config === 'string';
63+
const found = resolveable('./commitlint.config');
64+
const raw = found ? importFrom(process.cwd(), './commitlint.config') : {};
6865

69-
if (legacyFound && !rawFound) {
70-
console.warn(`Using legacy ${path.relative(process.cwd(), legacy.config)}. Rename to .commitlintrc to silence this warning.`);
66+
if (legacyFound && !found) {
67+
console.warn(`Using legacy ${path.relative(process.cwd(), legacy.config)}. Rename to commitlint.config.js to silence this warning.`);
7168
}
7269

73-
if (legacyFound && rawFound) {
74-
console.warn(`Ignored legacy ${path.relative(process.cwd(), legacy.config)} as ${path.relative(process.cwd(), raw.config)} superseeds it. Remove .conventional-changelog-lintrc to silence this warning.`);
70+
if (legacyFound && found) {
71+
console.warn(`Ignored legacy ${path.relative(process.cwd(), legacy.config)} as commitlint.config.js superseeds it. Remove .conventional-changelog-lintrc to silence this warning.`);
7572
}
7673

77-
if (rawFound) {
74+
if (found) {
7875
return raw;
7976
}
8077

8178
return legacy;
8279
}
80+
81+
function resolveable(id) {
82+
try {
83+
resolveFrom(process.cwd(), id);
84+
return true;
85+
} catch (err) {
86+
return false;
87+
}
88+
}

@commitlint/core/src/load.test.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ test('ignores unknow keys', async t => {
5555
t.context.back = chdir('fixtures/trash-file');
5656
const actual = await load();
5757
t.deepEqual(actual, {
58+
extends: [],
5859
rules: {
5960
foo: 'bar',
6061
baz: 'bar'
@@ -78,16 +79,18 @@ test('supports legacy .conventional-changelog-lintrc', async t => {
7879
t.context.back = chdir('fixtures/legacy');
7980
const actual = await load();
8081
t.deepEqual(actual, {
82+
extends: [],
8183
rules: {
8284
legacy: true
8385
}
8486
});
8587
});
8688

87-
test('.commitlintrc overrides .conventional-changelog-lintrc', async t => {
89+
test('commitlint.config.js overrides .conventional-changelog-lintrc', async t => {
8890
t.context.back = chdir('fixtures/overriden-legacy');
8991
const actual = await load();
9092
t.deepEqual(actual, {
93+
extends: [],
9194
rules: {
9295
legacy: false
9396
}

@commitlint/prompt-cli/readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
```bash
88
npm install --g @commitlint/prompt-cli @commitlint/config-angular
9-
echo '{"extends": ["@commitlint/config-angular"]}' > .commitlintrc
9+
echo "module.exports = {extends: ['@commitlint/config-angular']};" > .commitlint.config.js
1010
```
1111

1212
```bash

@commitlint/prompt/package.json

-2
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,7 @@
107107
"babel-polyfill": "^6.23.0",
108108
"babel-runtime": "^6.23.0",
109109
"chalk": "^1.1.1",
110-
"execa": "^0.7.0",
111110
"lodash": "^4.17.4",
112-
"meow": "^3.7.0",
113111
"throat": "^4.1.0",
114112
"vorpal": "^1.10.0"
115113
}

@commitlint/prompt/readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
```bash
88
npm install --save @commitlint/prompt @commitlint/config-angular commitizen
9-
echo '{"extends": ["@commitlint/config-angular"]}' > .commitlintrc
9+
echo "module.exports = {extends: ['@commitlint/config-angular']};" > .commitlint.config.js
1010
```
1111

1212
In package.json

commitlint.config.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
extends: ['./@commitlint/config-angular', './@commitlint/config-lerna-scopes']
3+
};

docs/shareable-config.md

+10-10
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,26 @@ This works recursively, enabling shareable configuration to extend on an indefin
88

99

1010
```js
11-
// .commitlint
12-
{
13-
"extends": ["example"] // => @commitlint-config-example
14-
}
11+
// .commitlint.config.js
12+
module.exports = {
13+
extends: ['example'] // => @commitlint-config-example
14+
};
1515
```
1616

1717
Special cases are scoped extend items those are not prefixed.
1818

1919
```js
20-
// .commitlint
21-
{
22-
"extends": ["@commitlint/config-angular"] // => @commitlint/config-angular
23-
}
20+
// .commitlint.config.js
21+
module.exports = {
22+
extends: ['@commitlint/config-angular'] // => @commitlint/config-angular
23+
};
2424
```
2525

2626
The same is true for relative imports
2727

2828
```js
2929
// .commitlint
30-
{
31-
"extends": ["./example"] // => ./example.js
30+
module.expors = {
31+
extends: ['./example'] // => ./example.js
3232
}
3333
```

docs/upgrade.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ npm install --save-dev conventional-changelog-lint@latest
1414

1515
#### Config
1616

17-
* **wildcards** config is ignored - as of version `2.0.0` the former `.wildcards` configuration is ignored entirely. If your `.commitlintrc` or an extended shareable configuration has a `.wildcards` key a warning will be issued.
17+
* **wildcards** config is ignored - as of version `2.0.0` the former `.wildcards` configuration is ignored entirely. If your `.conventional-changelog-lintrc`, `commitlint.config.js` or an extended shareable configuration has a `.wildcards` key a warning will be issued.
1818

1919
#### API
2020

@@ -36,7 +36,8 @@ mv .conventional-changelog-lintrc .commitlintrc
3636

3737
* `conventional-changelog-lint` command now is called `commitlint`
3838
* `commitlint` command now is installed via `@commitlint/cli`
39-
* `.conventional-changelog-lintrc` now is called `.commitlintrc`
39+
* `.conventional-changelog-lintrc` now is called `commitlint.config.js`
40+
* `commitlint` does not search upwards in the directory structure for config
4041
* `--preset | -p` flag was removed. The `angular` preset is used always.
4142

4243
#### Config

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616
"commit": "./@commitlint/prompt-cli/cli.js"
1717
},
1818
"xo": {
19-
"ignore": [
20-
"lib/**"
19+
"ignores": [
20+
"@commitlint/**/lib/**",
21+
"@commitlint/**/node_modules"
2122
]
2223
},
2324
"engines": {

0 commit comments

Comments
 (0)