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:
parent
da8ef7acc6
commit
7674492819
@ -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) {
|
|
||||||
reject(err);
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
resolve(loadTemplate(templateFilename));
|
resolve({fn:nodeLabel||nodeFn,path:nodeFilename});
|
||||||
|
}
|
||||||
|
} catch(err) {
|
||||||
|
resolve({fn:nodeLabel||nodeFn,path:nodeFilename,err:err});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -82,32 +86,14 @@ 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) {
|
||||||
return when.promise(function(resolve,reject) {
|
|
||||||
|
|
||||||
var pm = path.join(dir,"node_modules");
|
var pm = path.join(dir,"node_modules");
|
||||||
var promises = [];
|
return when.promise(function(resolve,reject) {
|
||||||
promises.push(when.promise(function(resolve,reject) {
|
|
||||||
whenNode.call(fs.readdir,pm).then(function(files) {
|
whenNode.call(fs.readdir,pm).then(function(files) {
|
||||||
var promises = [];
|
var promises = [];
|
||||||
files.forEach(function(fn) {
|
files.forEach(function(fn) {
|
||||||
@ -116,7 +102,7 @@ function scanForNodes(dir) {
|
|||||||
var pkg = require(pkgfn);
|
var pkg = require(pkgfn);
|
||||||
if (pkg['node-red']) {
|
if (pkg['node-red']) {
|
||||||
var moduleDir = path.join(pm,fn);
|
var moduleDir = path.join(pm,fn);
|
||||||
promises.push(loadNodesFromModule(moduleDir,pkg));
|
promises = promises.concat(loadNodesFromModule(moduleDir,pkg));
|
||||||
}
|
}
|
||||||
} catch(err) {
|
} catch(err) {
|
||||||
if (err.code != "MODULE_NOT_FOUND") {
|
if (err.code != "MODULE_NOT_FOUND") {
|
||||||
@ -125,36 +111,40 @@ function scanForNodes(dir) {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
when.settle(promises).then(function(results) {
|
when.settle(promises).then(function(results) {
|
||||||
var errors = [];
|
var promises = [];
|
||||||
results.forEach(function(result) {
|
results.forEach(function(result) {
|
||||||
if (result.state == 'fulfilled' && result.value) {
|
promises = promises.concat(result.value);
|
||||||
errors = errors.concat(result.value);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
resolve(errors);
|
resolve(promises);
|
||||||
});
|
});
|
||||||
},function(err) {
|
},function(err) {
|
||||||
resolve([]);
|
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);
|
|
||||||
});
|
|
||||||
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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,13 +208,30 @@ 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);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
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);
|
resolve(errors);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user