Skip to content

Commit

Permalink
feat: add preset parser
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
imrekb authored and marionebl committed Sep 1, 2017
1 parent 70b8278 commit 5cd2335
Show file tree
Hide file tree
Showing 15 changed files with 109 additions and 19 deletions.
14 changes: 9 additions & 5 deletions @commitlint/cli/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const rules = {
};

const configuration = {
string: ['from', 'to', 'extends'],
string: ['from', 'to', 'extends', 'parserPreset'],
boolean: ['edit', 'help', 'version', 'quiet', 'color'],
alias: {
c: 'color',
Expand All @@ -33,15 +33,17 @@ const configuration = {
q: 'quiet',
h: 'help',
v: 'version',
x: 'extends'
x: 'extends',
p: 'parserPreset'
},
description: {
color: 'toggle colored output',
edit: 'read last commit message found in ./git/COMMIT_EDITMSG',
extends: 'array of shareable configurations to extend',
from: 'lower end of the commit range to lint; applies if edit=false',
to: 'upper end of the commit range to lint; applies if edit=false',
quiet: 'toggle console output'
quiet: 'toggle console output',
parserPreset: 'preset parser'
},
default: {
color: true,
Expand Down Expand Up @@ -80,7 +82,7 @@ function main(options) {
Promise.all(
messages.map(commit => {
return load(getSeed(flags))
.then(opts => core.lint(commit, opts.rules))
.then(opts => core.lint(commit, opts.rules, opts))
.then(report => {
const formatted = core.format(report, {color: flags.color});

Expand All @@ -106,7 +108,9 @@ function main(options) {
function getSeed(seed) {
const e = Array.isArray(seed.extends) ? seed.extends : [seed.extends];
const n = e.filter(i => typeof i === 'string');
return n.length > 0 ? {extends: n} : {};
return n.length > 0
? {extends: n, parserPreset: seed.parserPreset}
: {parserPreset: seed.parserPreset};
}

// Start the engine
Expand Down
3 changes: 3 additions & 0 deletions @commitlint/core/fixtures/parser-preset/commitlint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
parserPreset: './conventional-changelog-custom'
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const defaultOpts = require('conventional-changelog-angular');
const _ = require('lodash');

module.exports = defaultOpts.then(data => {
const extented = _.cloneDeep(data);
extented.parserOpts.headerPattern = /^(\w*)(?:\((.*)\))?-(.*)$/;
return extented;
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
extends: ['./first-extended']
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
extends: ['./second-extended']
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const defaultOpts = require('conventional-changelog-angular');
const _ = require('lodash');

module.exports = defaultOpts.then(data => {
const extented = _.cloneDeep(data);
extented.parserOpts.headerPattern = /^(\w*)(?:\((.*)\))?-(.*)$/;
return extented;
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
parserPreset: './conventional-changelog-custom'
};
14 changes: 6 additions & 8 deletions @commitlint/core/src/library/parse.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import {sync} from 'conventional-commits-parser';
import defaultChangelogOpts from 'conventional-changelog-angular';

export default parse;

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

const {parserOpts} = await opts;
const parsed = parser(message, parserOpts);
parsed.raw = message;
return parsed;
Expand Down
25 changes: 25 additions & 0 deletions @commitlint/core/src/library/parse.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import importFrom from 'import-from';
import test from 'ava';
import parse from './parse';

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

test('uses custom opts parser', async t => {
const message = 'type(scope)-subject';
const changelogOpts = await importFrom(
process.cwd(),
'./fixtures/parser-preset/conventional-changelog-custom'
);
const actual = await parse(message, undefined, changelogOpts.parserOpts);
const expected = {
body: null,
footer: null,
header: 'type(scope)-subject',
mentions: [],
merge: null,
notes: [],
raw: 'type(scope)-subject',
references: [],
revert: null,
scope: 'scope',
subject: 'subject',
type: 'type'
};
t.deepEqual(actual, expected);
});

test('supports scopes with /', async t => {
const message = 'type(some/scope): subject';
const actual = await parse(message);
Expand Down
4 changes: 4 additions & 0 deletions @commitlint/core/src/library/resolve-extends.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ function loadExtends(config = {}, context = {}) {
cwd: path.dirname(resolved)
});

if (c && c.parserPreset) {
c.parserPreset = resolveId(c.parserPreset, ctx);
}

return [...configs, c, ...loadExtends(c, ctx)];
}, []);
}
Expand Down
4 changes: 2 additions & 2 deletions @commitlint/core/src/lint.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import isIgnored from './library/is-ignored';
import parse from './library/parse';
import implementations from './rules';

export default async (message, rules = {}) => {
export default async (message, rules = {}, opts = {}) => {
// Found a wildcard match, skip
if (isIgnored(message)) {
return {
Expand All @@ -14,7 +14,7 @@ export default async (message, rules = {}) => {
}

// Parse the commit message
const parsed = await parse(message);
const parsed = await parse(message, undefined, opts.parserOpts);

// Validate against all rules
const results = entries(rules)
Expand Down
16 changes: 16 additions & 0 deletions @commitlint/core/src/lint.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,19 @@ test('positive on ignored message and broken rule', async t => {
});
t.true(actual.valid);
});

test('positive on stub message and opts', async t => {
const actual = await lint(
'foo-bar',
{
'type-enum': [2, 'always', ['foo']],
'type-empty': [2, 'never']
},
{
parserOpts: {
headerPattern: /^(\w*)(?:\((.*)\))?-(.*)$/
}
}
);
t.true(actual.valid);
});
8 changes: 6 additions & 2 deletions @commitlint/core/src/load.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import resolveExtends from './library/resolve-extends';
import executeRule from './library/execute-rule';

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

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

const preset = valid(mergeWith({}, extended, config, w));
const preset = valid(mergeWith(extended, config, w));

if (preset.parserPreset) {
preset.parserOpts = await importFrom(process.cwd(), preset.parserPreset);
}

// Execute rule config functions if needed
const executed = await Promise.all(
Expand Down
12 changes: 12 additions & 0 deletions @commitlint/core/src/load.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ test('uses seed as configured', async t => {
t.is(actual.rules.foo, 'bar');
});

test('uses seed with parserPreset', async t => {
t.context.back = chdir('fixtures/parser-preset');
const actual = await load({parserPreset: './conventional-changelog-custom'});
t.truthy(actual.parserOpts);
});

test('invalid extend should throw', t => {
t.context.back = chdir('fixtures/extends-invalid');
t.throws(load());
Expand Down Expand Up @@ -51,6 +57,12 @@ test('recursive extends', async t => {
});
});

test('recursive extends with parserPreset', async t => {
t.context.back = chdir('fixtures/recursive-parser-preset');
const actual = await load();
t.truthy(actual.parserOpts);
});

test('ignores unknow keys', async t => {
t.context.back = chdir('fixtures/trash-file');
const actual = await load();
Expand Down
3 changes: 1 addition & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
environment:
matrix:
- nodejs_version: '4'
- nodejs_version: '6'
install:
- ps: Install-Product node $env:nodejs_version
- set CI=true
- npm install -g npm@4
- set PATH=%APPDATA%\npm;%PATH%
- npm install
matrix:
Expand Down

0 comments on commit 5cd2335

Please sign in to comment.