All files / lib cli-views.js

97.06% Statements 33/34
90.91% Branches 10/11
100% Functions 6/6
100% Lines 25/25
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 411x 1x 1x   1x 1x 1x 1x         1x   1x 1x 1x   1x 5x           5x   5x 4x 3x 2x 1x     1x 1x 1x 1x 1x 1x  
const c = require('chalk');
const _ = require('lodash');
const bytes = require('bytes');
 
const syntheticView = stat => {
  const size = bytes(stat.size);
  const gzip = bytes(stat.gzip);
  return `${c.bold.underline(stat.name)} (${c.grey.dim(stat.version)}) has ${c.blue.bold(
    stat.dependencyCount
  )} dependencies for a weight of ${c.cyan.bold(size)} (${c.dim.bold(gzip)} gzipped)`;
};
// TODO: later dynamic color + kilo printing + plural
const jsonView = JSON.stringify;
 
const sizeView = stat => stat.size;
const gzipsizeView = stat => stat.gzip;
const dependenciesView = stat => stat.dependencyCount;
 
const getView = (argv = {}) => {
  const viewOpts = _.intersection(_.keys(argv).filter(key => argv[key]), [
    'json',
    'gzip-size',
    'size',
    'dependencies'
  ]);
  Iif (_.size(viewOpts) > 1) throw new Error(`Can't use in the same time options ${viewOpts}`);
 
  if (argv.json) return jsonView;
  if (argv['gzip-size']) return gzipsizeView;
  if (argv.size) return sizeView;
  if (argv.dependencies) return dependenciesView;
  return syntheticView;
};
 
module.exports.syntheticView = syntheticView;
module.exports.jsonView = jsonView;
module.exports.sizeView = sizeView;
module.exports.gzipsizeView = gzipsizeView;
module.exports.dependenciesView = dependenciesView;
module.exports.getView = getView;