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);
};

@@ -104,24 +104,41 @@
 %0Aexport 
-default 
+function requireSearchRaw
 (relativ
@@ -148,19 +148,16 @@
 , paths)
- =%3E
  %7B%0A  con
@@ -392,22 +392,8 @@
 o);%0A
-  let result;%0A
   fo
@@ -474,22 +474,20 @@
       re
-sult =
+turn
  require
@@ -502,103 +502,8 @@
 h);%0A
-      return typeof result === 'object' && result && result.default ? result.default : result;%0A
     
@@ -600,11 +600,199 @@
 Paths);%0A
+%7D%0A%0Aexport default (relativeTo, paths) =%3E %7B%0A  const result = requireSearchRaw(relativeTo, paths);%0A  return typeof result === 'object' && result && result.default ? result.default : result;%0A
 %7D;%0A

extend requireSearch to export a raw search fn