All files / util require-search.js

100% Statements 11/11
80% Branches 4/5
100% Functions 3/3
100% Lines 11/11
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23      1x     6x 14x 14x 6x   6x 9x 9x 9x 5x         1x    
import debug from 'debug';
import path from 'path';
 
const d = debug('electron-forge:require-search');
 
export default (relativeTo, paths) => {
  const testPaths = paths
    .concat(paths.map(mapPath => path.resolve(relativeTo, mapPath)))
    .concat(paths.map(mapPath => path.resolve(relativeTo, 'node_modules', mapPath)));
  d('searching', testPaths, 'relative to', relativeTo);
  let result;
  for (const testPath of testPaths) {
    try {
      d('testing', testPath);
      result = require(testPath);
      return typeof result === 'object' && result && result.default ? result.default : result;
    } catch (err) {
      // Ignore the error
    }
  }
  d('failed to find a module in', testPaths);
};