all files / src/ arg-parser.js

92.31% Statements 36/39
92.86% Branches 13/14
100% Functions 5/5
92.31% Lines 36/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                                  14× 14× 39×         12× 12× 12× 12× 12× 23× 11× 11×   23×     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 bin = {
  node: 'node',
  iojs: 'iojs',
  esw: 'esw'
};
 
var getPath = function(options){
  logger.debug('GetPath: %s', options.format);
  return path.join(__dirname, formatterPath, options.format);
};
 
var contains = function(str, items){
  logger.debug('Contains: %s', str);
  return _.some(items, function(item){
    return str.indexOf(item) >= 0;
  });
};
 
module.exports = {
  parse: function (args, options) {
    var arr = [];
    var dirs = options._;
    var formatSpecified = false;
    logger.debug('Directories to check: %o', dirs);
    _.each(args, function(item){
      if (!keys[item] && !formats[item] && !bin[item] && !contains(item, [bin.esw, bin.iojs, bin.node])) {
        logger.debug('Pushing item: %s', item);
        arr.push(item);
      }
      if (formats[item]) {
        formatSpecified = true;
        logger.debug('Format specified');
        arr.push(getPath(options));
      }
    });
    Iif (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;
  }
};