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-08-01 23:05:49 +02:00
|
|
|
var crypto = require("crypto");
|
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;
|
|
|
|
|
2014-08-01 23:05:49 +02:00
|
|
|
var registry = (function() {
|
|
|
|
var nodeConfigCache = null;
|
|
|
|
var nodeConfigs = {};
|
|
|
|
var nodeList = [];
|
|
|
|
var nodeConstructors = {};
|
|
|
|
|
|
|
|
return {
|
|
|
|
addNodeSet: function(id,set) {
|
|
|
|
nodeConfigs[id] = set;
|
|
|
|
nodeList.push(id);
|
|
|
|
},
|
|
|
|
getNodeList: function() {
|
|
|
|
return nodeList.map(function(id) {
|
|
|
|
var n = nodeConfigs[id];
|
|
|
|
var r = {
|
|
|
|
id: n.id,
|
|
|
|
types: n.types,
|
|
|
|
name: n.name,
|
|
|
|
enabled: n.enabled
|
|
|
|
}
|
|
|
|
if (n.err) {
|
|
|
|
r.err = n.err.toString();
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
registerNodeConstructor: function(type,constructor) {
|
|
|
|
if (nodeConstructors[type]) {
|
|
|
|
throw new Error(type+" already registered");
|
|
|
|
}
|
|
|
|
util.inherits(constructor,Node);
|
|
|
|
nodeConstructors[type] = constructor;
|
|
|
|
|
|
|
|
events.emit("type-registered",type);
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets all of the node template configs
|
|
|
|
* @return all of the node templates in a single string
|
|
|
|
*/
|
|
|
|
getAllNodeConfigs: function() {
|
|
|
|
if (!nodeConfigCache) {
|
|
|
|
var result = "";
|
|
|
|
var script = "";
|
|
|
|
for (var i=0;i<nodeList.length;i++) {
|
|
|
|
var config = nodeConfigs[nodeList[i]];
|
|
|
|
if (config.enabled) {
|
|
|
|
result += config.config||"";
|
|
|
|
script += config.script||"";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result += '<script type="text/javascript">';
|
|
|
|
result += UglifyJS.minify(script, {fromString: true}).code;
|
|
|
|
result += '</script>';
|
|
|
|
nodeConfigCache = result;
|
|
|
|
}
|
|
|
|
return nodeConfigCache;
|
|
|
|
},
|
|
|
|
|
|
|
|
getNodeConfig: function(id) {
|
|
|
|
var config = nodeConfigs[id];
|
|
|
|
if (config) {
|
|
|
|
var result = config.config||"";
|
|
|
|
result += '<script type="text/javascript">'+(config.script||"")+'</script>';
|
|
|
|
return result;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
getNodeConstructor: function(type) {
|
|
|
|
return nodeConstructors[type];
|
|
|
|
},
|
|
|
|
|
|
|
|
clear: function() {
|
|
|
|
nodeConfigCache = null;
|
|
|
|
nodeConfigs = {};
|
|
|
|
nodeList = [];
|
|
|
|
nodeConstructors = {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
|
2014-07-28 17:37:39 +02:00
|
|
|
|
2014-08-01 23:05:49 +02:00
|
|
|
|
|
|
|
function init(_settings) {
|
|
|
|
Node = require("./Node");
|
|
|
|
settings = _settings;
|
|
|
|
}
|
2014-05-03 23:26:35 +02:00
|
|
|
|
2014-07-25 00:16:41 +02:00
|
|
|
/**
|
|
|
|
* Synchronously walks the directory looking for node files.
|
|
|
|
* Emits 'node-icon-dir' events for an icon dirs found
|
|
|
|
* @param dir the directory to search
|
|
|
|
* @return an array of fully-qualified paths to .js files
|
|
|
|
*/
|
|
|
|
function getNodeFiles(dir) {
|
|
|
|
var result = [];
|
2014-08-01 23:05:49 +02:00
|
|
|
var files = [];
|
|
|
|
try {
|
|
|
|
files = fs.readdirSync(dir);
|
|
|
|
} catch(err) {
|
|
|
|
return result;
|
|
|
|
}
|
2014-07-25 00:16:41 +02:00
|
|
|
files.sort();
|
|
|
|
files.forEach(function(fn) {
|
|
|
|
var stats = fs.statSync(path.join(dir,fn));
|
|
|
|
if (stats.isFile()) {
|
|
|
|
if (/\.js$/.test(fn)) {
|
2014-08-01 23:05:49 +02:00
|
|
|
var valid = true;
|
|
|
|
if (settings.nodesExcludes) {
|
|
|
|
for (var i=0;i<settings.nodesExcludes.length;i++) {
|
|
|
|
if (settings.nodesExcludes[i] == fn) {
|
|
|
|
valid = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (valid) {
|
|
|
|
result.push(path.join(dir,fn));
|
|
|
|
}
|
2014-05-07 21:45:26 +02:00
|
|
|
}
|
2014-07-25 00:16:41 +02:00
|
|
|
} else if (stats.isDirectory()) {
|
|
|
|
// Ignore /.dirs/, /lib/ /node_modules/
|
|
|
|
if (!/^(\..*|lib|icons|node_modules|test)$/.test(fn)) {
|
|
|
|
result = result.concat(getNodeFiles(path.join(dir,fn)));
|
|
|
|
} else if (fn === "icons") {
|
|
|
|
events.emit("node-icon-dir",path.join(dir,fn));
|
|
|
|
}
|
|
|
|
}
|
2014-05-03 23:26:35 +02:00
|
|
|
});
|
2014-07-25 00:16:41 +02:00
|
|
|
return result;
|
2014-05-03 23:26:35 +02:00
|
|
|
}
|
|
|
|
|
2014-07-25 00:16:41 +02:00
|
|
|
/**
|
|
|
|
* Scans the node_modules path for nodes
|
2014-07-30 19:07:20 +02:00
|
|
|
* @param moduleName the name of the module to be found
|
2014-07-25 00:16:41 +02:00
|
|
|
* @return a list of node modules: {dir,package}
|
|
|
|
*/
|
2014-07-28 17:37:39 +02:00
|
|
|
function scanTreeForNodesModules(moduleName) {
|
2014-07-25 00:16:41 +02:00
|
|
|
var dir = __dirname+"/../../nodes";
|
|
|
|
var results = [];
|
|
|
|
var up = path.resolve(path.join(dir,".."));
|
|
|
|
while (up !== dir) {
|
|
|
|
var pm = path.join(dir,"node_modules");
|
2014-05-05 01:10:50 +02:00
|
|
|
try {
|
2014-07-25 00:16:41 +02:00
|
|
|
var files = fs.readdirSync(pm);
|
|
|
|
files.forEach(function(fn) {
|
2014-07-28 17:37:39 +02:00
|
|
|
if (!moduleName || fn == moduleName) {
|
|
|
|
var pkgfn = path.join(pm,fn,"package.json");
|
|
|
|
try {
|
|
|
|
var pkg = require(pkgfn);
|
|
|
|
if (pkg['node-red']) {
|
|
|
|
var moduleDir = path.join(pm,fn);
|
|
|
|
results.push({dir:moduleDir,package:pkg});
|
|
|
|
}
|
|
|
|
} catch(err) {
|
|
|
|
if (err.code != "MODULE_NOT_FOUND") {
|
|
|
|
// TODO: handle unexpected error
|
|
|
|
}
|
2014-07-25 00:16:41 +02:00
|
|
|
}
|
2014-05-03 23:26:35 +02:00
|
|
|
}
|
2014-07-25 00:16:41 +02:00
|
|
|
});
|
2014-05-05 01:10:50 +02:00
|
|
|
} catch(err) {
|
2014-05-03 23:26:35 +02:00
|
|
|
}
|
|
|
|
|
2014-07-25 00:16:41 +02:00
|
|
|
dir = up;
|
|
|
|
up = path.resolve(path.join(dir,".."));
|
|
|
|
}
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
2014-07-28 17:37:39 +02:00
|
|
|
/**
|
|
|
|
* Loads the nodes provided in an npm package.
|
|
|
|
* @param moduleDir the root directory of the package
|
|
|
|
* @param pkg the module's package.json object
|
|
|
|
*/
|
|
|
|
function loadNodesFromModule(moduleDir,pkg) {
|
|
|
|
var nodes = pkg['node-red'].nodes||{};
|
2014-08-01 23:05:49 +02:00
|
|
|
var results = [];
|
2014-07-28 17:37:39 +02:00
|
|
|
var iconDirs = [];
|
|
|
|
for (var n in nodes) {
|
|
|
|
if (nodes.hasOwnProperty(n)) {
|
2014-08-01 23:05:49 +02:00
|
|
|
results.push(loadNodeConfig(path.join(moduleDir,nodes[n]),pkg.name+":"+n));
|
2014-07-28 17:37:39 +02:00
|
|
|
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-08-01 23:05:49 +02:00
|
|
|
return results;
|
2014-07-28 17:37:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-25 00:16:41 +02:00
|
|
|
/**
|
2014-08-01 23:05:49 +02:00
|
|
|
* Loads a node's configuration
|
|
|
|
* @param file the fully qualified path of the node's .js file
|
|
|
|
* @param name the name of the node
|
|
|
|
* @return the node object
|
2014-07-28 17:37:39 +02:00
|
|
|
* {
|
2014-08-01 23:05:49 +02:00
|
|
|
* id: a unqiue id for the node file
|
2014-07-28 17:37:39 +02:00
|
|
|
* name: the name of the node file, or label from the npm module
|
2014-08-01 23:05:49 +02:00
|
|
|
* file: the fully qualified path to the node's .js file
|
2014-07-28 17:37:39 +02:00
|
|
|
* template: the fully qualified path to the node's .html file
|
|
|
|
* config: the non-script parts of the node's .html file
|
|
|
|
* script: the script part of the node's .html file
|
2014-08-01 23:05:49 +02:00
|
|
|
* types: an array of node type names in this file
|
2014-07-28 17:37:39 +02:00
|
|
|
* }
|
2014-07-25 00:16:41 +02:00
|
|
|
*/
|
2014-08-01 23:05:49 +02:00
|
|
|
function loadNodeConfig(file,name) {
|
|
|
|
var id = crypto.createHash('sha1').update(file).digest("hex");
|
2014-07-28 17:37:39 +02:00
|
|
|
|
2014-08-01 23:05:49 +02:00
|
|
|
var node = {
|
|
|
|
id: id,
|
|
|
|
file: file,
|
|
|
|
name: name||path.basename(file),
|
|
|
|
template: file.replace(/\.js$/,".html"),
|
|
|
|
enabled: true
|
2014-07-28 17:37:39 +02:00
|
|
|
}
|
|
|
|
|
2014-08-01 23:05:49 +02:00
|
|
|
var content = fs.readFileSync(node.template,'utf8');
|
2014-07-28 17:37:39 +02:00
|
|
|
|
2014-07-31 17:49:19 +02:00
|
|
|
var $ = cheerio.load(content);
|
2014-07-28 17:37:39 +02:00
|
|
|
var template = "";
|
|
|
|
var script = "";
|
2014-08-01 23:05:49 +02:00
|
|
|
var types = [];
|
|
|
|
|
2014-07-28 17:37:39 +02:00
|
|
|
$("*").each(function(i,el) {
|
|
|
|
if (el.type == "script" && el.attribs.type == "text/javascript") {
|
|
|
|
script += el.children[0].data;
|
|
|
|
} else if (el.name == "script" || el.name == "style") {
|
2014-08-01 23:05:49 +02:00
|
|
|
if (el.attribs.type == "text/x-red" && el.attribs['data-template-name']) {
|
|
|
|
types.push(el.attribs['data-template-name'])
|
|
|
|
}
|
2014-07-28 17:37:39 +02:00
|
|
|
var openTag = "<"+el.name;
|
|
|
|
var closeTag = "</"+el.name+">";
|
|
|
|
if (el.attribs) {
|
|
|
|
for (var j in el.attribs) {
|
|
|
|
if (el.attribs.hasOwnProperty(j)) {
|
|
|
|
openTag += " "+j+'="'+el.attribs[j]+'"';
|
|
|
|
}
|
2014-07-02 00:46:25 +02:00
|
|
|
}
|
|
|
|
}
|
2014-07-28 17:37:39 +02:00
|
|
|
openTag += ">";
|
|
|
|
template += openTag+$(el).text()+closeTag;
|
2014-06-25 11:30:52 +02:00
|
|
|
}
|
2014-07-28 17:37:39 +02:00
|
|
|
});
|
2014-08-01 23:05:49 +02:00
|
|
|
node.types = types;
|
|
|
|
node.config = template;
|
|
|
|
node.script = script;
|
2014-07-28 17:37:39 +02:00
|
|
|
|
2014-08-01 23:05:49 +02:00
|
|
|
registry.addNodeSet(id,node);
|
|
|
|
return node;
|
2014-05-03 23:26:35 +02:00
|
|
|
}
|
|
|
|
|
2014-07-25 00:16:41 +02:00
|
|
|
/**
|
|
|
|
* Loads all palette nodes
|
2014-08-01 23:05:49 +02:00
|
|
|
* @param defaultNodesDir optional parameter, when set, it overrides the default
|
|
|
|
* location of nodeFiles - used by the tests
|
|
|
|
* @return a promise that resolves on completion of loading
|
2014-07-25 00:16:41 +02:00
|
|
|
*/
|
2014-07-30 19:07:20 +02:00
|
|
|
function load(defaultNodesDir) {
|
2014-05-03 23:26:35 +02:00
|
|
|
return when.promise(function(resolve,reject) {
|
2014-07-25 00:16:41 +02:00
|
|
|
// Find all of the nodes to load
|
2014-07-30 19:07:20 +02:00
|
|
|
var nodeFiles;
|
|
|
|
if(defaultNodesDir) {
|
|
|
|
nodeFiles = getNodeFiles(path.resolve(defaultNodesDir));
|
|
|
|
} else {
|
|
|
|
nodeFiles = getNodeFiles(__dirname+"/../../nodes");
|
|
|
|
}
|
|
|
|
|
2014-07-25 00:16:41 +02:00
|
|
|
if (settings.nodesDir) {
|
|
|
|
var dir = settings.nodesDir;
|
|
|
|
if (typeof settings.nodesDir == "string") {
|
|
|
|
dir = [dir];
|
2014-05-03 23:26:35 +02:00
|
|
|
}
|
2014-07-25 00:16:41 +02:00
|
|
|
for (var i=0;i<dir.length;i++) {
|
|
|
|
nodeFiles = nodeFiles.concat(getNodeFiles(dir[i]));
|
|
|
|
}
|
|
|
|
}
|
2014-08-01 23:05:49 +02:00
|
|
|
var nodes = [];
|
2014-07-25 00:16:41 +02:00
|
|
|
nodeFiles.forEach(function(file) {
|
2014-08-01 23:05:49 +02:00
|
|
|
nodes.push(loadNodeConfig(file));
|
2014-07-25 00:16:41 +02:00
|
|
|
});
|
|
|
|
|
2014-08-01 23:05:49 +02:00
|
|
|
// TODO: disabling npm module loading if defaultNodesDir set
|
|
|
|
// This indicates a test is being run - don't want to pick up
|
|
|
|
// unexpected nodes.
|
|
|
|
// Urgh.
|
|
|
|
if (!defaultNodesDir) {
|
|
|
|
// Find all of the modules containing nodes
|
|
|
|
var moduleFiles = scanTreeForNodesModules();
|
|
|
|
moduleFiles.forEach(function(moduleFile) {
|
|
|
|
nodes = nodes.concat(loadNodesFromModule(moduleFile.dir,moduleFile.package));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
var promises = [];
|
|
|
|
nodes.forEach(function(node) {
|
|
|
|
promises.push(loadNode(node));
|
2014-07-25 00:16:41 +02:00
|
|
|
});
|
|
|
|
|
2014-08-01 23:05:49 +02:00
|
|
|
//resolve([]);
|
|
|
|
when.settle(promises).then(function(results) {
|
2014-07-28 17:37:39 +02:00
|
|
|
// Trigger a load of the configs to get it precached
|
2014-08-01 23:05:49 +02:00
|
|
|
registry.getAllNodeConfigs();
|
2014-07-28 17:37:39 +02:00
|
|
|
|
2014-08-01 23:05:49 +02:00
|
|
|
resolve();
|
|
|
|
});
|
2014-05-03 23:26:35 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-07-25 00:16:41 +02:00
|
|
|
/**
|
2014-08-01 23:05:49 +02:00
|
|
|
* Loads the specified node into the runtime
|
|
|
|
* @param node a node info object - see loadNodeConfig
|
|
|
|
* @return a promise that resolves to an update node info object. The object
|
|
|
|
* has the following properties added:
|
|
|
|
* err: any error encountered whilst loading the node
|
|
|
|
*
|
2014-07-25 00:16:41 +02:00
|
|
|
*/
|
2014-08-01 23:05:49 +02:00
|
|
|
function loadNode(node) {
|
|
|
|
var nodeDir = path.dirname(node.file);
|
|
|
|
var nodeFn = path.basename(node.file);
|
|
|
|
try {
|
|
|
|
var loadPromise = null;
|
|
|
|
var r = require(node.file);
|
|
|
|
if (typeof r === "function") {
|
|
|
|
var promise = r(require('../red'));
|
|
|
|
if (promise != null && typeof promise.then === "function") {
|
|
|
|
loadPromise = promise.then(function() {
|
|
|
|
node.enabled = true;
|
|
|
|
return node;
|
|
|
|
}).otherwise(function(err) {
|
|
|
|
node.err = err;
|
|
|
|
node.enabled = false;
|
|
|
|
return node;
|
|
|
|
});
|
|
|
|
}
|
2014-07-28 17:37:39 +02:00
|
|
|
}
|
2014-08-01 23:05:49 +02:00
|
|
|
if (loadPromise == null) {
|
|
|
|
node.enabled = true;
|
|
|
|
loadPromise = when.resolve(node);
|
|
|
|
}
|
|
|
|
return loadPromise;
|
|
|
|
} catch(err) {
|
|
|
|
node.err = err;
|
|
|
|
node.enabled = false;
|
|
|
|
return when.resolve(node);
|
2014-07-25 00:16:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-04 18:30:55 +02:00
|
|
|
|
2014-07-28 17:37:39 +02:00
|
|
|
module.exports = {
|
2014-05-03 23:26:35 +02:00
|
|
|
init:init,
|
|
|
|
load:load,
|
2014-08-01 23:05:49 +02:00
|
|
|
clear: registry.clear,
|
|
|
|
registerType: registry.registerNodeConstructor,
|
|
|
|
get: registry.getNodeConstructor,
|
|
|
|
getNodeList: registry.getNodeList,
|
|
|
|
getNodeConfigs: registry.getAllNodeConfigs,
|
|
|
|
getNodeConfig: registry.getNodeConfig,
|
2014-05-03 23:26:35 +02:00
|
|
|
}
|