Skip to content

Commit 9d12b8f

Browse files
Danny McCormicksindresorhus
authored andcommitted
Add reporter option (sindresorhus#55)
1 parent 7a52420 commit 9d12b8f

File tree

4 files changed

+47
-4
lines changed

4 files changed

+47
-4
lines changed

cli.js

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,42 @@ const awesomeLint = require('.');
88
const main = async () => {
99
const cli = meow(`
1010
Usage
11-
$ awesome-lint
12-
`);
11+
$ awesome-lint <optional input filename>
12+
13+
Options
14+
--reporter, -r Use a custom reporter
15+
`, {
16+
flags: {
17+
reporter: {
18+
type: 'string',
19+
alias: 'r'
20+
}
21+
}
22+
});
1323

1424
const options = { };
1525
const input = cli.input[0];
26+
const reporterName = cli.flags.reporter;
1627

1728
if (input) {
1829
options.filename = input;
1930
} else {
2031
options.filename = findReadmeFile(process.cwd());
2132
}
2233

34+
if (reporterName) {
35+
// Check if reporter is an npm package
36+
try {
37+
options.reporter = require(reporterName).report;
38+
} catch (error) {
39+
if (error.code === 'MODULE_NOT_FOUND') {
40+
console.log(`No reporter found matching "${reporterName}". Proceeding with default reporter (vfile-reporter-pretty)`);
41+
} else {
42+
throw error;
43+
}
44+
}
45+
}
46+
2347
await awesomeLint.report(options);
2448
};
2549

index.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ lint.report = async options => {
5858

5959
if (messages.length === 0) {
6060
spinner.succeed();
61+
62+
if (options.reporter) {
63+
console.log(options.reporter([]));
64+
}
65+
6166
return;
6267
}
6368

@@ -69,7 +74,9 @@ lint.report = async options => {
6974
process.exitCode = 1;
7075

7176
file.path = path.basename(file.path);
72-
console.log(vfileReporterPretty([file]));
77+
78+
const reporter = options.reporter || vfileReporterPretty;
79+
console.log(reporter([file]));
7380
};
7481

7582
module.exports = lint;

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ Returns a `Promise` for a [`VFile`](https://github.com/wooorm/vfile).
8888

8989
#### awesomeLint.report()
9090

91-
Show the lint output.
91+
Show the lint output. This can be custom reported by setting `options.reporter=<function>` and passing in `options` as a parameter.
9292

9393

9494
## Maintainers

test/api.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,15 @@ import m from '..';
44
test('main', async t => {
55
t.true((await m({filename: 'test/fixtures/main.md'})).messages.length > 0);
66
});
7+
8+
test('`reporter` option', async t => {
9+
let wasReporterCalled = false;
10+
const reporter = reports => {
11+
if (reports.length > 0) {
12+
wasReporterCalled = true;
13+
}
14+
};
15+
16+
await m.report({filename: 'test/fixtures/main.md', reporter});
17+
t.true(wasReporterCalled);
18+
});

0 commit comments

Comments
 (0)