From 7ae54e204e998679fe9806cbe0ce34485c337a54 Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Sat, 25 Aug 2018 18:46:33 +0200 Subject: [PATCH] Fix streaming: add `--stdin` flag Closes GH-189. --- cli.js | 16 ++++++++++++---- test/cli.js | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 4 deletions(-) diff --git a/cli.js b/cli.js index e00b8a9..aed3213 100755 --- a/cli.js +++ b/cli.js @@ -51,12 +51,13 @@ var cli = meow( ' -q, --quiet output only warnings and errors', ' -t, --text treat input as plain-text (not markdown)', ' -d, --diff ignore unchanged lines (affects Travis only)', + ' --stdin read from stdin', '', 'When no input files are given, searches for markdown and text', 'files in the current directory, `doc`, and `docs`.', '', 'Examples', - ' $ echo "His network looks good" | alex', + ' $ echo "His network looks good" | alex --stdin', ' $ alex *.md !example.md', ' $ alex' ].join('\n'), @@ -64,6 +65,7 @@ var cli = meow( flags: { version: {type: 'boolean', alias: 'v'}, help: {type: 'boolean', alias: 'h'}, + stdin: {type: 'boolean'}, text: {type: 'boolean', alias: 't'}, diff: {type: 'boolean', alias: 'd'}, quiet: {type: 'boolean', alias: 'q'}, @@ -73,10 +75,16 @@ var cli = meow( ) /* Set-up. */ -var globs = ['{docs/**/,doc/**/,}*.{' + extensions.join(',') + '}'] +var defaultGlobs = ['{docs/**/,doc/**/,}*.{' + extensions.join(',') + '}'] +var globs -/* istanbul ignore else - Bug in tests. Something hangs, at least. */ -if (cli.input.length !== 0) { +if (cli.flags.stdin) { + if (cli.input.length !== 0) { + throw new Error('Do not pass globs with `--stdin`') + } +} else if (cli.input.length === 0) { + globs = defaultGlobs +} else { globs = cli.input } diff --git a/test/cli.js b/test/cli.js index 5465c3d..33d5812 100644 --- a/test/cli.js +++ b/test/cli.js @@ -25,6 +25,33 @@ test('help', function(t) { }) }) +test('stdin', function(t) { + return execa + .stderr('./cli.js', ['--stdin'], {input: 'His'}) + .catch(function(err) { + t.is( + err.stderr, + [ + '', + ' 1:1-1:4 warning `His` may be insensitive, use `Their`, `Theirs`, `Them` instead her-him retext-equality', + '', + '⚠ 1 warning', + '' + ].join('\n') + ) + }) +}) + +test('stdin and globs', function(t) { + var rp = path.join('test', 'fixtures', 'one.md') + + return execa + .stderr('./cli.js', ['--stdin', rp], {input: 'His'}) + .catch(function(err) { + t.regex(err.stderr, /Do not pass globs with `--stdin`/) + }) +}) + test('markdown by default', function(t) { var rp = path.join('test', 'fixtures', 'one.md') @@ -107,3 +134,23 @@ test('non-binary (optional)', function(t) { t.is(err.stderr, expected) }) }) + +test('default globs', function(t) { + return execa.stderr('./cli.js').catch(function(err) { + var expected = [ + 'code-of-conduct.md', + ' 1:1 error Cannot process specified file: it’s ignored', + '', + 'contributing.md: no issues found', + 'example.md', + ' 1:1 error Cannot process specified file: it’s ignored', + '', + 'readme.md: no issues found', + '', + '✖ 2 errors', + '' + ].join('\n') + + t.is(err.stderr, expected) + }) +})