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 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 nodes = {};
var logHandlers = [];
@ -90,35 +66,28 @@ registry.addLogHandler(ConsoleLogHandler);
var node_type_registry = (function() {
var node_types = {};
var node_configs = {};
var node_configs = [];
var obj = {
register: function(type,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;
if (! node_configs[configFilename]) {
node_configs[configFilename] = fs.readFileSync(configFilename,'utf8');
}
events.emit("type-registered",type);
} else {
util.log("["+type+"] missing template file: "+configFilename);
}
}
node_types[type] = node;
events.emit("type-registered",type);
},
registerConfig: function(config) {
node_configs.push(config);
},
get: function(type) {
return node_types[type];
},
getNodeConfigs: function() {
var result = "";
for (var nt in node_configs) {
result += node_configs[nt];
for (var i=0;i<node_configs.length;i++) {
result += node_configs[i];
}
return result;
},
count: function() {
return Object.keys(node_types).length;
}
}
return obj;
@ -237,6 +206,24 @@ function createNode(node,def) {
function load(_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) {
var pm = path.join(dir,"node_modules");
if (fs.existsSync(pm)) {
@ -252,7 +239,8 @@ function load(_settings) {
for (var i in nrn) {
console.log(" ",i,":",nrn[i]);
try {
require(path.join(pm,fn,nrn[i]));
var nodeFilename = path.join(pm,fn,nrn[i]);
loadNode(nodeFilename);
} catch(err) {
util.log("["+i+"] "+err);
//console.log(err.stack);
@ -278,7 +266,8 @@ function load(_settings) {
if (stats.isFile()) {
if (/\.js$/.test(fn)) {
try {
require(path.join(dir,fn));
var nodeFilename = path.join(dir,fn);
loadNode(nodeFilename);
} catch(err) {
errors.push({fn:fn, err:err});
//util.log("["+fn+"] "+err);
@ -287,7 +276,7 @@ function load(_settings) {
}
} else if (stats.isDirectory()) {
// 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)));
} else if (fn === "icons") {
events.emit("node-icon-dir",path.join(dir,fn));
@ -300,8 +289,15 @@ function load(_settings) {
var errors = loadNodes(__dirname+"/../nodes");
scanForNodes(__dirname+"/../nodes");
if (settings.nodesDir) {
errors = errors.concat(loadNodes(settings.nodesDir));
scanForNodes(settings.nodesDir);
var dir = 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);
return errors;
@ -419,6 +415,7 @@ module.exports = {
deleteCredentials: deleteCredentials,
createNode: createNode,
registerType: node_type_registry.register,
getType: node_type_registry.get,
getNodeConfigs: node_type_registry.getNodeConfigs,
addLogHandler: registry.addLogHandler,
load: load,