Skip to content

Commit

Permalink
Add -o/--output option to write to file instead of STDERR (fixes #15).
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidAnson committed Mar 17, 2018
1 parent 6c63bd5 commit e9c49dc
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ $ markdownlint --help
-h, --help output usage information
-V, --version output the version number
-s, --stdin read from STDIN (no files)
-o, --output [outputFile] write issues to file (no console)
-c, --config [configFile] configuration file
-i, --ignore [file|directory|glob] files to ignore/exclude
```
Expand Down
15 changes: 13 additions & 2 deletions markdownlint.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,22 +76,32 @@ function printResult(lintResult) {
};
});
}));
var lintResultString = '';
if (results.length > 0) {
results.sort(function (a, b) {
return a.file.localeCompare(b.file) || a.lineNumber - b.lineNumber ||
a.names.localeCompare(b.names) || a.description.localeCompare(b.description);
});
var lintResultString = results.map(function (result) {
lintResultString = results.map(function (result) {
return result.file + ': ' + result.lineNumber + ': ' + result.names + ' ' + result.description;
}).join('\n');
console.error(lintResultString);
// Note: process.exit(1) will end abruptly, interrupting asynchronous IO
// streams (e.g., when the output is being piped). Just set the exit code
// and let the program terminate normally.
// @see {@link https://nodejs.org/dist/latest-v8.x/docs/api/process.html#process_process_exit_code}
// @see {@link https://github.com/igorshubovych/markdownlint-cli/pull/29#issuecomment-343535291}
process.exitCode = 1;
}
if (program.output) {
try {
fs.writeFileSync(program.output, lintResultString);
} catch (err) {
console.warn('Cannot write to output file ' + program.output + ': ' + err.message);
process.exitCode = 2;
}
} else if (lintResultString) {
console.error(lintResultString);
}
}

function concatArray(item, array) {
Expand All @@ -104,6 +114,7 @@ program
.description(pkg.description)
.usage('[options] <files|directories|globs>')
.option('-s, --stdin', 'read from STDIN (no files)')
.option('-o, --output [outputFile]', 'write issues to file (no console)')
.option('-c, --config [configFile]', 'configuration file')
.option('-i, --ignore [file|directory|glob]', 'files to ignore/exclude', concatArray, []);

Expand Down
59 changes: 59 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

import fs from 'fs';
import path from 'path';
import test from 'ava';
import execa from 'execa';
Expand Down Expand Up @@ -254,3 +255,61 @@ test('--stdin with invalid input reports violations', async t => {
t.true(err.stderr.match(errorPattern).length === 2);
}
});

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

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

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

test('--output with invalid path fails', async t => {
var input = '';
var output = 'invalid/outputD.txt';
try {
await execa('../markdownlint.js',
['--stdin', '--output', output], {input: input});
t.fail();
} catch (err) {
t.true(err.stdout === '');
t.true(err.stderr.replace(/: ENOENT[^]*$/, '') === 'Cannot write to output file ' + output);
t.throws(() => fs.accessSync(output, 'utf8'), Error);
}
});

0 comments on commit e9c49dc

Please sign in to comment.