Skip to content

Commit c2cbabf

Browse files
committed
feat(core): Allow to configure with json, yaml and package.json
Fix conventional-changelog#73
1 parent e6ef072 commit c2cbabf

File tree

18 files changed

+155
-25
lines changed

18 files changed

+155
-25
lines changed
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+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
extends: ['./second-extended'],
3+
rules: {
4+
one: 1
5+
}
6+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
rules: {
3+
two: 2
4+
}
5+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"extends": ["./first-extended"],
3+
"rules": {
4+
"zero": 0
5+
}
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
extends: ['./second-extended'],
3+
rules: {
4+
one: 1
5+
}
6+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
rules: {
3+
two: 2
4+
}
5+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
extends: ['./second-extended'],
3+
rules: {
4+
one: 1
5+
}
6+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
rules: {
3+
two: 2
4+
}
5+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"commitlint": {
3+
"extends": ["./first-extended"],
4+
"rules": {
5+
"zero": 0
6+
}
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
extends:
2+
- "./first-extended"
3+
rules:
4+
zero: 0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
extends: ['./second-extended'],
3+
rules: {
4+
one: 1
5+
}
6+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
rules: {
3+
two: 2
4+
}
5+
};

@commitlint/core/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@
7474
"execa": "0.6.3",
7575
"globby": "6.1.0",
7676
"has-ansi": "3.0.0",
77-
"import-from": "2.1.0",
7877
"nyc": "10.3.2",
7978
"path-exists": "3.0.0",
8079
"resolve-from": "3.0.0",
@@ -86,6 +85,7 @@
8685
"chalk": "^2.0.1",
8786
"conventional-changelog-angular": "^1.3.3",
8887
"conventional-commits-parser": "^1.3.0",
88+
"cosmiconfig": "^3.0.1",
8989
"find-up": "^2.1.0",
9090
"franc": "^2.0.0",
9191
"git-raw-commits": "^1.1.2",

@commitlint/core/src/load.js

+22-21
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import path from 'path';
2-
import importFrom from 'import-from';
32
import {entries, merge, mergeWith, pick} from 'lodash';
43
import rc from 'rc';
4+
import cosmiconfig from 'cosmiconfig';
55
import resolveFrom from 'resolve-from';
66

77
import resolveExtends from './library/resolve-extends';
@@ -13,18 +13,22 @@ const valid = input => pick(input, 'extends', 'rules', 'parserPreset');
1313
export default async (seed = {}) => {
1414
// Obtain config from .rc files
1515
const raw = file();
16-
1716
// Merge passed config with file based options
1817
const config = valid(merge(raw, seed));
1918
const opts = merge({extends: [], rules: {}}, pick(config, 'extends'));
2019

2120
// Resolve parserPreset key
2221
if (typeof config.parserPreset === 'string') {
23-
const resolvedParserPreset = resolveFrom(process.cwd(), config.parserPreset);
22+
const resolvedParserPreset = resolveFrom(
23+
process.cwd(),
24+
config.parserPreset
25+
);
2426

2527
config.parserPreset = {
2628
name: config.parserPreset,
27-
path: `./${path.posix.relative(process.cwd(), resolvedParserPreset)}`.split(path.sep).join('/'),
29+
path: `./${path.posix.relative(process.cwd(), resolvedParserPreset)}`
30+
.split(path.sep)
31+
.join('/'),
2832
opts: require(resolvedParserPreset)
2933
};
3034
}
@@ -39,7 +43,10 @@ export default async (seed = {}) => {
3943
const preset = valid(mergeWith(extended, config, w));
4044

4145
// Await parser-preset if applicable
42-
if (typeof preset.parserPreset === 'object' && typeof preset.parserPreset.opts === 'object') {
46+
if (
47+
typeof preset.parserPreset === 'object' &&
48+
typeof preset.parserPreset.opts === 'object'
49+
) {
4350
preset.parserPreset.opts = await preset.parserPreset.opts;
4451
}
4552

@@ -76,11 +83,14 @@ export default async (seed = {}) => {
7683
function file() {
7784
const legacy = rc('conventional-changelog-lint');
7885
const legacyFound = typeof legacy.config === 'string';
86+
const explorer = cosmiconfig('commitlint', {
87+
rcExtensions: true,
88+
sync: true,
89+
stopDir: process.cwd()
90+
});
91+
const config = explorer.load('.');
7992

80-
const found = resolveable('./commitlint.config');
81-
const raw = found ? importFrom(process.cwd(), './commitlint.config') : {};
82-
83-
if (legacyFound && !found) {
93+
if (legacyFound && !config) {
8494
console.warn(
8595
`Using legacy ${path.relative(
8696
process.cwd(),
@@ -89,7 +99,7 @@ function file() {
8999
);
90100
}
91101

92-
if (legacyFound && found) {
102+
if (legacyFound && config) {
93103
console.warn(
94104
`Ignored legacy ${path.relative(
95105
process.cwd(),
@@ -98,18 +108,9 @@ function file() {
98108
);
99109
}
100110

101-
if (found) {
102-
return raw;
111+
if (config) {
112+
return config.config;
103113
}
104114

105115
return legacy;
106116
}
107-
108-
function resolveable(id) {
109-
try {
110-
resolveFrom(process.cwd(), id);
111-
return true;
112-
} catch (err) {
113-
return false;
114-
}
115-
}

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

+59-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ test('uses seed as configured', async t => {
2424
test('uses seed with parserPreset', async t => {
2525
t.context.back = chdir('fixtures/parser-preset');
2626

27-
const {parserPreset: actual} = await load({parserPreset: './conventional-changelog-custom'});
27+
const {parserPreset: actual} = await load({
28+
parserPreset: './conventional-changelog-custom'
29+
});
2830
t.is(actual.name, './conventional-changelog-custom');
2931
t.deepEqual(actual.opts, {
3032
parserOpts: {
@@ -63,6 +65,58 @@ test('recursive extends', async t => {
6365
});
6466
});
6567

68+
test('recursive extends with json file', async t => {
69+
t.context.back = chdir('fixtures/recursive-extends-json');
70+
const actual = await load();
71+
t.deepEqual(actual, {
72+
extends: ['./first-extended'],
73+
rules: {
74+
zero: 0,
75+
one: 1,
76+
two: 2
77+
}
78+
});
79+
});
80+
81+
test('recursive extends with yaml file', async t => {
82+
t.context.back = chdir('fixtures/recursive-extends-yaml');
83+
const actual = await load();
84+
t.deepEqual(actual, {
85+
extends: ['./first-extended'],
86+
rules: {
87+
zero: 0,
88+
one: 1,
89+
two: 2
90+
}
91+
});
92+
});
93+
94+
test('recursive extends with js file', async t => {
95+
t.context.back = chdir('fixtures/recursive-extends-js');
96+
const actual = await load();
97+
t.deepEqual(actual, {
98+
extends: ['./first-extended'],
99+
rules: {
100+
zero: 0,
101+
one: 1,
102+
two: 2
103+
}
104+
});
105+
});
106+
107+
test('recursive extends with package.json file', async t => {
108+
t.context.back = chdir('fixtures/recursive-extends-package');
109+
const actual = await load();
110+
t.deepEqual(actual, {
111+
extends: ['./first-extended'],
112+
rules: {
113+
zero: 0,
114+
one: 1,
115+
two: 2
116+
}
117+
});
118+
});
119+
66120
test('parser preset overwrites completely instead of merging', async t => {
67121
t.context.back = chdir('fixtures/parser-preset-override');
68122
const actual = await load();
@@ -83,7 +137,10 @@ test('recursive extends with parserPreset', async t => {
83137

84138
t.is(actual.parserPreset.name, './conventional-changelog-custom');
85139
t.is(typeof actual.parserPreset.opts, 'object');
86-
t.deepEqual(actual.parserPreset.opts.parserOpts.headerPattern, /^(\w*)(?:\((.*)\))?-(.*)$/);
140+
t.deepEqual(
141+
actual.parserPreset.opts.parserOpts.headerPattern,
142+
/^(\w*)(?:\((.*)\))?-(.*)$/
143+
);
87144
});
88145

89146
test('ignores unknow keys', async t => {

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ echo "module.exports = {extends: ['@commitlint/config-angular']}" > commitlint.c
2828

2929
## Config
3030

31-
* Configuration is picked up from `commitlint.config.js` files
31+
* Configuration is picked up from `commitlint.config.js`, `.commitlintrc.js`, `.commitlintrc.json`, or `.commitlintrc.yml` file or a `commitlint` field in `package.json`
3232
* Packages: [cli](./@commitlint/cli), [core](./@commitlint/core)
3333
* See [Rules](./docs/reference-rules.md) for a complete list of possible rules
3434
* An example configuration can be found at [@commitlint/config-angular](./@commitlint/config-angular/index.js)

docs/guides-ci-setup.md

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ npm install --save-dev @commitlint-{cli,angular}
1818
echo "module.exports = {extends: ['@commitlint/config-angular']};" > commitlint.config.js
1919
```
2020

21+
Alternatively the configuration can be defined in `.commitlintrc.js`, `.commitlintrc.json`, or `.commitlintrc.yml` file or a `commitlint` field in `package.json`.
22+
2123
## First test run with Travis
2224

2325
Add a `.travis.yml` to your project root

docs/guides-local-setup.md

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ npm install --save-dev @commitlint-{cli,angular}
1818
echo "module.exports = {extends: ['@commitlint/config-angular']};" > commitlint.config.js
1919
```
2020

21+
Alternatively the configuration can be defined in `.commitlintrc.js`, `.commitlintrc.json`, or `.commitlintrc.yml` file or a `commitlint` field in `package.json`.
22+
2123
## Install husky
2224

2325
Install `husky` as devDependency, a handy git hook helper available on npm.

0 commit comments

Comments
 (0)