all files / src/ arg-parser.js

100% Statements 36/36
100% Branches 12/12
100% Functions 3/3
100% Lines 36/36
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                            12× 12× 12× 12× 12× 12× 12× 12×   12×     12×   12× 11× 11×   12×      
'use strict';
var path = require('path');
var _ = require('lodash');
var logger = require('./log')('arg-parser');
logger.debug('Loaded');
 
var simpleDetail = 'simple-detail';
var formatterPath = 'formatters';
 
var defaultPath = './';
var formatKey = '-f';
var keys = {
  '-w': true,
  '--watch': true
};
var formats = { // still don't like this can cause too much duplication
  'simple': true,
  'simple-success': true,
  'simple-detail': true
};
 
var getPath = function(options){
  logger.debug('GetPath: %s', options.format);
  return path.join(__dirname, formatterPath, options.format);
};
 
module.exports = {
  parse: function (cliArgs, options) {
    var arr = [];
    var dirs = options._;
    var formatSpecified = false;
    var args = _.slice(cliArgs, 2, cliArgs.length);
    logger.debug('Directories to check: %o', dirs);
    logger.debug('Args %o', args);
    _.each(args, function(item){
      if (!keys[item] && !formats[item]) {
        logger.debug('Pushing item: %s', item);
        arr.push(item);
      }
      if (formats[item]) {
        formatSpecified = true;
        logger.debug('Format specified');
        arr.push(getPath(options));
      }
    });
    if (options.format === simpleDetail && !formatSpecified) {
      logger.debug('setting custom formatter');
      arr.push(formatKey);
      arr.push(getPath(options));
    }
    if (!dirs.length) {
      arr.push(defaultPath);
      logger.debug('Setting default path: %s', defaultPath);
    }
    return arr;
  }
};