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

Fix node loading order

This commit is contained in:
Nick O'Leary 2014-05-05 00:10:50 +01:00
parent da8ef7acc6
commit 7674492819

View File

@ -36,6 +36,7 @@ function loadTemplate(templateFilename) {
whenNode.call(fs.readFile,templateFilename,'utf8').done(function(content) { whenNode.call(fs.readFile,templateFilename,'utf8').done(function(content) {
registerConfig(content); registerConfig(content);
resolve(); resolve();
//console.log(templateFilename);
}, function(err) { }, function(err) {
reject("missing template file"); reject("missing template file");
}); });
@ -43,36 +44,39 @@ function loadTemplate(templateFilename) {
}); });
} }
function loadNode(nodeDir, nodeFn) { // Return a promise that resolves to:
// success: {fn,path}
// failure: {fn.path,err}
function loadNode(nodeDir, nodeFn, nodeLabel) {
return when.promise(function(resolve,reject) { return when.promise(function(resolve,reject) {
if (settings.nodesExcludes) { if (settings.nodesExcludes) {
for (var i=0;i<settings.nodesExcludes.length;i++) { for (var i=0;i<settings.nodesExcludes.length;i++) {
if (settings.nodesExcludes[i] == nodeFn) { if (settings.nodesExcludes[i] == nodeFn) {
//resolve({fn:nodeFn,path:nodeFilename,err:"nodesExcludes"});
resolve(); resolve();
return; return;
} }
} }
} }
var nodeFilename = path.join(nodeDir,nodeFn); var nodeFilename = path.join(nodeDir,nodeFn);
var templateFilename = nodeFilename.replace(/\.js$/,".html"); try {
var r = require(nodeFilename); var r = require(nodeFilename);
if (typeof r === "function") { if (typeof r === "function") {
try {
var promise = r(require('../red')); var promise = r(require('../red'));
if (promise != null && typeof promise.then === "function") { if (promise != null && typeof promise.then === "function") {
promise.then(function() { promise.then(function() {
resolve(loadTemplate(templateFilename)); resolve({fn:nodeLabel||nodeFn,path:nodeFilename});
},function(err) { },function(err) {
reject(err); resolve({fn:nodeLabel||nodeFn,path:nodeFilename,err:err});
}); });
} else { } else {
resolve(loadTemplate(templateFilename)); resolve({fn:nodeLabel||nodeFn,path:nodeFilename});
} }
} catch(err) { } else {
reject(err); resolve({fn:nodeLabel||nodeFn,path:nodeFilename});
} }
} else { } catch(err) {
resolve(loadTemplate(templateFilename)); resolve({fn:nodeLabel||nodeFn,path:nodeFilename,err:err});
} }
}); });
@ -82,79 +86,65 @@ function loadNodesFromModule(moduleDir,pkg) {
var nodes = pkg['node-red'].nodes||{}; var nodes = pkg['node-red'].nodes||{};
var promises = []; var promises = [];
for (var n in nodes) { for (var n in nodes) {
promises.push(when.promise(function(resolve) { promises.push(loadNode(moduleDir,nodes[n],pkg.name+":"+n));
loadNode(moduleDir,nodes[n]).then(resolve, function(err) {
resolve({'fn':pkg.name+":"+n,err:err});
});
}));
} }
return when.promise(function(resolve,reject) { return promises;
var errors = [];
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 scanForNodes(dir) { function scanForNodes(dir) {
var pm = path.join(dir,"node_modules");
return when.promise(function(resolve,reject) { return when.promise(function(resolve,reject) {
whenNode.call(fs.readdir,pm).then(function(files) {
var pm = path.join(dir,"node_modules"); var promises = [];
var promises = []; files.forEach(function(fn) {
promises.push(when.promise(function(resolve,reject) { var pkgfn = path.join(pm,fn,"package.json");
whenNode.call(fs.readdir,pm).then(function(files) { try {
var promises = []; var pkg = require(pkgfn);
files.forEach(function(fn) { if (pkg['node-red']) {
var pkgfn = path.join(pm,fn,"package.json"); var moduleDir = path.join(pm,fn);
try { promises = promises.concat(loadNodesFromModule(moduleDir,pkg));
var pkg = require(pkgfn); }
if (pkg['node-red']) { } catch(err) {
var moduleDir = path.join(pm,fn); if (err.code != "MODULE_NOT_FOUND") {
promises.push(loadNodesFromModule(moduleDir,pkg)); // TODO: handle unexpected error
}
} 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); when.settle(promises).then(function(results) {
var promises = [];
results.forEach(function(result) {
promises = promises.concat(result.value);
});
resolve(promises);
});
},function(err) {
resolve([]);
}); });
}); });
} }
function scanTreeForNodes(dir) {
return when.promise(function(resolve) {
var promises = [];
var up = path.resolve(path.join(dir,".."));
while (up !== dir) {
promises.push(scanForNodes(dir));
dir = up;
up = path.resolve(path.join(dir,".."));
}
when.settle(promises).then(function(results) {
var promises = [];
results.forEach(function(result) {
promises = promises.concat(result.value);
});
resolve(promises);
});
});
}
// Returns a promise that resolves to an array of node results
function loadNodes(dir) { function loadNodes(dir) {
return when.promise(function(resolve,reject) { return when.promise(function(resolve,reject) {
var promises = []; var promises = [];
@ -165,21 +155,12 @@ function loadNodes(dir) {
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) { promises.push(loadNode(dir,fn));
loadNode(dir,fn).then(resolve, function(err) {
resolve({'fn':fn,err:err});
});
}));
} }
} else if (stats.isDirectory()) { } else if (stats.isDirectory()) {
// Ignore /.dirs/, /lib/ /node_modules/ // Ignore /.dirs/, /lib/ /node_modules/
if (!/^(\..*|lib|icons|node_modules|test)$/.test(fn)) { if (!/^(\..*|lib|icons|node_modules|test)$/.test(fn)) {
promises.push(when.promise(function(resolve,reject) { promises.push(loadNodes(path.join(dir,fn)));
loadNodes(path.join(dir,fn)).then(function(errs) {
resolve(errs);
});
}));
} else if (fn === "icons") { } else if (fn === "icons") {
events.emit("node-icon-dir",path.join(dir,fn)); events.emit("node-icon-dir",path.join(dir,fn));
} }
@ -212,9 +193,9 @@ function load() {
return when.promise(function(resolve,reject) { return when.promise(function(resolve,reject) {
var RED = require("../red.js"); var RED = require("../red.js");
loadNodes(__dirname+"/../../nodes").then(function(errors) { loadNodes(__dirname+"/../../nodes").then(function(loadedNodes) {
var promises = []; var promises = [];
promises.push(scanForNodes(__dirname+"/../../nodes")); promises.push(scanTreeForNodes(__dirname+"/../../nodes"));
if (settings.nodesDir) { if (settings.nodesDir) {
var dir = settings.nodesDir; var dir = settings.nodesDir;
if (typeof settings.nodesDir == "string") { if (typeof settings.nodesDir == "string") {
@ -227,10 +208,27 @@ function load() {
when.settle(promises).then(function(results) { when.settle(promises).then(function(results) {
results.forEach(function(result) { results.forEach(function(result) {
if (result.state == 'fulfilled' && result.value) { if (result.state == 'fulfilled' && result.value) {
errors = errors.concat(result.value); loadedNodes = loadedNodes.concat(result.value);
} }
}); });
resolve(errors); var promises = [];
loadedNodes.forEach(function(v) {
if (v.err == null) {
var templateFilename = v.path.replace(/\.js$/,".html");
promises.push(loadTemplate(templateFilename));
} else {
promises.push(when());
}
});
when.settle(promises).then(function(results) {
for (var i=0;i<loadedNodes.length;i++) {
if (results[i].state == "rejected") {
loadedNodes[i].err = results[i].reason;
}
}
var errors = loadedNodes.filter(function(v) { return v.err != null;});
resolve(errors);
});
}); });
}); });
}); });