Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implementation seems to work but no tests yet
- Loading branch information
James Halliday
committed
Jun 18, 2011
1 parent
7885443
commit 5218f01
Showing
2 changed files
with
82 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
|
||
// http://nodejs.org/docs/v0.4.8/api/all.html#all_Together... | ||
|
||
var core = [ 'assert', 'buffer', 'child_process', 'crypto', 'dgram', 'dns', | ||
'events', 'fs', 'http', 'https', 'net', 'os', 'path', 'querystring', 'repl', | ||
'stream', 'sys', 'tls', 'tty', 'url', 'util', 'vm' | ||
].reduce(function (acc, x) { acc[x] = true; return acc }, {}); | ||
|
||
exports.sync = function (x, opts) { | ||
if (core[x]) return x; | ||
|
||
if (!opts) opts = {}; | ||
var y = opts.path || require.cache[__filename].parent.filename; | ||
|
||
if (x.match(/^(?:\.\/|\/|\.\.\/)/)) { | ||
var m = loadAsFileSync(y + x) || loadAsDirectorySync(y + x); | ||
if (m) return m; | ||
} | ||
|
||
var n = loadNodeModulesSync(x, path.dirname(y)); | ||
if (n) return n; | ||
|
||
throw new Error("Cannot find module '" + x + "'"); | ||
} | ||
|
||
function loadAsFileSync (x) { | ||
if (path.existsSync(x) && fs.statSync(x).isFile()) { | ||
return x; | ||
} | ||
else if (path.existsSync(x + '.js') && fs.statSync(x + '.js').isFile()) { | ||
return x + '.js'; | ||
} | ||
} | ||
|
||
function loadAsDirectorySync (x) { | ||
var pkgfile = x + '/package.json'; | ||
if (path.existsSync(pkgfile) && fs.statSync(pkgfile).isFile()) { | ||
var body = fs.readFileSync(pkgfile, 'utf8'); | ||
try { | ||
var pkg = JSON.parse(body); | ||
if (pkg.main) { | ||
var m = loadAsFileSync(path.resolve(x, pkg.main)); | ||
if (m) return m; | ||
} | ||
} | ||
catch (err) {} | ||
} | ||
|
||
return loadAsFileSync(x + '/index'); | ||
} | ||
|
||
function loadNodeModulesSync (x, start) { | ||
var dirs = nodeModulesPathsSync(start); | ||
for (var i = 0; i < dirs.length; i++) { | ||
var dir = dirs[i]; | ||
var m = loadAsFileSync(dir + '/' + x); | ||
if (m) return m; | ||
var n = loadAsDirectorySync(dir + '/' + x); | ||
if (n) return n; | ||
} | ||
} | ||
|
||
function nodeModulesPathsSync (start) { | ||
var parts = start.split(/\/+/); | ||
var root = parts.indexOf('node_modules'); | ||
if (root < 0) root = 0; | ||
|
||
var dirs = []; | ||
for (var i = parts.length - 1; i >= root; i--) { | ||
if (parts[i] === 'node_modules') continue; | ||
var dir = parts.slice(0, i + 1).join('/') + '/node_modules'; | ||
dirs.push(dir); | ||
} | ||
return dirs; | ||
} |