Skip to content

Commit 91968b8

Browse files
authored
feat: enable alternative config formats (#83)
* feat(core): Allow to configure with json, yaml and package.json Fix #73 * chore: consolidate dev dependencies * chore: introduce cwd awareness * allow forced cwds * remove flaky tests BREAKING CHANGE: discontinue support of conventional-changelog-lintrc * test: make git setup reliable
1 parent 09d0494 commit 91968b8

File tree

33 files changed

+364
-354
lines changed

33 files changed

+364
-354
lines changed

@commitlint/cli/cli.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ const rules = {
2323
};
2424

2525
const configuration = {
26-
string: ['from', 'to', 'extends', 'parser-preset'],
26+
string: ['cwd', 'from', 'to', 'extends', 'parser-preset'],
2727
boolean: ['edit', 'help', 'version', 'quiet', 'color'],
2828
alias: {
2929
c: 'color',
30+
d: 'cwd',
3031
e: 'edit',
3132
f: 'from',
3233
t: 'to',
@@ -38,15 +39,18 @@ const configuration = {
3839
},
3940
description: {
4041
color: 'toggle colored output',
42+
cwd: 'directory to execute in',
4143
edit: 'read last commit message found in ./git/COMMIT_EDITMSG',
4244
extends: 'array of shareable configurations to extend',
4345
from: 'lower end of the commit range to lint; applies if edit=false',
4446
to: 'upper end of the commit range to lint; applies if edit=false',
4547
quiet: 'toggle console output',
46-
'parser-preset': 'configuration preset to use for conventional-commits-parser'
48+
'parser-preset':
49+
'configuration preset to use for conventional-commits-parser'
4750
},
4851
default: {
4952
color: true,
53+
cwd: process.cwd(),
5054
edit: false,
5155
from: null,
5256
to: null,
@@ -67,21 +71,21 @@ const cli = meow(
6771
configuration
6872
);
6973

70-
const load = seed => core.load(seed);
74+
const load = (seed, opts) => core.load(seed, opts);
7175

7276
function main(options) {
7377
const raw = options.input;
7478
const flags = options.flags;
7579
const fromStdin = rules.fromStdin(raw, flags);
7680

7781
const range = pick(flags, 'edit', 'from', 'to');
78-
const input = fromStdin ? stdin() : core.read(range);
82+
const input = fromStdin ? stdin() : core.read(range, {cwd: flags.cwd});
7983
const fmt = new chalk.constructor({enabled: flags.color});
8084

8185
return input.then(raw => (Array.isArray(raw) ? raw : [raw])).then(messages =>
8286
Promise.all(
8387
messages.map(commit => {
84-
return load(getSeed(flags))
88+
return load(getSeed(flags), {cwd: flags.cwd})
8589
.then(loaded => {
8690
const parserOpts = selectParserOpts(loaded.parserPreset);
8791
const opts = parserOpts ? {parserOpts} : undefined;
@@ -127,7 +131,6 @@ main(cli).catch(err =>
127131
})
128132
);
129133

130-
131134
function selectParserOpts(parserPreset) {
132135
if (typeof parserPreset !== 'object') {
133136
return undefined;

@commitlint/cli/cli.test.js

+25-7
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const exec = (command, args = [], opts = {}) => {
3131
console.log(result.stderr);
3232
}
3333
return result;
34-
}
34+
};
3535
};
3636

3737
const cli = exec.bind(null, CLI);
@@ -40,8 +40,12 @@ const mkdir = exec.bind(null, bin('mkdirp'));
4040
const npm = exec.bind(null, 'npm');
4141
const rm = exec.bind(null, bin('rimraf'));
4242

43-
test('should throw when called without [input]', t => {
44-
t.throws(cli()(), /Expected a raw commit/);
43+
test('should throw when called without [input]', async t => {
44+
const dir = tmp.dirSync().name;
45+
46+
await init(dir);
47+
await t.throws(cli([], {cwd: dir})(), /Expected a raw commit/);
48+
await rm([dir])();
4549
});
4650

4751
test('should reprint input from stdin', async t => {
@@ -73,11 +77,19 @@ test('should fail for input from stdin with rule from rc', async t => {
7377
});
7478

7579
test('should fail for input from stdin with rule from js', async t => {
80+
const dir = tmp.dirSync().name;
81+
82+
await init(dir);
83+
await sander.copydir(EXTENDS_ROOT).to(dir);
84+
7685
const actual = await t.throws(
77-
cli(['--extends', './extended'], {cwd: EXTENDS_ROOT})('foo: bar')
86+
cli(['--extends', './extended'], {cwd: dir})('foo: bar')
7887
);
88+
7989
t.true(includes(actual.stdout, 'type must not be one of [foo]'));
8090
t.is(actual.code, 1);
91+
92+
await rm([dir])();
8193
});
8294

8395
test('should produce no error output with --quiet flag', async t => {
@@ -125,11 +137,13 @@ test('should work with husky commitmsg hook in sub packages', async () => {
125137

126138
test('should pick up parser preset', async t => {
127139
const cwd = PARSER_PRESET;
128-
129140
const actual = await t.throws(cli([], {cwd})('type(scope)-ticket subject'));
141+
130142
t.true(includes(actual.stdout, 'message may not be empty [subject-empty]'));
131143

132-
await cli(['--parser-preset', './parser-preset'], {cwd})('type(scope)-ticket subject');
144+
await cli(['--parser-preset', './parser-preset'], {cwd})(
145+
'type(scope)-ticket subject'
146+
);
133147
});
134148

135149
async function init(cwd) {
@@ -142,5 +156,9 @@ async function init(cwd) {
142156
}
143157

144158
function pkg(cwd) {
145-
return sander.writeFile(cwd, 'package.json', JSON.stringify({scripts: {commitmsg: `${CLI} -e`}}));
159+
return sander.writeFile(
160+
cwd,
161+
'package.json',
162+
JSON.stringify({scripts: {commitmsg: `${CLI} -e`}})
163+
);
146164
}

@commitlint/core/fixtures/legacy/.conventional-changelog-lintrc

-5
This file was deleted.

@commitlint/core/fixtures/overriden-legacy/.conventional-changelog-lintrc

-5
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+
};
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
@@ -1,5 +1,5 @@
11
module.exports = {
22
rules: {
3-
legacy: false
3+
two: 2
44
}
55
};
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+
};
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
@@ -1,6 +1 @@
1-
module.exports = {
2-
extends: ['./second-extended'],
3-
rules: {
4-
one: 1
5-
}
6-
};
1+
module.exports = require('./commitlint.config.js');
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
const defaultOpts = require('conventional-changelog-angular');
2-
const _ = require('lodash');
3-
4-
module.exports = defaultOpts.then(data => {
5-
const extented = _.cloneDeep(data);
6-
extented.parserOpts.headerPattern = /^(\w*)(?:\((.*)\))?-(.*)$/;
7-
return extented;
1+
module.exports = Promise.resolve().then(() => {
2+
return {
3+
parserOpts: {
4+
headerPattern: /^(\w*)(?:\((.*)\))?-(.*)$/
5+
}
6+
};
87
});

@commitlint/core/package.json

+6-9
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,15 @@
6363
"license": "MIT",
6464
"devDependencies": {
6565
"@commitlint/utils": "^3.1.1",
66-
"ansi-styles": "3.1.0",
67-
"ava": "0.18.2",
68-
"babel-cli": "^6.18.0",
66+
"ava": "0.22.0",
67+
"babel-cli": "^6.26.0",
6968
"babel-preset-commitlint": "^3.2.0",
70-
"babel-register": "6.24.1",
69+
"babel-register": "^6.26.0",
7170
"cross-env": "^5.0.1",
7271
"denodeify": "1.2.1",
7372
"dependency-check": "2.7.0",
7473
"execa": "0.6.3",
7574
"globby": "6.1.0",
76-
"has-ansi": "3.0.0",
7775
"import-from": "2.1.0",
7876
"nyc": "10.3.2",
7977
"path-exists": "3.0.0",
@@ -82,19 +80,18 @@
8280
"xo": "0.18.2"
8381
},
8482
"dependencies": {
83+
"@marionebl/git-raw-commits": "^1.2.0",
84+
"@marionebl/sander": "^0.6.0",
8585
"babel-runtime": "^6.23.0",
8686
"chalk": "^2.0.1",
8787
"conventional-changelog-angular": "^1.3.3",
8888
"conventional-commits-parser": "^1.3.0",
89+
"cosmiconfig": "^3.0.1",
8990
"find-up": "^2.1.0",
9091
"franc": "^2.0.0",
91-
"git-raw-commits": "^1.1.2",
92-
"import-from": "^2.1.0",
9392
"lodash": "^4.17.4",
94-
"mz": "^2.6.0",
9593
"path-exists": "^3.0.0",
9694
"pos": "^0.4.2",
97-
"rc": "^1.1.7",
9895
"resolve-from": "^3.0.0",
9996
"semver": "^5.3.0"
10097
}

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

-66
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import test from 'ava';
2-
import hasAnsi from 'has-ansi';
32
import chalk from 'chalk';
4-
import {yellow, red, magenta, blue} from 'ansi-styles';
53
import {includes} from 'lodash';
64
import format from './format';
75

@@ -49,19 +47,6 @@ test('returns a correct of empty .errors and .warnings', t => {
4947
t.true(includes(msg, '1 problems, 1 warnings'));
5048
});
5149

52-
test('colors messages by default', t => {
53-
const [msg] = format({
54-
errors: [],
55-
warnings: []
56-
});
57-
t.true(hasAnsi(msg));
58-
});
59-
60-
test('does not color messages if configured', t => {
61-
const [msg] = format({}, {color: false});
62-
t.false(hasAnsi(msg));
63-
});
64-
6550
test('uses appropriate signs by default', t => {
6651
const [err, warn] = format({
6752
errors: [
@@ -110,54 +95,3 @@ test('uses signs as configured', t => {
11095
t.true(includes(err, 'ERR'));
11196
t.true(includes(warn, 'WRN'));
11297
});
113-
114-
test('uses appropriate colors by default', t => {
115-
const [err, warn] = format({
116-
errors: [
117-
{
118-
level: 2,
119-
name: 'error-name',
120-
message: 'There was an error'
121-
}
122-
],
123-
warnings: [
124-
{
125-
level: 1,
126-
name: 'warning-name',
127-
message: 'There was a problem'
128-
}
129-
]
130-
});
131-
132-
t.true(includes(err, red.open));
133-
t.true(includes(warn, yellow.open));
134-
});
135-
136-
if (process.platform !== 'win32') {
137-
test('uses colors as configured', t => {
138-
const [err, warn] = format(
139-
{
140-
errors: [
141-
{
142-
level: 2,
143-
name: 'error-name',
144-
message: 'There was an error'
145-
}
146-
],
147-
warnings: [
148-
{
149-
level: 1,
150-
name: 'warning-name',
151-
message: 'There was a problem'
152-
}
153-
]
154-
},
155-
{
156-
colors: ['white', 'magenta', 'blue']
157-
}
158-
);
159-
160-
t.true(includes(err, blue.open));
161-
t.true(includes(warn, magenta.open));
162-
});
163-
}

0 commit comments

Comments
 (0)