all files / lib/common/cli/log/ index.js

38.46% Statements 5/13
0% Branches 0/2
16.67% Functions 1/6
38.46% Lines 5/13
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68    1× 1×             1× 1×                                                                                                             1×  
'use strict';
 
const colors = require('colors');
const _      = require('lodash');
 
/**
 * Class that represents CLI logging.
 */
class Log {
  constructor(sqz) {
    this.sqz = sqz;
    this.logIdentifier = 'squeezer';
    // this.spacer = (text) => text.replace(/^/gm, ' '.repeat(3));
  }
 
  /**
   * Display's a CLI debug message
   * @param {string} msg - debug message
   * @name this.sqz.cli.log.debug
   */
  debug(msg) {
    const params = this.sqz.cli.params.get();
 
    if (_.has(params.options, 'debug')) {
      this.console(`${colors.magenta.bold(`[${this.logIdentifier}] `)}${msg}`);
    }
  }
 
  /**
   * Display's a CLI warning message
   * @param {string} msg - warning message
   * @name this.sqz.cli.log.warn
   */
  warn(msg) {
    this.console(`${colors.red.bold(`[${this.logIdentifier}] `)}${msg}`);
  }
 
  /**
   * Display's a CLI information message
   * @param {string} msg - information message
   * @name this.sqz.cli.log.info
   */
  info(msg) {
    this.console(`${colors.yellow.bold(`[${this.logIdentifier}] `)}${msg}`);
  }
 
  /**
   * Display's a CLI error message ( and exists the CLI with terminal code 1 )
   * @param {string} msg - error message
   * @name this.sqz.cli.log.error
   */
  error(msg) {
    this.console(this.sqz.cli.error.get(colors.yellow(msg)));
    process.exit(1);
  }
 
  /**
   * Use the same NodeJS console.log feature
   * @param {string} msg - information message
   * @name this.sqz.cli.log.console
   */
  console(msg) {
    console.log(msg); // eslint-disable-line no-console
  }
}
 
module.exports = Log;