Skip to content

Instantly share code, notes, and snippets.

@fictorial
Created November 13, 2009 19:34
Show Gist options
  • Save fictorial/234093 to your computer and use it in GitHub Desktop.
Save fictorial/234093 to your computer and use it in GitHub Desktop.
GLOBAL.import_package = function(name) {
var package = {};
require("posix").readdir(name).wait()
.filter(function (filename) {
return filename.match(/^(.+)\.js$/);
})
.forEach(function (filename) {
var basename = filename.split(/^(.+)\.js$/)[1];
package[basename] = require("./" + name + "/" + basename);
});
return package;
};
// in test/foo.js:
// this.bar = function () { return 42 }
//
// in runme.js:
// var pkg = import_package("test");
// require("sys").debug(pkg.foo.bar());
//
// $ node runme.js
// DEBUG: 42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment