Skip to content

Commit

Permalink
add formatter.js
Browse files Browse the repository at this point in the history
  • Loading branch information
yaniswang committed Oct 25, 2015
1 parent 6d4d6b0 commit 55464e9
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions bin/formatter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
var path = require('path');
var events = require('events');
var glob = require('glob');

var mapFormatters;
var arrSupportedFormatters;
var HTMLHint;
var options;

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

// 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;
}

var formatter =new events.EventEmitter();
formatter.getSupported = function(){
return arrSupportedFormatters;
}
formatter.init = function(tmpHTMLHint, tmpOptions){
HTMLHint = tmpHTMLHint;
options = tmpOptions;
}
formatter.setFormat = function(format){
var formatHandel = mapFormatters[format];
if(formatHandel === undefined){
console.log('No supported formatter, supported formatters: %s'.red, arrSupportedFormatters.join(', '));
process.exit(1);
}
else{
formatHandel(formatter, HTMLHint, options)
}
}

module.exports = formatter;

0 comments on commit 55464e9

Please sign in to comment.