Skip to content

Commit

Permalink
feat(cli): add format option for report output
Browse files Browse the repository at this point in the history
  • Loading branch information
byCedric authored and marionebl committed Oct 5, 2018
1 parent b0e63d9 commit 1ecf097
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
formatter: 'custom-formatter',
rules: {
'type-enum': [2, 'never', ['foo']]
}
};
21 changes: 20 additions & 1 deletion @commitlint/cli/src/cli.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#!/usr/bin/env node
require('babel-polyfill'); // eslint-disable-line import/no-unassigned-import

const format = require('@commitlint/format');
const load = require('@commitlint/load');
const lint = require('@commitlint/lint');
const read = require('@commitlint/read');
Expand Down Expand Up @@ -63,6 +62,12 @@ const flags = {
description: 'lower end of the commit range to lint; applies if edit=false',
type: 'string'
},
format: {
alias: 'o',
default: null,
description: 'output format of the results',
type: 'string'
},
'parser-preset': {
alias: 'p',
description: 'configuration preset to use for conventional-commits-parser',
Expand Down Expand Up @@ -135,6 +140,7 @@ async function main(options) {
const loaded = await load(getSeed(flags), loadOpts);
const parserOpts = selectParserOpts(loaded.parserPreset);
const opts = parserOpts ? {parserOpts} : {parserOpts: {}};
const format = loadFormatter(loaded, flags);

// Strip comments if reading from `.git/COMMIT_EDIT_MSG`
if (range.edit) {
Expand Down Expand Up @@ -248,6 +254,19 @@ function selectParserOpts(parserPreset) {
return parserPreset.parserOpts;
}

function loadFormatter(config, flags) {
const moduleName = flags.format || config.formatter;
let modulePath;

try {
modulePath = require.resolve(`${moduleName}`);
} catch (error) {
throw new Error(`Using format ${moduleName}, but cannot find the module.`);
}

return require(modulePath);
}

// Catch unhandled rejections globally
process.on('unhandledRejection', (reason, promise) => {
console.log('Unhandled Rejection at: Promise ', promise, ' reason: ', reason);
Expand Down
24 changes: 24 additions & 0 deletions @commitlint/cli/src/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,30 @@ test('should not print full commit message when input succeeds', async t => {
t.is(actual.code, 0);
});

test('should fail for invalid formatters from configuration', async t => {
const cwd = await git.bootstrap('fixtures/custom-formatter');
const actual = await cli([], {cwd})('foo: bar');
t.true(
actual.stderr.includes(
`Using format custom-formatter, but cannot find the module`
)
);
t.is(actual.stdout, '');
t.is(actual.code, 1);
});

test('should fail for invalid formatters from flags', async t => {
const cwd = await git.bootstrap('fixtures/custom-formatter');
const actual = await cli(['--format', 'through-flag'], {cwd})('foo: bar');
t.true(
actual.stderr.includes(
`Using format through-flag, but cannot find the module`
)
);
t.is(actual.stdout, '');
t.is(actual.code, 1);
});

async function writePkg(payload, options) {
const pkgPath = path.join(options.cwd, 'package.json');
const pkg = JSON.parse(await sander.readFile(pkgPath));
Expand Down

0 comments on commit 1ecf097

Please sign in to comment.