Skip to content

Commit 5cd2335

Browse files
imrekbmarionebl
authored andcommitted
feat: add preset parser
* feat: add parserPreset * chore: use latest npm on appveyor * chore: reset npm config on appveyor * style(core): simplify dereferencing * chore: use correct npm config cmd * chore: win32, sigh * chore: upgrade appveyor to node6
1 parent 70b8278 commit 5cd2335

File tree

15 files changed

+109
-19
lines changed

15 files changed

+109
-19
lines changed

@commitlint/cli/cli.js

Lines changed: 9 additions & 5 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'],
26+
string: ['from', 'to', 'extends', 'parserPreset'],
2727
boolean: ['edit', 'help', 'version', 'quiet', 'color'],
2828
alias: {
2929
c: 'color',
@@ -33,15 +33,17 @@ const configuration = {
3333
q: 'quiet',
3434
h: 'help',
3535
v: 'version',
36-
x: 'extends'
36+
x: 'extends',
37+
p: 'parserPreset'
3738
},
3839
description: {
3940
color: 'toggle colored output',
4041
edit: 'read last commit message found in ./git/COMMIT_EDITMSG',
4142
extends: 'array of shareable configurations to extend',
4243
from: 'lower end of the commit range to lint; applies if edit=false',
4344
to: 'upper end of the commit range to lint; applies if edit=false',
44-
quiet: 'toggle console output'
45+
quiet: 'toggle console output',
46+
parserPreset: 'preset parser'
4547
},
4648
default: {
4749
color: true,
@@ -80,7 +82,7 @@ function main(options) {
8082
Promise.all(
8183
messages.map(commit => {
8284
return load(getSeed(flags))
83-
.then(opts => core.lint(commit, opts.rules))
85+
.then(opts => core.lint(commit, opts.rules, opts))
8486
.then(report => {
8587
const formatted = core.format(report, {color: flags.color});
8688

@@ -106,7 +108,9 @@ function main(options) {
106108
function getSeed(seed) {
107109
const e = Array.isArray(seed.extends) ? seed.extends : [seed.extends];
108110
const n = e.filter(i => typeof i === 'string');
109-
return n.length > 0 ? {extends: n} : {};
111+
return n.length > 0
112+
? {extends: n, parserPreset: seed.parserPreset}
113+
: {parserPreset: seed.parserPreset};
110114
}
111115

112116
// Start the engine
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
parserPreset: './conventional-changelog-custom'
3+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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;
8+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
extends: ['./first-extended']
3+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
extends: ['./second-extended']
3+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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;
8+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
parserPreset: './conventional-changelog-custom'
3+
};

@commitlint/core/src/library/parse.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
import {sync} from 'conventional-commits-parser';
2+
import defaultChangelogOpts from 'conventional-changelog-angular';
23

34
export default parse;
45

5-
async function parse(message, parser = sync) {
6-
// Prevent conventional-changelog-angular from spamming startup
7-
// TODO: Remove when https://github.com/conventional-changelog/conventional-changelog/pull/206 lands
8-
const _error = console.error;
9-
console.error = () => {};
10-
const opts = require('conventional-changelog-angular');
11-
console.error = _error;
6+
async function parse(message, parser = sync, parserOpts) {
7+
if (!parserOpts) {
8+
const changelogOpts = await defaultChangelogOpts;
9+
parserOpts = changelogOpts.parserOpts;
10+
}
1211

13-
const {parserOpts} = await opts;
1412
const parsed = parser(message, parserOpts);
1513
parsed.raw = message;
1614
return parsed;

@commitlint/core/src/library/parse.test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import importFrom from 'import-from';
12
import test from 'ava';
23
import parse from './parse';
34

@@ -71,6 +72,30 @@ test('uses angular grammar', async t => {
7172
t.deepEqual(actual, expected);
7273
});
7374

75+
test('uses custom opts parser', async t => {
76+
const message = 'type(scope)-subject';
77+
const changelogOpts = await importFrom(
78+
process.cwd(),
79+
'./fixtures/parser-preset/conventional-changelog-custom'
80+
);
81+
const actual = await parse(message, undefined, changelogOpts.parserOpts);
82+
const expected = {
83+
body: null,
84+
footer: null,
85+
header: 'type(scope)-subject',
86+
mentions: [],
87+
merge: null,
88+
notes: [],
89+
raw: 'type(scope)-subject',
90+
references: [],
91+
revert: null,
92+
scope: 'scope',
93+
subject: 'subject',
94+
type: 'type'
95+
};
96+
t.deepEqual(actual, expected);
97+
});
98+
7499
test('supports scopes with /', async t => {
75100
const message = 'type(some/scope): subject';
76101
const actual = await parse(message);

@commitlint/core/src/library/resolve-extends.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ function loadExtends(config = {}, context = {}) {
3838
cwd: path.dirname(resolved)
3939
});
4040

41+
if (c && c.parserPreset) {
42+
c.parserPreset = resolveId(c.parserPreset, ctx);
43+
}
44+
4145
return [...configs, c, ...loadExtends(c, ctx)];
4246
}, []);
4347
}

0 commit comments

Comments
 (0)