Skip to content

Commit 1ecf097

Browse files
byCedricmarionebl
authored andcommitted
feat(cli): add format option for report output
1 parent b0e63d9 commit 1ecf097

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
formatter: 'custom-formatter',
3+
rules: {
4+
'type-enum': [2, 'never', ['foo']]
5+
}
6+
};

@commitlint/cli/src/cli.js

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#!/usr/bin/env node
22
require('babel-polyfill'); // eslint-disable-line import/no-unassigned-import
33

4-
const format = require('@commitlint/format');
54
const load = require('@commitlint/load');
65
const lint = require('@commitlint/lint');
76
const read = require('@commitlint/read');
@@ -63,6 +62,12 @@ const flags = {
6362
description: 'lower end of the commit range to lint; applies if edit=false',
6463
type: 'string'
6564
},
65+
format: {
66+
alias: 'o',
67+
default: null,
68+
description: 'output format of the results',
69+
type: 'string'
70+
},
6671
'parser-preset': {
6772
alias: 'p',
6873
description: 'configuration preset to use for conventional-commits-parser',
@@ -135,6 +140,7 @@ async function main(options) {
135140
const loaded = await load(getSeed(flags), loadOpts);
136141
const parserOpts = selectParserOpts(loaded.parserPreset);
137142
const opts = parserOpts ? {parserOpts} : {parserOpts: {}};
143+
const format = loadFormatter(loaded, flags);
138144

139145
// Strip comments if reading from `.git/COMMIT_EDIT_MSG`
140146
if (range.edit) {
@@ -248,6 +254,19 @@ function selectParserOpts(parserPreset) {
248254
return parserPreset.parserOpts;
249255
}
250256

257+
function loadFormatter(config, flags) {
258+
const moduleName = flags.format || config.formatter;
259+
let modulePath;
260+
261+
try {
262+
modulePath = require.resolve(`${moduleName}`);
263+
} catch (error) {
264+
throw new Error(`Using format ${moduleName}, but cannot find the module.`);
265+
}
266+
267+
return require(modulePath);
268+
}
269+
251270
// Catch unhandled rejections globally
252271
process.on('unhandledRejection', (reason, promise) => {
253272
console.log('Unhandled Rejection at: Promise ', promise, ' reason: ', reason);

@commitlint/cli/src/cli.test.js

+24
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,30 @@ test('should not print full commit message when input succeeds', async t => {
239239
t.is(actual.code, 0);
240240
});
241241

242+
test('should fail for invalid formatters from configuration', async t => {
243+
const cwd = await git.bootstrap('fixtures/custom-formatter');
244+
const actual = await cli([], {cwd})('foo: bar');
245+
t.true(
246+
actual.stderr.includes(
247+
`Using format custom-formatter, but cannot find the module`
248+
)
249+
);
250+
t.is(actual.stdout, '');
251+
t.is(actual.code, 1);
252+
});
253+
254+
test('should fail for invalid formatters from flags', async t => {
255+
const cwd = await git.bootstrap('fixtures/custom-formatter');
256+
const actual = await cli(['--format', 'through-flag'], {cwd})('foo: bar');
257+
t.true(
258+
actual.stderr.includes(
259+
`Using format through-flag, but cannot find the module`
260+
)
261+
);
262+
t.is(actual.stdout, '');
263+
t.is(actual.code, 1);
264+
});
265+
242266
async function writePkg(payload, options) {
243267
const pkgPath = path.join(options.cwd, 'package.json');
244268
const pkg = JSON.parse(await sander.readFile(pkgPath));

0 commit comments

Comments
 (0)