Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add CLI option to output messages as raw JSON #58

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
}
2 changes: 1 addition & 1 deletion coverage.html

Large diffs are not rendered by default.

Loading