Skip to content

Commit c981cbe

Browse files
committed
style: refactor parser-preset feature a bit
1 parent 5cd2335 commit c981cbe

File tree

15 files changed

+136
-26
lines changed

15 files changed

+136
-26
lines changed

@commitlint/cli/cli.js

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const rules = {
2323
};
2424

2525
const configuration = {
26-
string: ['from', 'to', 'extends', 'parserPreset'],
26+
string: ['from', 'to', 'extends', 'parser-preset'],
2727
boolean: ['edit', 'help', 'version', 'quiet', 'color'],
2828
alias: {
2929
c: 'color',
@@ -34,7 +34,7 @@ const configuration = {
3434
h: 'help',
3535
v: 'version',
3636
x: 'extends',
37-
p: 'parserPreset'
37+
p: 'parser-preset'
3838
},
3939
description: {
4040
color: 'toggle colored output',
@@ -43,7 +43,7 @@ const configuration = {
4343
from: 'lower end of the commit range to lint; applies if edit=false',
4444
to: 'upper end of the commit range to lint; applies if edit=false',
4545
quiet: 'toggle console output',
46-
parserPreset: 'preset parser'
46+
'parser-preset': 'configuration preset to use for conventional-commits-parser'
4747
},
4848
default: {
4949
color: true,
@@ -82,7 +82,11 @@ function main(options) {
8282
Promise.all(
8383
messages.map(commit => {
8484
return load(getSeed(flags))
85-
.then(opts => core.lint(commit, opts.rules, opts))
85+
.then(loaded => {
86+
const parserOpts = selectParserOpts(loaded.parserPreset);
87+
const opts = parserOpts ? {parserOpts} : undefined;
88+
return core.lint(commit, loaded.rules, opts);
89+
})
8690
.then(report => {
8791
const formatted = core.format(report, {color: flags.color});
8892

@@ -123,6 +127,21 @@ main(cli).catch(err =>
123127
})
124128
);
125129

130+
131+
function selectParserOpts(parserPreset) {
132+
if (typeof parserPreset !== 'object') {
133+
return undefined;
134+
}
135+
136+
const opts = parserPreset.opts;
137+
138+
if (typeof opts !== 'object') {
139+
return undefined;
140+
}
141+
142+
return opts.parserOpts;
143+
}
144+
126145
// Catch unhandled rejections globally
127146
process.on('unhandledRejection', (reason, promise) => {
128147
console.log('Unhandled Rejection at: Promise ', promise, ' reason: ', reason);

@commitlint/cli/cli.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const CLI = here('cli.js');
1414
const SIMPLE = fix('simple');
1515
const EXTENDS_ROOT = fix('extends-root');
1616
const EMPTY = fix('empty');
17+
const PARSER_PRESET = fix('parser-preset');
1718

1819
const HUSKY = tmp.dirSync().name;
1920
const HUSKY_INTEGRATION = path.join(tmp.dirSync().name, 'integration');
@@ -122,6 +123,15 @@ test('should work with husky commitmsg hook in sub packages', async () => {
122123
await rm([upper])();
123124
});
124125

126+
test('should pick up parser preset', async t => {
127+
const cwd = PARSER_PRESET;
128+
129+
const actual = await t.throws(cli([], {cwd})('type(scope)-ticket subject'));
130+
t.true(includes(actual.stdout, 'message may not be empty [subject-empty]'));
131+
132+
await cli(['--parser-preset', './parser-preset'], {cwd})('type(scope)-ticket subject');
133+
});
134+
125135
async function init(cwd) {
126136
await git(['init'], {cwd})();
127137

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = {
2+
rules: {
3+
'type-enum': [2, 'always', ['type']],
4+
'scope-enum': [2, 'always', ['scope']],
5+
'subject-empty': [2, 'never']
6+
}
7+
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
parserOpts: {
3+
headerPattern: /^(\w*)\((\w*)\)-(\w*)\s(.*)$/,
4+
headerCorrespondence: ['type', 'scope', 'ticket', 'subject']
5+
}
6+
};

@commitlint/cli/help.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ module.exports = configuration => {
2222
const desc = line[1];
2323
const defaults = line[2];
2424
const fs = flags.map(
25-
flag => (flag.length > 1 ? `--${flag}` : `-${flag}`)
25+
flag => (flag.length > 1 ? `--${flag}` : ` -${flag}`)
2626
);
2727
const ds = defaults ? `, defaults to: ${defaults}` : '';
2828
const length = flags.reduce((sum, flag) => sum + flag.length, 0);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = {
2+
extends: ['./extended'],
3+
parserPreset: './custom'
4+
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
parserOpts: {
3+
headerPattern: /.*/,
4+
},
5+
b: 'b'
6+
};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = Promise.resolve({
2+
headerPattern: /^(\w*)(?:\((.*)\))?-(.*)$/,
3+
a: 'a'
4+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
parserPreset: './custom'
3+
};
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
module.exports = {
2-
parserPreset: './conventional-changelog-custom'
2+
parserOpts: {
3+
parserPreset: './conventional-changelog-custom'
4+
}
35
};

0 commit comments

Comments
 (0)