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

302 lines
9.9 KiB
JavaScript
Raw Normal View History

2014-05-03 23:26:35 +02:00
/**
* Copyright 2014 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
var util = require("util");
var when = require("when");
var whenNode = require('when/node');
var fs = require("fs");
var path = require("path");
2014-05-04 18:30:55 +02:00
var cheerio = require("cheerio");
var UglifyJS = require("uglify-js");
2014-05-03 23:26:35 +02:00
var events = require("../events");
var Node;
var settings;
var node_types = {};
var node_configs = [];
2014-05-04 18:30:55 +02:00
var node_scripts = [];
2014-05-03 23:26:35 +02:00
function loadTemplate(templateFilename) {
return when.promise(function(resolve,reject) {
whenNode.call(fs.readFile,templateFilename,'utf8').done(function(content) {
2014-05-07 21:45:26 +02:00
try {
registerConfig(content);
} catch(err) {
reject("invalid template file: "+err.message);
}
2014-05-03 23:26:35 +02:00
resolve();
}, function(err) {
reject("missing template file");
});
});
}
2014-05-05 01:10:50 +02:00
// Return a promise that resolves to:
// success: {fn,path}
// failure: {fn.path,err}
function loadNode(nodeDir, nodeFn, nodeLabel) {
2014-05-03 23:26:35 +02:00
return when.promise(function(resolve,reject) {
if (settings.nodesExcludes) {
for (var i=0;i<settings.nodesExcludes.length;i++) {
if (settings.nodesExcludes[i] == nodeFn) {
2014-05-05 01:10:50 +02:00
//resolve({fn:nodeFn,path:nodeFilename,err:"nodesExcludes"});
2014-05-03 23:26:35 +02:00
resolve();
return;
}
}
}
var nodeFilename = path.join(nodeDir,nodeFn);
2014-05-05 01:10:50 +02:00
try {
var r = require(nodeFilename);
if (typeof r === "function") {
2014-05-04 00:32:04 +02:00
var promise = r(require('../red'));
2014-05-03 23:26:35 +02:00
if (promise != null && typeof promise.then === "function") {
promise.then(function() {
2014-05-05 01:10:50 +02:00
resolve({fn:nodeLabel||nodeFn,path:nodeFilename});
2014-05-03 23:26:35 +02:00
},function(err) {
2014-05-05 01:10:50 +02:00
resolve({fn:nodeLabel||nodeFn,path:nodeFilename,err:err});
2014-05-03 23:26:35 +02:00
});
} else {
2014-05-05 01:10:50 +02:00
resolve({fn:nodeLabel||nodeFn,path:nodeFilename});
2014-05-03 23:26:35 +02:00
}
2014-05-05 01:10:50 +02:00
} else {
resolve({fn:nodeLabel||nodeFn,path:nodeFilename});
2014-05-03 23:26:35 +02:00
}
2014-05-05 01:10:50 +02:00
} catch(err) {
resolve({fn:nodeLabel||nodeFn,path:nodeFilename,err:err});
2014-05-03 23:26:35 +02:00
}
});
}
function loadNodesFromModule(moduleDir,pkg) {
var nodes = pkg['node-red'].nodes||{};
var promises = [];
var iconDirs = [];
2014-05-03 23:26:35 +02:00
for (var n in nodes) {
2014-07-02 00:46:25 +02:00
if (nodes.hasOwnProperty(n)) {
promises.push(loadNode(moduleDir,nodes[n],pkg.name+":"+n));
var iconDir = path.join(moduleDir,path.dirname(nodes[n]),"icons");
if (iconDirs.indexOf(iconDir) == -1) {
if (fs.existsSync(iconDir)) {
events.emit("node-icon-dir",iconDir);
iconDirs.push(iconDir);
}
}
}
2014-05-03 23:26:35 +02:00
}
2014-05-05 01:10:50 +02:00
return promises;
}
function scanForNodes(dir) {
var pm = path.join(dir,"node_modules");
2014-05-03 23:26:35 +02:00
return when.promise(function(resolve,reject) {
2014-05-05 01:10:50 +02:00
whenNode.call(fs.readdir,pm).then(function(files) {
var promises = [];
files.forEach(function(fn) {
var pkgfn = path.join(pm,fn,"package.json");
try {
var pkg = require(pkgfn);
if (pkg['node-red']) {
var moduleDir = path.join(pm,fn);
promises = promises.concat(loadNodesFromModule(moduleDir,pkg));
}
} catch(err) {
if (err.code != "MODULE_NOT_FOUND") {
// TODO: handle unexpected error
}
2014-05-03 23:26:35 +02:00
}
});
2014-05-05 01:10:50 +02:00
when.settle(promises).then(function(results) {
var promises = [];
results.forEach(function(result) {
promises = promises.concat(result.value);
});
resolve(promises);
});
},function(err) {
resolve([]);
2014-05-03 23:26:35 +02:00
});
});
}
2014-05-05 01:10:50 +02:00
function scanTreeForNodes(dir) {
return when.promise(function(resolve) {
2014-05-03 23:26:35 +02:00
var promises = [];
var up = path.resolve(path.join(dir,".."));
2014-05-05 01:10:50 +02:00
while (up !== dir) {
promises.push(scanForNodes(dir));
dir = up;
up = path.resolve(path.join(dir,".."));
2014-05-03 23:26:35 +02:00
}
2014-05-05 01:10:50 +02:00
2014-05-03 23:26:35 +02:00
when.settle(promises).then(function(results) {
2014-05-05 01:10:50 +02:00
var promises = [];
2014-05-03 23:26:35 +02:00
results.forEach(function(result) {
2014-05-05 01:10:50 +02:00
promises = promises.concat(result.value);
2014-05-03 23:26:35 +02:00
});
2014-05-05 01:10:50 +02:00
resolve(promises);
2014-05-03 23:26:35 +02:00
});
});
}
2014-05-05 01:10:50 +02:00
2014-05-03 23:26:35 +02:00
2014-05-05 01:10:50 +02:00
// Returns a promise that resolves to an array of node results
2014-05-03 23:26:35 +02:00
function loadNodes(dir) {
return when.promise(function(resolve,reject) {
var promises = [];
whenNode.call(fs.readdir,dir).done(function(files) {
files = files.sort();
files.forEach(function(fn) {
var stats = fs.statSync(path.join(dir,fn));
if (stats.isFile()) {
if (/\.js$/.test(fn)) {
2014-05-05 01:10:50 +02:00
promises.push(loadNode(dir,fn));
2014-05-03 23:26:35 +02:00
}
} else if (stats.isDirectory()) {
// Ignore /.dirs/, /lib/ /node_modules/
if (!/^(\..*|lib|icons|node_modules|test)$/.test(fn)) {
2014-05-05 01:10:50 +02:00
promises.push(loadNodes(path.join(dir,fn)));
2014-05-03 23:26:35 +02:00
} else if (fn === "icons") {
events.emit("node-icon-dir",path.join(dir,fn));
}
}
});
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([]);
// non-existant dir
});
});
}
function init(_settings) {
Node = require("./Node");
2014-05-03 23:26:35 +02:00
settings = _settings;
}
function load() {
return when.promise(function(resolve,reject) {
2014-05-05 01:10:50 +02:00
loadNodes(__dirname+"/../../nodes").then(function(loadedNodes) {
2014-05-03 23:26:35 +02:00
var promises = [];
2014-05-05 01:10:50 +02:00
promises.push(scanTreeForNodes(__dirname+"/../../nodes"));
2014-05-03 23:26:35 +02:00
if (settings.nodesDir) {
var dir = settings.nodesDir;
if (typeof settings.nodesDir == "string") {
dir = [dir];
}
for (var i=0;i<dir.length;i++) {
promises.push(loadNodes(dir[i]));
}
}
when.settle(promises).then(function(results) {
results.forEach(function(result) {
if (result.state == 'fulfilled' && result.value) {
2014-05-05 01:10:50 +02:00
loadedNodes = loadedNodes.concat(result.value);
2014-05-03 23:26:35 +02:00
}
});
2014-05-05 01:10:50 +02:00
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);
});
2014-05-03 23:26:35 +02:00
});
});
});
}
2014-05-04 18:30:55 +02:00
function registerConfig(config) {
$ = cheerio.load(config);
var template = "";
$("*").each(function(i,el) {
if (el.type == "script" && el.attribs.type == "text/javascript") {
var content = el.children[0].data;
el.children[0].data = UglifyJS.minify(content, {fromString: true}).code;
node_scripts.push($(this).text());
} else if (el.name == "script" || el.name == "style") {
var openTag = "<"+el.name;
var closeTag = "</"+el.name+">";
if (el.attribs) {
2014-07-02 00:46:25 +02:00
for (var j in el.attribs) {
if (el.attribs.hasOwnProperty(j)) {
2014-07-02 00:48:26 +02:00
openTag += " "+j+'="'+el.attribs[j]+'"';
2014-07-02 00:46:25 +02:00
}
2014-05-04 18:30:55 +02:00
}
}
openTag += ">";
template += openTag+$(el).text()+closeTag;
}
});
node_configs.push(template);
}
2014-05-03 23:26:35 +02:00
var typeRegistry = module.exports = {
init:init,
load:load,
registerType: function(type,node) {
util.inherits(node,Node);
node_types[type] = node;
events.emit("type-registered",type);
},
get: function(type) {
return node_types[type];
},
getNodeConfigs: function() {
var result = "";
for (var i=0;i<node_configs.length;i++) {
result += node_configs[i];
}
2014-05-04 18:30:55 +02:00
result += '<script type="text/javascript">';
2014-07-02 00:46:25 +02:00
for (var j=0;j<node_scripts.length;j++) {
result += node_scripts[j];
2014-05-04 18:30:55 +02:00
}
result += '</script>';
2014-05-03 23:26:35 +02:00
return result;
}
}