Skip to content

Commit

Permalink
Refactor to simplify based on result being empty when successful.
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidAnson committed Jan 27, 2018
1 parent 528b85d commit 0730386
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 21 deletions.
25 changes: 5 additions & 20 deletions markdownlint.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
var fs = require('fs');
var path = require('path');
var program = require('commander');
var values = require('lodash.values');
var flatten = require('lodash.flatten');
var extend = require('deep-extend');
var markdownlint = require('markdownlint');
Expand Down Expand Up @@ -62,30 +61,17 @@ function lint(lintFiles, config) {
return markdownlint.sync(lintOptions);
}

function printResult(lintResult, hasErrors) {
if (hasErrors) {
console.error(lintResult.toString());
function printResult(lintResult) {
var lintResultString = lintResult.toString();
if (lintResultString) {
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;
return;
}

var result = lintResult.toString();
if (result) {
console.log(result);
}
}

function notEmptyObject(item) {
return Object.keys(item).length > 0;
}

function hasResultErrors(lintResult) {
return values(lintResult).some(notEmptyObject);
}

program
Expand All @@ -101,8 +87,7 @@ var files = prepareFileList(program.args);
if (files && files.length > 0) {
var config = readConfiguration(program);
var lintResult = lint(files, config);
var hasErrors = hasResultErrors(lintResult);
printResult(lintResult, hasErrors);
printResult(lintResult);
} else {
program.help();
}
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
"deep-extend": "~0.4.1",
"glob": "~7.0.3",
"lodash.flatten": "~4.3.0",
"lodash.values": "~4.2.0",
"markdownlint": "~0.7.0",
"rc": "~1.1.6"
},
Expand Down

0 comments on commit 0730386

Please sign in to comment.