From 9d12b8fc63e63beef74f5f858896b88e946d9550 Mon Sep 17 00:00:00 2001 From: Danny McCormick Date: Wed, 13 Feb 2019 02:49:14 -0500 Subject: [PATCH] Add `reporter` option (#55) --- cli.js | 28 ++++++++++++++++++++++++++-- index.js | 9 ++++++++- readme.md | 2 +- test/api.js | 12 ++++++++++++ 4 files changed, 47 insertions(+), 4 deletions(-) diff --git a/cli.js b/cli.js index 3c1cd71..bc50813 100755 --- a/cli.js +++ b/cli.js @@ -8,11 +8,22 @@ const awesomeLint = require('.'); const main = async () => { const cli = meow(` Usage - $ awesome-lint - `); + $ awesome-lint + + 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; @@ -20,6 +31,19 @@ const main = async () => { 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); }; diff --git a/index.js b/index.js index 0116a3f..d26fdd0 100644 --- a/index.js +++ b/index.js @@ -58,6 +58,11 @@ lint.report = async options => { if (messages.length === 0) { spinner.succeed(); + + if (options.reporter) { + console.log(options.reporter([])); + } + return; } @@ -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; diff --git a/readme.md b/readme.md index dd318cb..604d494 100644 --- a/readme.md +++ b/readme.md @@ -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=` and passing in `options` as a parameter. ## Maintainers diff --git a/test/api.js b/test/api.js index 58318f3..e35c796 100644 --- a/test/api.js +++ b/test/api.js @@ -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); +});