All files / piscosour/lib params.js

87.5% Statements 49/56
63.64% Branches 14/22
85.71% Functions 6/7
87.5% Lines 49/56
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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128    1x 1x 1x 1x   1x                               1x                                             1x 1x 3x 1x 1x   1x 1x       1x     1x 1x     1x   1x 1x 1x 2x 1x 1x     1x     1x     1x     1x 1x   1x 1x 1x     1x                   1x 1x 1x 1x 1x     1x 1x 20x 5x     1x 1x     1x 1x 1x 1x   1x     1x  
'use strict';
 
const nopt = require('nopt');
const _ = require('lodash');
const paramUtils = require('./utils/paramUtils');
const fsUtils = require('./utils/fsUtils');
 
let knownOpts = {
  'onlyLocal': Boolean,
  'junitReport': Boolean,
  'help': Boolean,
  'functionalTests': Boolean,
  'all': Boolean,
  'version': Boolean,
  'list': ['all', 'recipes', 'flows', 'steps', 'contexts'],
  'output': ['verbose', 'debug', 'silly'],
  'initStep': [String, null],
  'endStep': [String, null],
  'paramsFile': [String, Array],
  'writeCache': Boolean,
  'saveRequirements': Boolean,
  'showContext': Boolean
};
const shortHands = {
  'u': [ '--junitReport' ],
  'ol': [ '--onlyLocal' ],
  'p': [ '--paramsFile' ],
  'w': [ '--writeCache' ],
  'sr': [ '--saveRequirements' ],
  'c': [ '--showContext' ],
  'i': [ '--initStep' ],
  'e': [ '--endStep' ],
  'h': [ '--help' ],
  'ft': [ '--functionalTests' ],
  'a': [ '--all' ],
  'la': ['--list', 'all'],
  'lr': ['--list', 'recipes'],
  'lst': ['--list', 'flows'],
  'lsh': ['--list', 'steps'],
  'lt': ['--list', 'contexts'],
  'ov': ['--output', 'verbose'],
  'od': ['--output', 'debug'],
  'os': ['--output', 'silly'],
  'v': [ '--version' ]
};
 
const defaultString = function(opts) {
  process.argv.forEach((option) => {
    if (option.startsWith('--')) {
      const inopt = option.replace('--', '');
      Iif (inopt.startsWith('b-')) {
        opts[ inopt ] = Boolean;
      } else Eif (!opts[ inopt ]) {
        opts[ inopt ] = [String, null];
      }
    }
  });
  return opts;
};
 
knownOpts = defaultString(knownOpts);
const paramsOpt = nopt(knownOpts, shortHands, process.argv, 2);
 
 
const getParams = function() {
 
  const init = function() {
    let result = {};
    for (const name in paramsOpt) {
      if (name !== 'argv') {
        let param = name;
        Iif (name.startsWith('b-')) {
          param = name.replace('b-', '');
        }
        Iif (name.includes('.')) {
          result = paramUtils.refactorObjectsFromCommandLine(result, param, paramsOpt[ name ]);
        }
        result[ param ] = paramsOpt[ name ];
      }
    }
    return result;
  };
 
  const params = init();
  let origParams = _.cloneDeep(params);
 
  const merge = function(orig) {
    orig = paramUtils.mergeObjects(origParams, orig, paramUtils.mergeLodash);
    return orig;
  };
 
  Iif (origParams.paramsFile) {
    origParams.paramsFile.forEach((paramsFile) => {
      if (fsUtils.exists(paramsFile)) {
        origParams = paramUtils.mergeObjects(origParams, fsUtils.readConfig(paramsFile), paramUtils.mergeLodash);
      } else {
        console.warn(`WARNING: paramsFile is defined but file ${paramsFile} doesn't exist!`);
      }
    });
  }
 
  const info = function(opt) {
    const ev = knownOpts[ opt ];
    let res = '';
    Eif (Object.prototype.toString.call(ev) === '[object Array]' && typeof ev[ 0 ] === 'string') {
      res = '( ' + ev + ' )';
    }
 
    res += ' [';
    for (const short in shortHands) {
      if (shortHands[ short ][ 0 ] === '--' + opt) {
        res += ' -' + short;
      }
    }
    res += ']';
    return res;
  };
 
  params.knownOpts = knownOpts;
  params.merge = merge;
  params.info = info;
  params.commands = paramsOpt.argv.remain;
 
  return params;
};
 
module.exports = getParams();