1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Make node loading more asynchronouse

This commit is contained in:
Nick O'Leary 2014-05-02 11:16:07 +01:00
parent 4dda4aeef2
commit 84093bcb6e
2 changed files with 206 additions and 105 deletions

View File

@ -18,6 +18,9 @@ var EventEmitter = require("events").EventEmitter;
var fs = require("fs"); var fs = require("fs");
var path = require("path"); var path = require("path");
var clone = require("clone"); var clone = require("clone");
var when = require("when");
var whenNode = require('when/node');
var events = require("./events"); var events = require("./events");
var storage = null; var storage = null;
var settings = null; var settings = null;
@ -208,109 +211,206 @@ function createNode(node,def) {
} }
function load(_settings) { function load(_settings) {
settings = _settings; return when.promise(function(resolve,reject) {
settings = _settings;
var RED = require("./red.js"); var RED = require("./red.js");
function loadNode(nodeDir, nodeFn) { function loadTemplate(templateFilename) {
return when.promise(function(resolve,reject) {
if (settings.nodesExcludes) { whenNode.call(fs.readFile,templateFilename,'utf8').done(function(content) {
for (var i=0;i<settings.nodesExcludes.length;i++) { node_type_registry.registerConfig(content);
if (settings.nodesExcludes[i] == nodeFn) { resolve();
return; }, function(err) {
reject("missing template file");
});
});
}
function loadNode(nodeDir, nodeFn) {
return when.promise(function(resolve,reject) {
if (settings.nodesExcludes) {
for (var i=0;i<settings.nodesExcludes.length;i++) {
if (settings.nodesExcludes[i] == nodeFn) {
resolve();
return;
}
}
} }
} var nodeFilename = path.join(nodeDir,nodeFn);
} var templateFilename = nodeFilename.replace(/\.js$/,".html");
var nodeFilename = path.join(nodeDir,nodeFn); var r = require(nodeFilename);
var r = require(nodeFilename); if (typeof r === "function") {
if (typeof r === "function") { try {
r(RED); var promise = r(RED);
} if (promise != null && typeof promise.then === "function") {
var templateFilename = nodeFilename.replace(/\.js$/,".html"); promise.then(function() {
if (fs.existsSync(templateFilename)) { resolve(loadTemplate(templateFilename));
node_type_registry.registerConfig(fs.readFileSync(templateFilename,'utf8')); },function(err) {
} else { reject(err);
util.log("["+type+"] missing template file: "+templateFilename); });
} } else {
} resolve(loadTemplate(templateFilename));
}
} catch(err) {
reject(err);
}
} else {
resolve(loadTemplate(templateFilename));
}
});
function scanForNodes(dir) { }
var pm = path.join(dir,"node_modules");
if (fs.existsSync(pm)) { function loadNodesFromModule(moduleDir,pkg) {
fs.readdirSync(pm).filter(function(fn) { var nodes = pkg['node-red'].nodes||{};
var pkgfn = path.join(pm,fn,"package.json"); var promises = [];
if (fs.existsSync(pkgfn)) { for (var n in nodes) {
var pkg = require(pkgfn); promises.push(when.promise(function(resolve) {
if (pkg['node-red']) { loadNode(moduleDir,nodes[n]).then(resolve, function(err) {
console.log(pkg.name,pkg.version); resolve({'fn':pkg.name+":"+n,err:err});
var nr = pkg['node-red']; });
if (nr.nodes) { }));
var nrn = nr.nodes; }
for (var i in nrn) { return when.promise(function(resolve,reject) {
console.log(" ",i,":",nrn[i]); var errors = [];
try { when.settle(promises).then(function(results) {
var nodeDir = path.join(pm,fn); var errors = [];
loadNode(nodeDir,nrn[i]); results.forEach(function(result) {
} catch(err) { if (result.state == 'fulfilled' && result.value) {
util.log("["+i+"] "+err); errors = errors.concat(result.value);
//console.log(err.stack); }
});
resolve(errors);
});
});
}
function scanForNodes(dir) {
return when.promise(function(resolve,reject) {
var pm = path.join(dir,"node_modules");
var promises = [];
promises.push(when.promise(function(resolve,reject) {
whenNode.call(fs.readdir,pm).then(function(files) {
var promises = [];
files.forEach(function(fn) {
var pkgfn = path.join(pm,fn,"package.json");
try {
var pkg = require(pkgfn);
if (pkg['node-red']) {
var moduleDir = path.join(pm,fn);
promises.push(loadNodesFromModule(moduleDir,pkg));
}
} catch(err) {
if (err.code != "MODULE_NOT_FOUND") {
// TODO: handle unexpected error
} }
} }
} });
} when.settle(promises).then(function(results) {
var errors = [];
results.forEach(function(result) {
if (result.state == 'fulfilled' && result.value) {
errors = errors.concat(result.value);
}
});
resolve(errors);
});
},function(err) {
resolve([]);
})
}));
var up = path.resolve(path.join(dir,".."));
if (up !== dir) {
promises.push(scanForNodes(up))
} }
when.settle(promises).then(function(results) {
var errors = [];
results.forEach(function(result) {
if (result.state == 'fulfilled' && result.value) {
errors = errors.concat(result.value);
}
});
resolve(errors);
});
}); });
} }
var up = path.join(dir,".."); function loadNodes(dir) {
if (up !== dir) { return when.promise(function(resolve,reject) {
scanForNodes(up); var promises = [];
}
}
function loadNodes(dir) { whenNode.call(fs.readdir,dir).done(function(files) {
var errors = []; files = files.sort();
if (fs.existsSync(dir)) { files.forEach(function(fn) {
fs.readdirSync(dir).sort().filter(function(fn){ var stats = fs.statSync(path.join(dir,fn));
var stats = fs.statSync(path.join(dir,fn)); if (stats.isFile()) {
if (stats.isFile()) { if (/\.js$/.test(fn)) {
if (/\.js$/.test(fn)) { promises.push(when.promise(function(resolve,reject) {
try { loadNode(dir,fn).then(resolve, function(err) {
loadNode(dir,fn); resolve({'fn':fn,err:err});
} catch(err) { });
errors.push({fn:fn, err:err}); }));
//util.log("["+fn+"] "+err); }
//console.log(err.stack); } else if (stats.isDirectory()) {
// Ignore /.dirs/, /lib/ /node_modules/
if (!/^(\..*|lib|icons|node_modules|test)$/.test(fn)) {
promises.push(when.promise(function(resolve,reject) {
loadNodes(path.join(dir,fn)).then(function(errs) {
resolve(errs);
});
}));
} else if (fn === "icons") {
events.emit("node-icon-dir",path.join(dir,fn));
} }
} }
} else if (stats.isDirectory()) { });
// Ignore /.dirs/, /lib/ /node_modules/
if (!/^(\..*|lib|icons|node_modules|test)$/.test(fn)) { when.settle(promises).then(function(results) {
errors = errors.concat(loadNodes(path.join(dir,fn))); var errors = [];
} else if (fn === "icons") { results.forEach(function(result) {
events.emit("node-icon-dir",path.join(dir,fn)); if (result.state == 'fulfilled' && result.value) {
} errors = errors.concat(result.value);
} }
});
resolve(errors);
});
}, function(err) {
resolve([]);
// non-existant dir
});
}); });
} }
return errors;
}
var errors = loadNodes(__dirname+"/../nodes");
scanForNodes(__dirname+"/../nodes");
if (settings.nodesDir) {
var dir = settings.nodesDir;
if (typeof settings.nodesDir == "string") {
dir = [dir];
}
for (var i=0;i<dir.length;i++) { var promises = [];
errors = errors.concat(loadNodes(dir[i])); promises.push(loadNodes(__dirname+"/../nodes"));
scanForNodes(dir[i]); promises.push(scanForNodes(__dirname+"/../nodes"));
if (settings.nodesDir) {
var dir = settings.nodesDir;
if (typeof settings.nodesDir == "string") {
dir = [dir];
}
for (var i=0;i<dir.length;i++) {
promises.push(loadNodes(dir[i]));
}
} }
} when.settle(promises).then(function(results) {
//console.log(errors); var errors = [];
return errors; results.forEach(function(result) {
//events.emit("nodes-loaded"); if (result.state == 'fulfilled' && result.value) {
errors = errors.concat(result.value);
}
});
resolve(errors);
});
});
} }
var activeConfig = []; var activeConfig = [];

View File

@ -70,26 +70,27 @@ function start() {
console.log("\nWelcome to Node-RED\n===================\n"); console.log("\nWelcome to Node-RED\n===================\n");
util.log("[red] Version: "+RED.version()); util.log("[red] Version: "+RED.version());
util.log("[red] Loading palette nodes"); util.log("[red] Loading palette nodes");
var nodeErrors = redNodes.load(settings); redNodes.load(settings).then(function(nodeErrors) {
if (nodeErrors.length > 0) { if (nodeErrors.length > 0) {
util.log("------------------------------------------"); util.log("------------------------------------------");
if (settings.verbose) { if (settings.verbose) {
for (var i=0;i<nodeErrors.length;i+=1) { for (var i=0;i<nodeErrors.length;i+=1) {
util.log("["+nodeErrors[i].fn+"] "+nodeErrors[i].err); util.log("["+nodeErrors[i].fn+"] "+nodeErrors[i].err);
}
} else {
util.log("[red] Failed to register "+nodeErrors.length+" node type"+(nodeErrors.length==1?"":"s"));
util.log("[red] Run with -v for details");
} }
} else { util.log("------------------------------------------");
util.log("[red] Failed to register "+nodeErrors.length+" node type"+(nodeErrors.length==1?"":"s"));
util.log("[red] Run with -v for details");
} }
util.log("------------------------------------------"); defer.resolve();
} storage.getFlows().then(function(flows) {
defer.resolve(); if (flows.length > 0) {
storage.getFlows().then(function(flows) { redNodes.setConfig(flows);
if (flows.length > 0) { }
redNodes.setConfig(flows); }).otherwise(function(err) {
} util.log("[red] Error loading flows : "+err);
}).otherwise(function(err) { });
util.log("[red] Error loading flows : "+err);
}); });
}); });