Skip to content

Commit

Permalink
feat: enable alternative config formats (#83)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
marionebl authored Oct 3, 2017
1 parent 09d0494 commit 91968b8
Show file tree
Hide file tree
Showing 33 changed files with 364 additions and 354 deletions.
15 changes: 9 additions & 6 deletions @commitlint/cli/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ const rules = {
};

const configuration = {
string: ['from', 'to', 'extends', 'parser-preset'],
string: ['cwd', 'from', 'to', 'extends', 'parser-preset'],
boolean: ['edit', 'help', 'version', 'quiet', 'color'],
alias: {
c: 'color',
d: 'cwd',
e: 'edit',
f: 'from',
t: 'to',
Expand All @@ -38,15 +39,18 @@ const configuration = {
},
description: {
color: 'toggle colored output',
cwd: 'directory to execute in',
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',
'parser-preset': 'configuration preset to use for conventional-commits-parser'
'parser-preset':
'configuration preset to use for conventional-commits-parser'
},
default: {
color: true,
cwd: process.cwd(),
edit: false,
from: null,
to: null,
Expand All @@ -67,21 +71,21 @@ const cli = meow(
configuration
);

const load = seed => core.load(seed);
const load = (seed, opts) => core.load(seed, opts);

function main(options) {
const raw = options.input;
const flags = options.flags;
const fromStdin = rules.fromStdin(raw, flags);

const range = pick(flags, 'edit', 'from', 'to');
const input = fromStdin ? stdin() : core.read(range);
const input = fromStdin ? stdin() : core.read(range, {cwd: flags.cwd});
const fmt = new chalk.constructor({enabled: flags.color});

return input.then(raw => (Array.isArray(raw) ? raw : [raw])).then(messages =>
Promise.all(
messages.map(commit => {
return load(getSeed(flags))
return load(getSeed(flags), {cwd: flags.cwd})
.then(loaded => {
const parserOpts = selectParserOpts(loaded.parserPreset);
const opts = parserOpts ? {parserOpts} : undefined;
Expand Down Expand Up @@ -127,7 +131,6 @@ main(cli).catch(err =>
})
);


function selectParserOpts(parserPreset) {
if (typeof parserPreset !== 'object') {
return undefined;
Expand Down
32 changes: 25 additions & 7 deletions @commitlint/cli/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const exec = (command, args = [], opts = {}) => {
console.log(result.stderr);
}
return result;
}
};
};

const cli = exec.bind(null, CLI);
Expand All @@ -40,8 +40,12 @@ const mkdir = exec.bind(null, bin('mkdirp'));
const npm = exec.bind(null, 'npm');
const rm = exec.bind(null, bin('rimraf'));

test('should throw when called without [input]', t => {
t.throws(cli()(), /Expected a raw commit/);
test('should throw when called without [input]', async t => {
const dir = tmp.dirSync().name;

await init(dir);
await t.throws(cli([], {cwd: dir})(), /Expected a raw commit/);
await rm([dir])();
});

test('should reprint input from stdin', async t => {
Expand Down Expand Up @@ -73,11 +77,19 @@ test('should fail for input from stdin with rule from rc', async t => {
});

test('should fail for input from stdin with rule from js', async t => {
const dir = tmp.dirSync().name;

await init(dir);
await sander.copydir(EXTENDS_ROOT).to(dir);

const actual = await t.throws(
cli(['--extends', './extended'], {cwd: EXTENDS_ROOT})('foo: bar')
cli(['--extends', './extended'], {cwd: dir})('foo: bar')
);

t.true(includes(actual.stdout, 'type must not be one of [foo]'));
t.is(actual.code, 1);

await rm([dir])();
});

test('should produce no error output with --quiet flag', async t => {
Expand Down Expand Up @@ -125,11 +137,13 @@ test('should work with husky commitmsg hook in sub packages', async () => {

test('should pick up parser preset', async t => {
const cwd = PARSER_PRESET;

const actual = await t.throws(cli([], {cwd})('type(scope)-ticket subject'));

t.true(includes(actual.stdout, 'message may not be empty [subject-empty]'));

await cli(['--parser-preset', './parser-preset'], {cwd})('type(scope)-ticket subject');
await cli(['--parser-preset', './parser-preset'], {cwd})(
'type(scope)-ticket subject'
);
});

async function init(cwd) {
Expand All @@ -142,5 +156,9 @@ async function init(cwd) {
}

function pkg(cwd) {
return sander.writeFile(cwd, 'package.json', JSON.stringify({scripts: {commitmsg: `${CLI} -e`}}));
return sander.writeFile(
cwd,
'package.json',
JSON.stringify({scripts: {commitmsg: `${CLI} -e`}})
);
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
extends: ['./first-extended'],
rules: {
zero: 0
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
extends: ['./second-extended'],
rules: {
one: 1
}
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = {
rules: {
legacy: false
two: 2
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": ["./first-extended"],
"rules": {
"zero": 0
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
extends: ['./second-extended'],
rules: {
one: 1
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
rules: {
two: 2
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
extends: ['./second-extended'],
rules: {
one: 1
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
rules: {
two: 2
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"commitlint": {
"extends": ["./first-extended"],
"rules": {
"zero": 0
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
extends:
- "./first-extended"
rules:
zero: 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
extends: ['./second-extended'],
rules: {
one: 1
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
rules: {
two: 2
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
extends: ['./second-extended'],
rules: {
one: 1
}
};
Original file line number Diff line number Diff line change
@@ -1,6 +1 @@
module.exports = {
extends: ['./second-extended'],
rules: {
one: 1
}
};
module.exports = require('./commitlint.config.js');
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
const defaultOpts = require('conventional-changelog-angular');
const _ = require('lodash');

module.exports = defaultOpts.then(data => {
const extented = _.cloneDeep(data);
extented.parserOpts.headerPattern = /^(\w*)(?:\((.*)\))?-(.*)$/;
return extented;
module.exports = Promise.resolve().then(() => {
return {
parserOpts: {
headerPattern: /^(\w*)(?:\((.*)\))?-(.*)$/
}
};
});
15 changes: 6 additions & 9 deletions @commitlint/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,15 @@
"license": "MIT",
"devDependencies": {
"@commitlint/utils": "^3.1.1",
"ansi-styles": "3.1.0",
"ava": "0.18.2",
"babel-cli": "^6.18.0",
"ava": "0.22.0",
"babel-cli": "^6.26.0",
"babel-preset-commitlint": "^3.2.0",
"babel-register": "6.24.1",
"babel-register": "^6.26.0",
"cross-env": "^5.0.1",
"denodeify": "1.2.1",
"dependency-check": "2.7.0",
"execa": "0.6.3",
"globby": "6.1.0",
"has-ansi": "3.0.0",
"import-from": "2.1.0",
"nyc": "10.3.2",
"path-exists": "3.0.0",
Expand All @@ -82,19 +80,18 @@
"xo": "0.18.2"
},
"dependencies": {
"@marionebl/git-raw-commits": "^1.2.0",
"@marionebl/sander": "^0.6.0",
"babel-runtime": "^6.23.0",
"chalk": "^2.0.1",
"conventional-changelog-angular": "^1.3.3",
"conventional-commits-parser": "^1.3.0",
"cosmiconfig": "^3.0.1",
"find-up": "^2.1.0",
"franc": "^2.0.0",
"git-raw-commits": "^1.1.2",
"import-from": "^2.1.0",
"lodash": "^4.17.4",
"mz": "^2.6.0",
"path-exists": "^3.0.0",
"pos": "^0.4.2",
"rc": "^1.1.7",
"resolve-from": "^3.0.0",
"semver": "^5.3.0"
}
Expand Down
66 changes: 0 additions & 66 deletions @commitlint/core/src/format.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import test from 'ava';
import hasAnsi from 'has-ansi';
import chalk from 'chalk';
import {yellow, red, magenta, blue} from 'ansi-styles';
import {includes} from 'lodash';
import format from './format';

Expand Down Expand Up @@ -49,19 +47,6 @@ test('returns a correct of empty .errors and .warnings', t => {
t.true(includes(msg, '1 problems, 1 warnings'));
});

test('colors messages by default', t => {
const [msg] = format({
errors: [],
warnings: []
});
t.true(hasAnsi(msg));
});

test('does not color messages if configured', t => {
const [msg] = format({}, {color: false});
t.false(hasAnsi(msg));
});

test('uses appropriate signs by default', t => {
const [err, warn] = format({
errors: [
Expand Down Expand Up @@ -110,54 +95,3 @@ test('uses signs as configured', t => {
t.true(includes(err, 'ERR'));
t.true(includes(warn, 'WRN'));
});

test('uses appropriate colors by default', t => {
const [err, warn] = format({
errors: [
{
level: 2,
name: 'error-name',
message: 'There was an error'
}
],
warnings: [
{
level: 1,
name: 'warning-name',
message: 'There was a problem'
}
]
});

t.true(includes(err, red.open));
t.true(includes(warn, yellow.open));
});

if (process.platform !== 'win32') {
test('uses colors as configured', t => {
const [err, warn] = format(
{
errors: [
{
level: 2,
name: 'error-name',
message: 'There was an error'
}
],
warnings: [
{
level: 1,
name: 'warning-name',
message: 'There was a problem'
}
]
},
{
colors: ['white', 'magenta', 'blue']
}
);

t.true(includes(err, blue.open));
t.true(includes(warn, magenta.open));
});
}
Loading

0 comments on commit 91968b8

Please sign in to comment.