Skip to content

Commit

Permalink
Merge pull request #58 from jawshooah:json-formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
yaniswang committed Oct 6, 2015
1 parent b778e16 commit 7c5f2f6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module.exports = function(grunt) {
pkg: grunt.file.readJSON('package.json'),
jshint: {
all: {
src: ['Gruntfile.js', 'src/**/*.js', 'test/**/*.js'],
src: ['Gruntfile.js', 'src/**/*.js', 'test/**/*.js', 'bin/*'],
options: {
jshintrc: ".jshintrc"
}
Expand Down
47 changes: 34 additions & 13 deletions bin/htmlhint
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ program
.option('-l, --list', 'show all of the rules available.')
.option('-c, --config <file>', 'custom configuration file.')
.option('-r, --rules <ruleid, ruleid=value ...>', 'set all of the rules available.', map)
.option('-j, --json', 'output messages as raw JSON')
.parse(process.argv);

if(program.list){
Expand All @@ -46,7 +47,9 @@ if(ruleset === undefined){
ruleset = getConfig(program.config);
}

quit(processFiles(arrAllFiles, ruleset));
var jsonOutput = program.json && [];

quit(processFiles(arrAllFiles, ruleset, jsonOutput));

function listRules(){
var rules = HTMLHint.rules;
Expand Down Expand Up @@ -104,38 +107,56 @@ function getFiles(filepath, arrFiles){
}
}

function processFiles(arrFiles, ruleset){
function processFiles(arrFiles, ruleset, jsonOutput){
var exitcode = 0,
allHintCount = 0;
arrFiles.forEach(function(filepath){
var hintCount = hintFile(filepath, ruleset);
var hintCount = hintFile(filepath, ruleset, jsonOutput);
if(hintCount > 0){
exitcode = 1;
allHintCount += hintCount;
}
});
if(allHintCount > 0){
console.log('\r\n%d problems.'.red, allHintCount);
if(jsonOutput){
console.log(JSON.stringify(jsonOutput));
}
else{
console.log('No problem.'.green);
if(allHintCount > 0){
console.log('\r\n%d problems.'.red, allHintCount);
}
else{
console.log('No problem.'.green);
}
}
return exitcode;
}

function hintFile(filepath, ruleset){
function hintFile(filepath, ruleset, jsonOutput){
var html = fs.readFileSync(filepath, 'utf-8');
var messages = HTMLHint.verify(html, ruleset);
if(messages.length > 0){
console.log(filepath+':');
messages.forEach(function(hint){
console.log('\tline %d, col %d: %s', hint.line, hint.col, hint.message[hint.type === 'error'?'red':'yellow']);
});
console.log('');
if(jsonOutput){
logJson(filepath, messages, jsonOutput);
}
else{
logPretty(filepath, messages);
}
}
return messages.length;
}

function logPretty(filepath, messages){
console.log(filepath+':');
messages.forEach(function(hint){
console.log('\tline %d, col %d: %s', hint.line, hint.col, hint.message[hint.type === 'error'?'red':'yellow']);
});
console.log('');
}

function logJson(filepath, messages, jsonOutput){
jsonOutput.push({'file': filepath, 'messages': messages});
}

function quit(code){
if ((!process.stdout.flush || !process.stdout.flush()) && (parseFloat(process.versions.node) < 0.5)) {
process.once("drain", function () {
Expand All @@ -144,4 +165,4 @@ function quit(code){
} else {
process.exit(code || 0);
}
}
}

0 comments on commit 7c5f2f6

Please sign in to comment.