Skip to content

Instantly share code, notes, and snippets.

@aconbere
Created December 2, 2010 04:16
Show Gist options
  • Select an option

  • Save aconbere/724759 to your computer and use it in GitHub Desktop.

Select an option

Save aconbere/724759 to your computer and use it in GitHub Desktop.
// needs to handle the following cases
//
// require("a.<ext>")
// -> a.<ext>
//
// require("a")
// -> a
// -> a.<ext>
// -> a/index.<ext>
function findModulePath (request, paths) {
paths = request.charAt(0) === "/" ? [""] : paths;
var fs = requireNative('fs')
, exts = Object.keys(extensions);
var tryFile = function (requestPath) {
debug(requestPath);
try {
stats = fs.statSync(requestPath);
if (stats && !stats.isDirectory()) {
return requestPath;
}
} catch(e) {}
return false;
};
var tryExtensions = function (p) {
debug(JSON.stringify(exts));
for (var i = 0; i < exts.length; i++) {
f = tryFile(p + exts[i]);
if (f) return f;
}
};
for (var i = 0, PL = paths.length; i < PL; i++) {
var p = paths[i];
, f = tryFile(path.join(p, request)) ||
tryExtensions(path.join(p, request))
tryExtensions(path.join(p, request, "index"));
if (f) { return f; }
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment