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

Lines changed: 9 additions & 6 deletions
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

Lines changed: 25 additions & 7 deletions
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

Lines changed: 0 additions & 5 deletions
This file was deleted.

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

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 6 additions & 0 deletions
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+
};
Lines changed: 6 additions & 0 deletions
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
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"extends": ["./first-extended"],
3+
"rules": {
4+
"zero": 0
5+
}
6+
}
Lines changed: 6 additions & 0 deletions
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+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
rules: {
3+
two: 2
4+
}
5+
};

0 commit comments

Comments
 (0)