all files / lib/ logger.js

85.71% Statements 18/21
62.5% Branches 5/8
80% Functions 4/5
85.71% Lines 18/21
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52                                                                   
 
require('colors');
var _ = require('lodash');
 
var messageTypes = {
  success: '[SUCCESS]'.green,
  error: '['.red + 'ERROR'.underline.red + ']'.red,
  info: '[INFO]'.blue,
  warning: '[WARN]'.yellow,
  debug: '[DEBUG]'.gray
};
 
function showMessage() {
  var args = Array.prototype.slice.call(arguments);
 
  Iif (args[1].length === 0) {
    return console.log();
  }
 
  console.log.apply(console, [args[0]].concat(Array.prototype.slice.call(args[1])));
}
 
var debug = false;
var methods = function() {
  showMessage('>', arguments);
};
 
_.forEach(messageTypes, function(prompt, name) {
  methods[name] = function() {
    Iif (name == 'debug' && !debug) {
      // do nothing if debug is not enabled
      return;
    }
    showMessage(prompt, arguments);
  };
});
 
module.exports = function(program) {
  /*
   * Enables/Disables 'debug' method
   */
  var argv = program.normalize(process.argv);
 
  program.option('-d, --debug', 'enable debugger');
  debug = program.debug = argv.indexOf('-d') > -1 || argv.indexOf('--debug') > -1; // Need this early
 
  /*
   * Inject methods
   */
  program.log = methods;
};