Skip to content

Commit

Permalink
add cli parameter:--nocolor, disable color in cli
Browse files Browse the repository at this point in the history
  • Loading branch information
yaniswang committed Oct 25, 2015
1 parent de90962 commit 6d4d6b0
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 116 deletions.
1 change: 1 addition & 0 deletions CHANGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ add:
1. change cli parameter: `--plugin` to `--rulesdir`
2. add formatter directory support
3. add formatters: compact, markdown
4. add cli parameter:`--nocolor`, disable color in cli

## ver 0.9.10 (2015-10-12)

Expand Down
10 changes: 5 additions & 5 deletions bin/formatters/checkstyle.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
*/
var xml = require('xml');

var formatter = {
onEnd: function(hintInfo){
var checkstyleFormatter = function(formatter, HTMLHint, options){
formatter.on('end', function(event){
var arrFiles = [];
var arrAllMessages = hintInfo.arrAllMessages;
var arrAllMessages = event.arrAllMessages;
arrAllMessages.forEach(function(fileInfo){
var arrMessages = fileInfo.messages;
var arrErrors = [];
Expand Down Expand Up @@ -47,6 +47,6 @@ var formatter = {
declaration: true,
indent: ' '
}));
}
});
};
module.exports = formatter;
module.exports = checkstyleFormatter;
24 changes: 13 additions & 11 deletions bin/formatters/compact.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,27 @@
* Copyright (c) 2015, Yanis Wang <yanis.wang@gmail.com>
* MIT Licensed
*/
var formatter = {
onFileHint: function(result){
result.messages.forEach(function (message) {
var compactFormatter = function(formatter, HTMLHint, options){
var nocolor = options.nocolor;
formatter.on('file', function(event){
event.messages.forEach(function (message) {
console.log('%s: line %d, col %d, %s - %s (%s)',
result.file,
event.file,
message.line,
message.col,
message.type,
message.message,
message.rule.id
);
);
});
},
onEnd: function(hintInfo){
var allHintCount = hintInfo.allHintCount;
});
formatter.on('end', function(event){
var allHintCount = event.allHintCount;
if(allHintCount > 0){
console.log('');
console.log('%d problems', hintInfo.allHintCount);
var message = '%d problems';
console.log(nocolor ? message : message.red, event.allHintCount);
}
}
});
};
module.exports = formatter;
module.exports = compactFormatter;
46 changes: 26 additions & 20 deletions bin/formatters/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,42 @@
* Copyright (c) 2015, Yanis Wang <yanis.wang@gmail.com>
* MIT Licensed
*/
var formatter = {
onStart: function(){
var defaultFormatter = function(formatter, HTMLHint, options){
var nocolor = options.nocolor;
formatter.on('start', function(){
console.log('');
},
onConfigLoaded: function(config, configPath){
console.log(' Config loaded: %s', configPath.cyan);
});
formatter.on('config', function(event){
var configPath = event.configPath;
console.log(' Config loaded: %s', nocolor ? configPath : configPath.cyan);
console.log('');
},
onFileHint: function(result, HTMLHint){
console.log(' '+result.file.white);
var arrLogs = HTMLHint.format(result.messages, {
colors: true,
});
formatter.on('file', function(event){
console.log(' '+event.file.white);
var arrLogs = HTMLHint.format(event.messages, {
colors: nocolor ? false : true,
indent: 6
});
arrLogs.forEach(function(str){
console.log(str);
});
console.log('');
},
onEnd: function(hintInfo){
var allFileCount = hintInfo.allFileCount;
var allHintCount = hintInfo.allHintCount;
var allHintFileCount = hintInfo.allHintFileCount;
var time = hintInfo.time;
});
formatter.on('end', function(event){
var allFileCount = event.allFileCount;
var allHintCount = event.allHintCount;
var allHintFileCount = event.allHintFileCount;
var time = event.time;
var message;
if(allHintCount > 0){
console.log('Scan %d files, found %d errors in %d files (%d ms)'.red, allFileCount, allHintCount, allHintFileCount, time);
message = 'Scan %d files, found %d errors in %d files (%d ms)';
console.log(nocolor ? message : message.red, allFileCount, allHintCount, allHintFileCount, time);
}
else{
console.log('Scan %d files, without errors (%d ms).'.green, allFileCount, time);
message = 'Scan %d files, without errors (%d ms).';
console.log(nocolor ? message : message.green, allFileCount, time);
}
}
});
};
module.exports = formatter;

module.exports = defaultFormatter;
10 changes: 5 additions & 5 deletions bin/formatters/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
* Copyright (c) 2015, Yanis Wang <yanis.wang@gmail.com>
* MIT Licensed
*/
var formatter = {
onEnd: function(hintInfo){
console.log(JSON.stringify(hintInfo.arrAllMessages));
}
var jsonFormatter = function(formatter, HTMLHint, options){
formatter.on('end', function(event){
console.log(JSON.stringify(event.arrAllMessages));
});
};
module.exports = formatter;
module.exports = jsonFormatter;
14 changes: 7 additions & 7 deletions bin/formatters/junit.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
*/
var xml = require('xml');

var formatter = {
onEnd: function(hintInfo, HTMLHint){
var junitFormatter = function(formatter, HTMLHint, options){
formatter.on('end', function(event){
var arrTestcase = [];
var arrAllMessages = hintInfo.arrAllMessages;
var arrAllMessages = event.arrAllMessages;
arrAllMessages.forEach(function(fileInfo){
var arrMessages = fileInfo.messages;
var arrLogs = HTMLHint.format(arrMessages);
Expand Down Expand Up @@ -37,8 +37,8 @@ var formatter = {
{
_attr: {
name: 'HTMLHint Tests',
time: (hintInfo.time / 1000).toFixed(3),
tests: hintInfo.allFileCount,
time: (event.time / 1000).toFixed(3),
tests: event.allFileCount,
failures: arrAllMessages.length
}
}
Expand All @@ -50,6 +50,6 @@ var formatter = {
declaration: true,
indent: ' '
}));
}
});
};
module.exports = formatter;
module.exports = junitFormatter;
10 changes: 5 additions & 5 deletions bin/formatters/markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
* Copyright (c) 2015, Yanis Wang <yanis.wang@gmail.com>
* MIT Licensed
*/
var formatter = {
onEnd: function(hintInfo, HTMLHint){
var markdownFormatter = function(formatter, HTMLHint, options){
formatter.on('end', function(event){
console.log('# TOC');
var arrToc = [];
var arrContents = [];
var arrAllMessages = hintInfo.arrAllMessages;
var arrAllMessages = event.arrAllMessages;
arrAllMessages.forEach(function(fileInfo){
var filePath = fileInfo.file;
var arrMessages = fileInfo.messages;
Expand Down Expand Up @@ -35,6 +35,6 @@ var formatter = {
});
console.log(arrToc.join('\r\n')+'\r\n');
console.log(arrContents.join('\r\n'));
}
});
};
module.exports = formatter;
module.exports = markdownFormatter;
22 changes: 12 additions & 10 deletions bin/formatters/unix.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,25 @@
* Copyright (c) 2015, Yanis Wang <yanis.wang@gmail.com>
* MIT Licensed
*/
var formatter = {
onFileHint: function(result){
result.messages.forEach(function (message) {
var unixFormatter = function(formatter, HTMLHint, options){
var nocolor = options.nocolor;
formatter.on('file', function(event){
event.messages.forEach(function (message) {
console.log([
result.file,
event.file,
message.line,
message.col,
" " + message.message + ' ['+message.type+'/'+message.rule.id+']'
].join(":"));
});
},
onEnd: function(hintInfo){
var allHintCount = hintInfo.allHintCount;
});
formatter.on('end', function(event){
var allHintCount = event.allHintCount;
if(allHintCount > 0){
console.log('');
console.log('%d problems', hintInfo.allHintCount);
var message = '%d problems';
console.log(nocolor ? message : message.red, event.allHintCount);
}
}
});
};
module.exports = formatter;
module.exports = unixFormatter;
80 changes: 27 additions & 53 deletions bin/htmlhint
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var glob = require("glob");
var parseGlob = require('parse-glob');

var HTMLHint = require("../index").HTMLHint;
var formatter = require('./formatter');
var pkg = require('../package.json');

require('colors');
Expand Down Expand Up @@ -38,14 +39,7 @@ program.on('--help', function(){
console.log('');
});

// load custom formatters
var mapFormatters = loadFormatters();
var arrSupportedFormatters = [];
for(var formatterName in mapFormatters){
if(formatterName !== 'default'){
arrSupportedFormatters.push(formatterName);
}
}
var arrSupportedFormatters = formatter.getSupported();

program
.version(pkg.version)
Expand All @@ -56,6 +50,7 @@ program
.option('-R, --rulesdir <file|folder>', 'load custom rules from file or folder')
.option('-f, --format <'+arrSupportedFormatters.join('|')+'>', 'output messages as custom format')
.option('-i, --ignore <pattern, pattern ...>', 'add pattern to exclude matches')
.option('--nocolor', 'disable color')
.parse(process.argv);

if(program.list){
Expand All @@ -68,12 +63,13 @@ if(arrTargets.length === 0){
arrTargets.push('./');
}

// check format
// init formatter
formatter.init(HTMLHint, {
'nocolor': program.nocolor
});
var format = program.format || 'default';
var formatter = mapFormatters[format];
if(formatter === undefined){
console.log('No supported formatter, supported formatters: %s'.red, arrSupportedFormatters.join(', '));
process.exit(1);
if(format){
formatter.setFormat(format);
}

hintTargets(arrTargets, {
Expand Down Expand Up @@ -111,9 +107,8 @@ function hintTargets(arrTargets, options){
}

// start hint
if(formatter.onStart){
formatter.onStart();
}
formatter.emit('start');

var arrTasks = [];
arrTargets.forEach(function(target){
arrTasks.push(function(next){
Expand All @@ -129,37 +124,17 @@ function hintTargets(arrTargets, options){
async.series(arrTasks, function(){
// end hint
var spendTime = new Date().getTime() - startTime;
if(formatter.onEnd){
formatter.onEnd({
arrAllMessages: arrAllMessages,
allFileCount: allFileCount,
allHintFileCount: allHintFileCount,
allHintCount: allHintCount,
time: spendTime
}, HTMLHint);
}
formatter.emit('end', {
arrAllMessages: arrAllMessages,
allFileCount: allFileCount,
allHintFileCount: allHintFileCount,
allHintCount: allHintCount,
time: spendTime
});
process.exit(allHintCount > 0 ? 1: 0);
});
}

// load all formatters
function loadFormatters(){
var arrFiles = glob.sync('./formatters/*.js', {
'cwd': __dirname,
'dot': false,
'nodir': true,
'strict': false,
'silent': true
});
var mapFormatters = {};
arrFiles.forEach(function(file){
var fileInfo = path.parse(file);
var formatterPath = path.resolve(__dirname, file);
mapFormatters[fileInfo.name] = require(formatterPath);
});
return mapFormatters;
}

// load custom rles
function loadCustomRules(rulesdir){
rulesdir = rulesdir.replace(/\\/g, '/');
Expand Down Expand Up @@ -219,13 +194,11 @@ function hintAllFiles(target, options, onFinised){
var spendTime = new Date().getTime() - startTime;
var hintCount = messages.length;
if(hintCount > 0){
if(formatter.onFileHint){
formatter.onFileHint({
'file': filepath,
'messages': messages,
'time': spendTime
}, HTMLHint);
}
formatter.emit('file', {
'file': filepath,
'messages': messages,
'time': spendTime
});
arrTargetMessages.push({
'file': filepath,
'messages': messages,
Expand Down Expand Up @@ -318,9 +291,10 @@ function getConfig(configPath, base, formatter){
ruleset;
try{
ruleset = JSON.parse(stripJsonComments(config));
if(formatter.onConfigLoaded){
formatter.onConfigLoaded(ruleset, configPath);
}
formatter.emit('config', {
ruleset: ruleset,
configPath: configPath
});
}
catch(e){}
return ruleset;
Expand Down

0 comments on commit 6d4d6b0

Please sign in to comment.