Created
July 7, 2010 07:24
-
-
Save creationix/466415 to your computer and use it in GitHub Desktop.
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
var FS = require('fs'), | |
PATH = require('path'); | |
var libs = {}; | |
// Include node's built-in modules | |
// NOTE: process.binding is a node private API, this means it may change without notice. | |
Object.keys(process.binding('natives')).forEach(function (name) { | |
libs[name] = name; | |
}); | |
// Include libs in the require paths | |
require.paths.forEach(function (path) { | |
if (PATH.existsSync(path)) { | |
FS.readdirSync(path).forEach(function (filename) { | |
var fullPath = PATH.join(path, filename); | |
if (FS.statSync(fullPath).isDirectory()) { | |
if (PATH.existsSync(PATH.join(fullPath, "index.js"))) { | |
libs[filename] = fullPath; | |
} | |
if (PATH.existsSync(PATH.join(fullPath, "index.node"))) { | |
libs[filename] = fullPath; | |
} | |
return; | |
} | |
var p = filename.lastIndexOf('.'); | |
var extension = filename.substr(p); | |
if (extension === '.js' || extension === ".node") { | |
libs[filename.substr(0, p)] = fullPath.substr(0, fullPath.lastIndexOf('.')); | |
} | |
}); | |
} | |
}); | |
// Set up lazy loaders | |
Object.keys(libs).forEach(function (lib) { | |
var name = lib.toUpperCase(); | |
var path = libs[lib]; | |
var code = function () { | |
// When called, replace the property with the real deal | |
delete global[name]; | |
return global[name] = require(path); | |
}; | |
global.__defineGetter__(name, code); | |
}); |
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
require('./magic'); | |
// Assuming Connect is in your require.paths somewhere... | |
// The first reference to "CONNECT" will require('connect') and replace | |
// the CONNECT variable with the module itself. | |
CONNECT.createServer(CONNECT.logger(), CONNECT.staticProvider(__dirname)).listen(8080); | |
SYS.puts("Starting magical connect server at http://localhost:8080/magic.js"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment