Browse Source

small cleanups to the require structure

v0.7.4-release
Ryan 16 years ago
parent
commit
64117d9c94
  1. 45
      src/main.js

45
src/main.js

@ -39,10 +39,7 @@ node.path = new function () {
var filename = node.path.join(base_directory, name) + ".js"; var filename = node.path.join(base_directory, name) + ".js";
File.exists(filename, function (status) { File.exists(filename, function (status) {
if (status) callback(status ? filename : null);
callback(filename);
else
callback(null);
}); });
} }
@ -53,16 +50,12 @@ node.path = new function () {
this.name = name; this.name = name;
this.target = target; this.target = target;
//puts("new Sub. name = " + name);
this.load = function (base_directory, callback) { this.load = function (base_directory, callback) {
findScript(base_directory, name, function (filename) { findScript(base_directory, name, function (filename) {
if (filename === null) { if (filename === null) {
stderr.puts("Cannot find a script matching: " + name); stderr.puts("Cannot find a script matching: " + name);
process.exit(1); process.exit(1);
} }
//puts("load subscript: " + filename);
//puts(" target = " + target);
loadScript(filename, target, callback); loadScript(filename, target, callback);
}); });
}; };
@ -74,13 +67,13 @@ node.path = new function () {
function Scaffold (source, filename, module) { function Scaffold (source, filename, module) {
// wrap the source in a strange function // wrap the source in a strange function
var source = "function (__filename) {\n" var source = "function (__filename) {"
+ " var on_load;\n" + " var on_load;"
+ " var exports = this;\n" + " var exports = this;"
+ " var require = this.__require;\n" + " var require = this.__require;"
+ " var include = this.__include;\n" + " var include = this.__include;"
+ source + "\n" + source
+ " this.__on_load = on_load;\n" + " this.__on_load = on_load;"
+ "};" + "};"
; ;
// returns the function // returns the function
@ -99,17 +92,10 @@ node.path = new function () {
compiled.apply(module, [filename]); compiled.apply(module, [filename]);
// The module still needs to have its submodules loaded. // The module still needs to have its submodules loaded.
this.module = module; this.module = module;
this.subs = module.__subs; this.subs = module.__subs;
this.on_load = module.__on_load; this.on_load = module.__on_load;
/*
puts("new Scaffold.");
for(var i = 0; i < this.subs.length; i++) {
puts("- subs[" + i.toString() + "] " + this.subs[i].toString());
}
*/
// remove these references so they don't get exported. // remove these references so they don't get exported.
delete module.__subs; delete module.__subs;
delete module.__on_load; delete module.__on_load;
@ -124,20 +110,14 @@ node.path = new function () {
process.exit(1); process.exit(1);
} }
//puts("loadScript: " + filename);
var scaffold = new Scaffold(content, filename, target); var scaffold = new Scaffold(content, filename, target);
function finish() { function finish() {
if (scaffold.on_load instanceof Function) if (scaffold.on_load instanceof Function)
scaffold.on_load(); scaffold.on_load();
else
; //puts("no on_load for " + filename + " .. scaffold.on_load = " + scaffold.on_load);
if (callback instanceof Function) if (callback instanceof Function)
callback(); callback();
else
; //puts("no loadScript callback for " + filename);
} }
// Each time require() or include() was called inside the script // Each time require() or include() was called inside the script
@ -149,9 +129,8 @@ node.path = new function () {
while (scaffold.subs.length > 0) { while (scaffold.subs.length > 0) {
var sub = scaffold.subs.shift(); var sub = scaffold.subs.shift();
sub.load(node.path.dirname(filename), function () { sub.load(node.path.dirname(filename), function () {
//puts("finish sub load: " + sub.name); if(scaffold.subs.length == 0)
if(scaffold.subs.length != 0) return; finish();
finish();
}); });
} }
} }

Loading…
Cancel
Save