All files / src index.js

100% Statements 70/70
90.91% Branches 40/44
100% Functions 15/15
100% Lines 62/62

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 1452x 2x 2x 2x 2x 2x 2x 2x     2x           2x 11x 1x 10x 4x 9x 5x 8x 2x     6x             2x 7x 6x             1x     4x   2x 1x     1x             2x             16x 4x 2x   2x     2x 8x 5x 2x 6x   2x     8x     10x 8x 8x   1x 1x   1x 1x   1x 1x   5x         3x     10x 8x 8x   1x 1x   1x 1x   1x 1x   5x         3x     2x                                  
import os from 'os';
import child from 'child_process';
import Promise from 'bluebird';
import F from './functional';
import { hasSubstr, stringify } from './utilities';
import linux from './linux';
import macOS from './macOS';
import windows from './windows';
const { tautology, thrower } = F;
const { compose, composeP } = F.R;
child.exec = Promise.promisify(child.exec, { context: child });
 
// --------------------------------------
// Common Core - Validation
// --------------------------------------
 
const validateDev = (dev) => {
  if(typeof dev === 'function') {
    return dev;
  }else if(typeof dev === 'string') {
    return (v, k) => hasSubstr(k, dev);
  }else if(dev instanceof RegExp) {
    return (v, k) => dev.test(k);
  }else if(typeof dev === 'undefined' || dev === null) {
    return tautology;
  }
 
  thrower(
    `fs.filesystem expected first argument 'dev' to be a function, string, regex or undefined/null. ` +
    `Found ${typeof dev === 'object' ? dev.constructor.name : typeof dev} instead.`,
    TypeError
  );
};
 
const validateCallback = (cb) => {
  if(typeof cb !== 'function') {
    thrower(
      `fs.filesystem expected second argument 'callback' to be instanceof function. ` +
      `Found ${typeof cb === 'object' ? cb.constructor.name : typeof cb} instead.`,
      TypeError
    );
  }
 
  return cb;
};
 
const validate = (validateDev, validateCallback) =>
  (dev, callback) => {
    if(typeof dev === 'function' && !callback) {
      return [ tautology, dev ];
    }
 
    return [ validateDev(dev), validateCallback(callback) ];
  };
 
// --------------------------------------
// Common Core - Main & Export Functions
// --------------------------------------
 
const ENVIRONMENT = {
  env: {
    LANG: 'en_US.UTF-8',
    PATH: process.env.PATH,
  },
};
 
const execute = (cmd, parser) => (filter, cb, sync = false) => {
  if(sync) {
    return compose(parser(filter), stringify, child.execSync)(cmd, ENVIRONMENT);
  }
  return composeP((v) => cb(null, v), parser(filter), stringify, child.exec)(cmd, ENVIRONMENT).catch(cb);
};
 
const inferDevSize = (result) => {
  for(const k in result){
    if(result[k].size === null) {
      var sizeSum = result[k].volumes.reduce((size, vol) => {
        return size + vol.space.total;
      }, 0);
      result[k].size = sizeSum;
    }
  }
  return result;
};
 
const filesystem = (macOS, linux, windows, validate, platform) => (dev, callback) => {
  var devices = null;
  switch(platform) {
  case 'darwin':
    devices = macOS(...validate(dev, callback)).devices;
    break;
  case 'linux':
    devices = linux(...validate(dev, callback)).devices;
    break;
  case 'win32':
    devices = windows(...validate(dev, callback)).devices;
    break;
  default:
    thrower(
      'fs.filesystem : Unsupported OS. fs.filesystem does not support ' +
      `${platform} at the moment`
    );
  }
  return inferDevSize(devices);
};
 
const filesystemSync = (macOS, linux, windows, validateDev, platform) => (dev) => {
  var devices = null;
  switch(platform){
  case 'darwin':
    devices = macOS(validateDev(dev), null, true).devices;
    break;
  case 'linux':
    devices = linux(validateDev(dev), null, true).devices;
    break;
  case 'win32':
    devices = windows(validateDev(dev), null, true).devices;
    break;
  default:
    thrower(
      'fs.filesystem : Unsupported OS. fs.filesystem does not support ' +
      `${platform} at the moment`
    );
  }
  return inferDevSize(devices);
};
 
const sync = filesystemSync(
  execute(macOS.COMMAND, macOS.parser),
  execute(linux.COMMAND, linux.parser),
  execute(windows.COMMAND, windows.parser),
  validateDev,
  os.platform()
);
 
export{ sync as filesystemSync, inferDevSize, ENVIRONMENT };
 
export default filesystem(
  execute(macOS.COMMAND, macOS.parser),
  execute(linux.COMMAND, linux.parser),
  execute(windows.COMMAND, windows.parser),
  validate(validateDev, validateCallback),
  os.platform()
);