Skip to content

Commit

Permalink
Add -s/--stdin option to read from STDIN instead of file|directory|gl…
Browse files Browse the repository at this point in the history
…ob (fixes #32).
  • Loading branch information
DavidAnson committed Mar 15, 2018
1 parent 92aab0e commit 6c63bd5
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 12 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ $ markdownlint --help

-h, --help output usage information
-V, --version output the version number
-s, --stdin read from STDIN (no files)
-c, --config [configFile] configuration file
-i, --ignore [file|directory|glob] files to ignore/exclude
```
Expand Down
28 changes: 17 additions & 11 deletions markdownlint.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
var fs = require('fs');
var path = require('path');
var program = require('commander');
var getStdin = require('get-stdin');
var differenceWith = require('lodash.differencewith');
var flatten = require('lodash.flatten');
var extend = require('deep-extend');
Expand Down Expand Up @@ -62,15 +63,6 @@ function prepareFileList(files) {
});
}

function lint(lintFiles, config) {
var lintOptions = {
resultVersion: 2,
files: lintFiles,
config: config
};
return markdownlint.sync(lintOptions);
}

function printResult(lintResult) {
var results = flatten(Object.keys(lintResult).map(function (file) {
return lintResult[file].map(function (result) {
Expand Down Expand Up @@ -111,6 +103,7 @@ program
.version(pkg.version)
.description(pkg.description)
.usage('[options] <files|directories|globs>')
.option('-s, --stdin', 'read from STDIN (no files)')
.option('-c, --config [configFile]', 'configuration file')
.option('-i, --ignore [file|directory|glob]', 'files to ignore/exclude', concatArray, []);

Expand All @@ -124,10 +117,23 @@ var diff = differenceWith(files, ignores, function (a, b) {
return paths.original;
});

if (files.length > 0) {
function lintAndPrint(stdin, files) {
var config = readConfiguration(program);
var lintResult = lint(diff, config);
var lintOptions = {
config: config,
files: files || [],
strings: {
stdin: stdin || ''
}
};
var lintResult = markdownlint.sync(lintOptions);
printResult(lintResult);
}

if ((files.length > 0) && !program.stdin) {
lintAndPrint(null, diff);
} else if ((files.length === 0) && program.stdin) {
getStdin().then(lintAndPrint);
} else {
program.help();
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"dependencies": {
"commander": "~2.9.0",
"deep-extend": "~0.4.1",
"get-stdin": "~5.0.1",
"glob": "~7.0.3",
"lodash.differencewith": "~4.5.0",
"lodash.flatten": "~4.4.0",
Expand Down
47 changes: 46 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import path from 'path';
import test from 'ava';
import execa from 'execa';

const errorPattern = /\.(md|markdown): \d+: MD\d{3}/gm;
const errorPattern = /(\.md|\.markdown|stdin): \d+: MD\d{3}/gm;

test('--version option', async t => {
const result = await execa('../markdownlint.js', ['--version']);
Expand All @@ -20,6 +20,18 @@ test('--help option', async t => {
t.true(result.stderr === '');
});

test('no files shows help', async t => {
const result = await execa('../markdownlint.js', []);
t.true(result.stdout.indexOf('--help') >= 0);
t.true(result.stderr === '');
});

test('files and --stdin shows help', async t => {
const result = await execa('../markdownlint.js', ['--stdin', 'correct.md']);
t.true(result.stdout.indexOf('--help') >= 0);
t.true(result.stderr === '');
});

test('linting of correct Markdown file yields no output', async t => {
const result = await execa('../markdownlint.js',
['--config', 'test-config.json', 'correct.md']);
Expand Down Expand Up @@ -209,3 +221,36 @@ test('glob linting does not try to lint directories as files', async t => {
t.true(err.stderr.match(errorPattern).length > 0);
}
});

test('--stdin with empty input has no output', async t => {
var input = '';
const result = await execa('../markdownlint.js', ['--stdin'], {input: input});
t.true(result.stdout === '');
t.true(result.stderr === '');
});

test('--stdin with valid input has no output', async t => {
var input = [
'# Heading',
'',
'Text'
].join('\n');
const result = await execa('../markdownlint.js', ['--stdin'], {input: input});
t.true(result.stdout === '');
t.true(result.stderr === '');
});

test('--stdin with invalid input reports violations', async t => {
var input = [
'Heading',
'',
'Text '
].join('\n');
try {
await execa('../markdownlint.js', ['--stdin'], {input: input});
t.fail();
} catch (err) {
t.true(err.stdout === '');
t.true(err.stderr.match(errorPattern).length === 2);
}
});

0 comments on commit 6c63bd5

Please sign in to comment.