Skip to content

Commit 5cd2335

Browse files
imrekbmarionebl
imrekb
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

+9-5
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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
parserPreset: './conventional-changelog-custom'
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+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
extends: ['./first-extended']
3+
};
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+
});
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

+6-8
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

+25
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

+4
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
}

@commitlint/core/src/lint.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import isIgnored from './library/is-ignored';
33
import parse from './library/parse';
44
import implementations from './rules';
55

6-
export default async (message, rules = {}) => {
6+
export default async (message, rules = {}, opts = {}) => {
77
// Found a wildcard match, skip
88
if (isIgnored(message)) {
99
return {
@@ -14,7 +14,7 @@ export default async (message, rules = {}) => {
1414
}
1515

1616
// Parse the commit message
17-
const parsed = await parse(message);
17+
const parsed = await parse(message, undefined, opts.parserOpts);
1818

1919
// Validate against all rules
2020
const results = entries(rules)

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

+16
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,19 @@ test('positive on ignored message and broken rule', async t => {
3434
});
3535
t.true(actual.valid);
3636
});
37+
38+
test('positive on stub message and opts', async t => {
39+
const actual = await lint(
40+
'foo-bar',
41+
{
42+
'type-enum': [2, 'always', ['foo']],
43+
'type-empty': [2, 'never']
44+
},
45+
{
46+
parserOpts: {
47+
headerPattern: /^(\w*)(?:\((.*)\))?-(.*)$/
48+
}
49+
}
50+
);
51+
t.true(actual.valid);
52+
});

@commitlint/core/src/load.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import resolveExtends from './library/resolve-extends';
88
import executeRule from './library/execute-rule';
99

1010
const w = (a, b) => (Array.isArray(b) ? b : undefined);
11-
const valid = input => pick(input, 'extends', 'rules');
11+
const valid = input => pick(input, 'extends', 'rules', 'parserPreset');
1212

1313
export default async (seed = {}) => {
1414
// Obtain config from .rc files
@@ -24,7 +24,11 @@ export default async (seed = {}) => {
2424
cwd: raw.config ? path.dirname(raw.config) : process.cwd()
2525
});
2626

27-
const preset = valid(mergeWith({}, extended, config, w));
27+
const preset = valid(mergeWith(extended, config, w));
28+
29+
if (preset.parserPreset) {
30+
preset.parserOpts = await importFrom(process.cwd(), preset.parserPreset);
31+
}
2832

2933
// Execute rule config functions if needed
3034
const executed = await Promise.all(

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

+12
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ test('uses seed as configured', async t => {
2121
t.is(actual.rules.foo, 'bar');
2222
});
2323

24+
test('uses seed with parserPreset', async t => {
25+
t.context.back = chdir('fixtures/parser-preset');
26+
const actual = await load({parserPreset: './conventional-changelog-custom'});
27+
t.truthy(actual.parserOpts);
28+
});
29+
2430
test('invalid extend should throw', t => {
2531
t.context.back = chdir('fixtures/extends-invalid');
2632
t.throws(load());
@@ -51,6 +57,12 @@ test('recursive extends', async t => {
5157
});
5258
});
5359

60+
test('recursive extends with parserPreset', async t => {
61+
t.context.back = chdir('fixtures/recursive-parser-preset');
62+
const actual = await load();
63+
t.truthy(actual.parserOpts);
64+
});
65+
5466
test('ignores unknow keys', async t => {
5567
t.context.back = chdir('fixtures/trash-file');
5668
const actual = await load();

appveyor.yml

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
environment:
22
matrix:
3-
- nodejs_version: '4'
3+
- nodejs_version: '6'
44
install:
55
- ps: Install-Product node $env:nodejs_version
66
- set CI=true
7-
- npm install -g npm@4
87
- set PATH=%APPDATA%\npm;%PATH%
98
- npm install
109
matrix:

0 commit comments

Comments
 (0)