all files / lib/ files.js

100% Statements 44/44
86.67% Branches 13/15
100% Functions 13/13
100% Lines 43/43
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                        14×     158× 158×     130×     159×     14× 42×       14×       16×       28× 28× 132×   130×   26×     61× 56×      
'use strict';
 
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getRoot = getRoot;
exports.findSubDirectories = findSubDirectories;
exports.notSubDirectory = notSubDirectory;
exports.isSubDirectory = isSubDirectory;
exports.reducePaths = reducePaths;
 
var _lodash = require('lodash');
 
var _lodash2 = _interopRequireDefault(_lodash);
 
var _path = require('path');
 
var _path2 = _interopRequireDefault(_path);
 
var _os = require('os');
 
var _os2 = _interopRequireDefault(_os);
 
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
 
function flatten(a, b) {
  return a.concat(b);
}
 
function hasParent(parent) {
  var root = getRoot(parent);
  return parent && parent !== root && parent !== '.';
}
 
function getParent(dir) {
  return _path2.default.dirname(dir);
}
 
function getRoot(dir) {
  return _os2.default.platform() === 'win32' ? dir.split(_path2.default.sep)[0] + _path2.default.sep : _path2.default.sep;
}
 
function getSubDirectories(base, allPaths) {
  return allPaths.filter(function (candidate) {
    return base !== candidate && isSubDirectory(base, candidate);
  });
}
 
function findSubDirectories(paths) {
  return paths.map(function (path) {
    return getSubDirectories(path, paths);
  }).reduce(flatten, []);
}
 
function notSubDirectory(subDirs) {
  return function (path) {
    return !_lodash2.default.includes(subDirs, path);
  };
}
 
function isSubDirectory(base, candidate) {
  var parent = candidate;
  while (hasParent(parent)) {
    if (base === parent) {
      return true;
    }
    parent = getParent(parent);
  }
  return false;
}
 
function reducePaths(searchPaths) {
  if (searchPaths.length === 1) {
    return searchPaths;
  }
 
  var subDirs = findSubDirectories(searchPaths.sort());
  return searchPaths.filter(notSubDirectory(subDirs));
}