Skip to content

Commit

Permalink
Add support for custom formatters
Browse files Browse the repository at this point in the history
Closes GH-308.
  • Loading branch information
k-yle committed Nov 13, 2020
1 parent f9bcc22 commit facc438
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 21 deletions.
41 changes: 20 additions & 21 deletions cli.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/env node
'use strict'

var PassThrough = require('stream').PassThrough
var notifier = require('update-notifier')
var supportsColor = require('supports-color')
var meow = require('meow')
var engine = require('unified-engine')
var unified = require('unified')
Expand All @@ -13,7 +13,7 @@ var mdx = require('remark-mdx')
var english = require('retext-english')
var remark2retext = require('remark-retext')
var rehype2retext = require('rehype-retext')
var report = require('vfile-reporter')
var defaultReporter = require('vfile-reporter')
var equality = require('retext-equality')
var profanities = require('retext-profanities')
var diff = require('unified-diff')
Expand Down Expand Up @@ -43,13 +43,14 @@ var cli = meow(
'',
'Options:',
'',
' -w, --why output sources (when available)',
' -q, --quiet output only warnings and errors',
' -t, --text treat input as plain-text (not markdown)',
' -l, --html treat input as html (not markdown)',
' --mdx treat input as mdx (not markdown)',
' -d, --diff ignore unchanged lines (affects Travis only)',
' --stdin read from stdin',
' -w, --why output sources (when available)',
' -q, --quiet output only warnings and errors',
' -t, --text treat input as plain-text (not markdown)',
' -l, --html treat input as html (not markdown)',
' --mdx treat input as mdx (not markdown)',
' -d, --diff ignore unchanged lines (affects Travis only)',
' --reporter=REPORTER use a custom vfile-reporter',
' --stdin read from stdin',
'',
'When no input files are given, searches for markdown and text',
'files in the current directory, `doc`, and `docs`.',
Expand All @@ -68,6 +69,7 @@ var cli = meow(
mdx: {type: 'boolean'},
html: {type: 'boolean', alias: 'l'},
diff: {type: 'boolean', alias: 'd'},
reporter: {type: 'string'},
quiet: {type: 'boolean', alias: 'q'},
why: {type: 'boolean', alias: 'w'}
}
Expand Down Expand Up @@ -101,26 +103,23 @@ engine(
files: globs,
extensions: extensions,
configTransform: transform,
output: false,
out: false,
streamError: new PassThrough(),
output: false,
rcName: '.alexrc',
packageField: 'alex',
color: supportsColor.stderr,
reporter: cli.flags.reporter || defaultReporter,
reporterOptions: {
verbose: cli.flags.why
},
quiet: cli.flags.quiet,
ignoreName: '.alexignore',
silentlyIgnore: silentlyIgnore,
frail: true,
defaultConfig: transform()
},
function (err, code, result) {
var out = report(err || result.files, {
verbose: cli.flags.why,
quiet: cli.flags.quiet
})

if (out) {
console.error(out)
}

function (err, code) {
if (err) console.error(err.message)
process.exit(code)
}
)
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
"remark-preset-wooorm": "^7.0.0",
"tape": "^5.0.0",
"tinyify": "^3.0.0",
"vfile-reporter-json": "^2.0.0",
"xo": "^0.32.0"
},
"scripts": {
Expand Down
41 changes: 41 additions & 0 deletions test/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,47 @@ test('alex-cli', function (t) {
}
})

t.test('custom reporter', function (t) {
var fp = path.join('test', 'fixtures', 'profanity-sureness', 'two.md')

t.plan(1)

childProcess.exec('./cli.js --reporter json ' + fp, onexec)

const expectedJson = JSON.stringify([
{
path: fp,
cwd: process.cwd(),
history: [fp],
messages: []
}
])

function onexec(err, stdout, stderr) {
t.deepEqual(
[err, stdout, stderr],
[null, '', expectedJson + '\n'],
'should work'
)
}
})

t.test("custom formatter that isn't installed", function (t) {
var fp = path.join('test', 'fixtures', 'profanity-sureness', 'two.md')

t.plan(1)

childProcess.exec('./cli.js --reporter doesntexist ' + fp, onexec)

function onexec(err, stdout, stderr) {
t.deepEqual(
[err, stderr, stdout],
[null, 'Could not find reporter `doesntexist`\n', ''],
'should work'
)
}
})

t.test('deny', function (t) {
var fp = path.join('test', 'fixtures', 'deny', 'two.md')

Expand Down

0 comments on commit facc438

Please sign in to comment.