Created
December 2, 2010 04:36
-
-
Save aconbere/724784 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // needs to handle the following cases | |
| // | |
| // require("a.<ext>") | |
| // -> a.<ext> | |
| // | |
| // require("a") | |
| // -> a | |
| // -> a.<ext> | |
| // -> a/index.<ext> | |
| function findModulePath (request, paths) { | |
| var fs = requireNative('fs') | |
| , exts = Object.keys(extensions); | |
| paths = request.charAt(0) === "/" ? [""] : paths; | |
| // check if the file exists and is not a directory | |
| var tryFile = function (requestPath) { | |
| try { | |
| stats = fs.statSync(requestPath); | |
| if (stats && !stats.isDirectory()) { | |
| return requestPath; | |
| } | |
| } catch(e) {} | |
| return false; | |
| }; | |
| // given a path check a the file exists with any of the set extensions | |
| var tryExtensions = function (p, extension) { | |
| for (var i = 0, EL = exts.length; i < EL; i++) { | |
| f = tryFile(p + exts[i]); | |
| if (f) { return f; } | |
| } | |
| return false; | |
| }; | |
| // For each path | |
| for (var i = 0, PL = paths.length; i < PL; i++) { | |
| var p = paths[i] | |
| // try to join the request to the path | |
| , f = tryFile(path.join(p, request)) || | |
| // try it with each of the extensions | |
| tryExtensions(path.join(p, request)) || | |
| // try it with each of the extensions at "index" | |
| 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