Skip to content

Commit

Permalink
Add reporter option (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
Danny McCormick authored and sindresorhus committed Feb 13, 2019
1 parent 7a52420 commit 9d12b8f
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 4 deletions.
28 changes: 26 additions & 2 deletions cli.js
Expand Up @@ -8,18 +8,42 @@ const awesomeLint = require('.');
const main = async () => {
const cli = meow(`
Usage
$ awesome-lint
`);
$ awesome-lint <optional input filename>
Options
--reporter, -r Use a custom reporter
`, {
flags: {
reporter: {
type: 'string',
alias: 'r'
}
}
});

const options = { };
const input = cli.input[0];
const reporterName = cli.flags.reporter;

if (input) {
options.filename = input;
} else {
options.filename = findReadmeFile(process.cwd());
}

if (reporterName) {
// Check if reporter is an npm package
try {
options.reporter = require(reporterName).report;
} catch (error) {
if (error.code === 'MODULE_NOT_FOUND') {
console.log(`No reporter found matching "${reporterName}". Proceeding with default reporter (vfile-reporter-pretty)`);
} else {
throw error;
}
}
}

await awesomeLint.report(options);
};

Expand Down
9 changes: 8 additions & 1 deletion index.js
Expand Up @@ -58,6 +58,11 @@ lint.report = async options => {

if (messages.length === 0) {
spinner.succeed();

if (options.reporter) {
console.log(options.reporter([]));
}

return;
}

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

file.path = path.basename(file.path);
console.log(vfileReporterPretty([file]));

const reporter = options.reporter || vfileReporterPretty;
console.log(reporter([file]));
};

module.exports = lint;
2 changes: 1 addition & 1 deletion readme.md
Expand Up @@ -88,7 +88,7 @@ Returns a `Promise` for a [`VFile`](https://github.com/wooorm/vfile).

#### awesomeLint.report()

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


## Maintainers
Expand Down
12 changes: 12 additions & 0 deletions test/api.js
Expand Up @@ -4,3 +4,15 @@ import m from '..';
test('main', async t => {
t.true((await m({filename: 'test/fixtures/main.md'})).messages.length > 0);
});

test('`reporter` option', async t => {
let wasReporterCalled = false;
const reporter = reports => {
if (reports.length > 0) {
wasReporterCalled = true;
}
};

await m.report({filename: 'test/fixtures/main.md', reporter});
t.true(wasReporterCalled);
});

0 comments on commit 9d12b8f

Please sign in to comment.