all files / cli/help/ index.js

15% Statements 6/40
0% Branches 0/26
50% Functions 1/2
15.38% Lines 6/39
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 69 70 71 72 73 74 75 76 77 78 79 80 81 82    1× 1×   1×   1×                   1×                                                                                                                             1×  
'use strict';
 
const appRoot = require('app-root-path');
const colors  = require('colors');
 
const _       = require('lodash');
 
const bin     = Object.keys(require(`${appRoot}/package.json`).bin)[0];
 
/**
 * Class which builds the help message  .
 */
class Help {
  /**
   * @param {Object} sqz - Squeezer CLI instance
   */
  constructor(sqz) {
    this.sqz = sqz;
  }
 
  /**
   * Return current version information
   * @returns {Object}
   * @name this.sqz.cli.help.get
   */
  get(commands, param) {
    const cmd        = commands[param];
    const spacer     = ' '.repeat(1);
    const spacerSub  = ' '.repeat(4);
    const desc       = cmd.description || cmd.summary;
    let msg          = '';
    let maxOptionLen = 0;
 
    msg += `${spacer}${colors.yellow('\n Usage: ')}\n\n`;
    msg += `${spacerSub}$ ${bin} ${param} ${cmd.options ? '[options]' : ''}\n\n`;
 
    msg += `${spacer}${colors.yellow('Description:')}\n\n`;
    msg += `${desc.replace(/^/gm, spacerSub)}\n\n`;
 
    if (cmd.options || cmd.flags) {
      msg += `${spacer}${colors.yellow('Options:')}\n\n`;
 
      _.forEach(cmd.options, (value, key) => {
        let keyLen = key.length;
        if (value.flag) {
          keyLen += 6;
        }
        if (keyLen > maxOptionLen) maxOptionLen = keyLen;
      });
 
      const fullSpacer = ' '.repeat((maxOptionLen) + 5);
      let leftSpacer   = '';
 
      _.forEach(cmd.options, (value, key) => {
        const optionDisplay = `--${key}` +
          `${value.flag ? ` / -${value.flag} ` : ''}`;
 
        leftSpacer = ' '.repeat((maxOptionLen - optionDisplay.length) + 3);
 
        msg += `${spacerSub}${colors.green(optionDisplay)}`;
        msg += `${leftSpacer}${value.title} ${value.required ? '(required)' : '(optional)'}`;
        msg += `${value.defaultValue ? colors.blue.bold(` "${value.defaultValue}"`) : ''}\n`;
        msg += `${value.value ? `${' '.repeat(4)}value` : ''}`;
        msg += `${value.description ? `\n${value.description.replace(/^/gm, fullSpacer)}\n` : ''} `;
        msg += '\n';
      });
    }
 
    if (cmd.examples && cmd.examples.length > 0) {
      msg += `\n${spacer}${colors.yellow('Examples:')}\n\n`;
 
      _.forEach(cmd.examples, (example) => {
        msg += `${spacerSub}$ ${bin} ${param} ${example}\n`;
      });
    }
 
    return msg;
  }
}
 
module.exports = Help;