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

Change node config load to be async

This commit is contained in:
Nick O'Leary 2015-04-07 23:46:52 +01:00
parent 9dd7e2e43d
commit 2fe9c1e55f

View File

@ -36,7 +36,7 @@ function load(defaultNodesDir,disableNodePathScan) {
} }
function loadNodeFiles(nodeFiles) { function loadNodeFiles(nodeFiles) {
var nodes = []; var promises = [];
for (var module in nodeFiles) { for (var module in nodeFiles) {
/* istanbul ignore else */ /* istanbul ignore else */
if (nodeFiles.hasOwnProperty(module)) { if (nodeFiles.hasOwnProperty(module)) {
@ -45,7 +45,7 @@ function loadNodeFiles(nodeFiles) {
/* istanbul ignore else */ /* istanbul ignore else */
if (nodeFiles[module].nodes.hasOwnProperty(node)) { if (nodeFiles[module].nodes.hasOwnProperty(node)) {
try { try {
nodes.push(loadNodeConfig(nodeFiles[module].nodes[node])) promises.push(loadNodeConfig(nodeFiles[module].nodes[node]))
} catch(err) { } catch(err) {
// //
} }
@ -54,68 +54,72 @@ function loadNodeFiles(nodeFiles) {
} }
} }
} }
return loadNodeSetList(nodes); return when.settle(promises).then(function(results) {
var nodes = results.map(function(r) { return r.value; });
return loadNodeSetList(nodes);
});
} }
function loadNodeConfig(fileInfo) { function loadNodeConfig(fileInfo) {
var file = fileInfo.file; return when.promise(function(resolve) {
var module = fileInfo.module; var file = fileInfo.file;
var name = fileInfo.name; var module = fileInfo.module;
var version = fileInfo.version; var name = fileInfo.name;
var version = fileInfo.version;
var id = module + "/" + name;
var info = registry.getNodeInfo(id); var id = module + "/" + name;
var isEnabled = true; var info = registry.getNodeInfo(id);
if (info) { var isEnabled = true;
if (info.hasOwnProperty("loaded")) { if (info) {
throw new Error(file+" already loaded"); if (info.hasOwnProperty("loaded")) {
} throw new Error(file+" already loaded");
isEnabled = info.enabled;
}
var node = {
id: id,
module: module,
name: name,
file: file,
template: file.replace(/\.js$/,".html"),
enabled: isEnabled,
loaded:false
};
try {
var content = fs.readFileSync(node.template,'utf8');
var types = [];
var regExp = /<script ([^>]*)data-template-name=['"]([^'"]*)['"]/gi;
var match = null;
while((match = regExp.exec(content)) !== null) {
types.push(match[2]);
}
node.types = types;
node.config = content;
// TODO: parse out the javascript portion of the template
//node.script = "";
for (var i=0;i<node.types.length;i++) {
if (registry.getTypeId(node.types[i])) {
node.err = node.types[i]+" already registered";
break;
} }
isEnabled = info.enabled;
} }
} catch(err) {
node.types = []; var node = {
if (err.code === 'ENOENT') { id: id,
node.err = "Error: "+file+" does not exist"; module: module,
} else { name: name,
node.err = err.toString(); file: file,
} template: file.replace(/\.js$/,".html"),
} enabled: isEnabled,
loaded:false
registry.addNodeSet(id,node,version); };
return node;
fs.readFile(node.template,'utf8', function(err,content) {
if (err) {
node.types = [];
if (err.code === 'ENOENT') {
node.err = "Error: "+file+" does not exist";
} else {
node.err = err.toString();
}
} else {
var types = [];
var regExp = /<script ([^>]*)data-template-name=['"]([^'"]*)['"]/gi;
var match = null;
while((match = regExp.exec(content)) !== null) {
types.push(match[2]);
}
node.types = types;
node.config = content;
// TODO: parse out the javascript portion of the template
//node.script = "";
for (var i=0;i<node.types.length;i++) {
if (registry.getTypeId(node.types[i])) {
node.err = node.types[i]+" already registered";
break;
}
}
}
registry.addNodeSet(id,node,version);
resolve(node);
});
});
} }