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

Allow nodesDir to be an array of paths

This commit is contained in:
Nick O'Leary 2014-04-07 22:00:59 +01:00
parent 61285a0ee8
commit 0b8e8de260

View File

@ -22,30 +22,6 @@ var events = require("./events");
var storage = null; var storage = null;
var settings = null; var settings = null;
function getCallerFilename(type) {
//if (type == "summary") {
// var err = new Error();
// console.log(err.stack);
// return null;
//}
// Save original Error.prepareStackTrace
var origPrepareStackTrace = Error.prepareStackTrace;
// Override with function that just returns `stack`
Error.prepareStackTrace = function (_, stack) {
return stack;
}
// Create a new `Error`, which automatically gets `stack`
var err = new Error();
// Evaluate `err.stack`, which calls our new `Error.prepareStackTrace`
var stack = err.stack;
// Restore original `Error.prepareStackTrace`
Error.prepareStackTrace = origPrepareStackTrace;
// Remove superfluous function call on stack
stack.shift();
stack.shift();
return stack[0].getFileName();
}
var registry = (function() { var registry = (function() {
var nodes = {}; var nodes = {};
var logHandlers = []; var logHandlers = [];
@ -90,35 +66,28 @@ registry.addLogHandler(ConsoleLogHandler);
var node_type_registry = (function() { var node_type_registry = (function() {
var node_types = {}; var node_types = {};
var node_configs = {}; var node_configs = [];
var obj = { var obj = {
register: function(type,node) { register: function(type,node) {
util.inherits(node, Node); util.inherits(node, Node);
var callerFilename = getCallerFilename(type);
if (callerFilename == null) {
util.log("["+type+"] unable to determine filename");
} else {
var configFilename = callerFilename.replace(/\.js$/,".html");
if (fs.existsSync(configFilename)) {
node_types[type] = node; node_types[type] = node;
if (! node_configs[configFilename]) {
node_configs[configFilename] = fs.readFileSync(configFilename,'utf8');
}
events.emit("type-registered",type); events.emit("type-registered",type);
} else { },
util.log("["+type+"] missing template file: "+configFilename); registerConfig: function(config) {
} node_configs.push(config);
}
}, },
get: function(type) { get: function(type) {
return node_types[type]; return node_types[type];
}, },
getNodeConfigs: function() { getNodeConfigs: function() {
var result = ""; var result = "";
for (var nt in node_configs) { for (var i=0;i<node_configs.length;i++) {
result += node_configs[nt]; result += node_configs[i];
} }
return result; return result;
},
count: function() {
return Object.keys(node_types).length;
} }
} }
return obj; return obj;
@ -237,6 +206,24 @@ function createNode(node,def) {
function load(_settings) { function load(_settings) {
settings = _settings; settings = _settings;
var RED = require("./red.js");
function loadNode(nodeFilename) {
var preCount = node_type_registry.count();
var r = require(nodeFilename);
if (typeof r === "function") {
r(RED);
}
if (preCount != node_type_registry.count()) {
var templateFilename = nodeFilename.replace(/\.js$/,".html");
if (fs.existsSync(templateFilename)) {
node_type_registry.registerConfig(fs.readFileSync(templateFilename,'utf8'));
} else {
util.log("["+type+"] missing template file: "+templateFilename);
}
}
}
function scanForNodes(dir) { function scanForNodes(dir) {
var pm = path.join(dir,"node_modules"); var pm = path.join(dir,"node_modules");
if (fs.existsSync(pm)) { if (fs.existsSync(pm)) {
@ -252,7 +239,8 @@ function load(_settings) {
for (var i in nrn) { for (var i in nrn) {
console.log(" ",i,":",nrn[i]); console.log(" ",i,":",nrn[i]);
try { try {
require(path.join(pm,fn,nrn[i])); var nodeFilename = path.join(pm,fn,nrn[i]);
loadNode(nodeFilename);
} catch(err) { } catch(err) {
util.log("["+i+"] "+err); util.log("["+i+"] "+err);
//console.log(err.stack); //console.log(err.stack);
@ -278,7 +266,8 @@ function load(_settings) {
if (stats.isFile()) { if (stats.isFile()) {
if (/\.js$/.test(fn)) { if (/\.js$/.test(fn)) {
try { try {
require(path.join(dir,fn)); var nodeFilename = path.join(dir,fn);
loadNode(nodeFilename);
} catch(err) { } catch(err) {
errors.push({fn:fn, err:err}); errors.push({fn:fn, err:err});
//util.log("["+fn+"] "+err); //util.log("["+fn+"] "+err);
@ -287,7 +276,7 @@ function load(_settings) {
} }
} else if (stats.isDirectory()) { } else if (stats.isDirectory()) {
// Ignore /.dirs/, /lib/ /node_modules/ // Ignore /.dirs/, /lib/ /node_modules/
if (!/^(\..*|lib|icons|node_modules)$/.test(fn)) { if (!/^(\..*|lib|icons|node_modules|test)$/.test(fn)) {
errors = errors.concat(loadNodes(path.join(dir,fn))); errors = errors.concat(loadNodes(path.join(dir,fn)));
} 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));
@ -300,8 +289,15 @@ function load(_settings) {
var errors = loadNodes(__dirname+"/../nodes"); var errors = loadNodes(__dirname+"/../nodes");
scanForNodes(__dirname+"/../nodes"); scanForNodes(__dirname+"/../nodes");
if (settings.nodesDir) { if (settings.nodesDir) {
errors = errors.concat(loadNodes(settings.nodesDir)); var dir = settings.nodesDir;
scanForNodes(settings.nodesDir); if (typeof settings.nodesDir == "string") {
dir = [dir];
}
for (var i=0;i<dir.length;i++) {
errors = errors.concat(loadNodes(dir[i]));
scanForNodes(dir[i]);
}
} }
//console.log(errors); //console.log(errors);
return errors; return errors;
@ -419,6 +415,7 @@ module.exports = {
deleteCredentials: deleteCredentials, deleteCredentials: deleteCredentials,
createNode: createNode, createNode: createNode,
registerType: node_type_registry.register, registerType: node_type_registry.register,
getType: node_type_registry.get,
getNodeConfigs: node_type_registry.getNodeConfigs, getNodeConfigs: node_type_registry.getNodeConfigs,
addLogHandler: registry.addLogHandler, addLogHandler: registry.addLogHandler,
load: load, load: load,