From 3e24601518a9c285af2ccf183bde88ffc899bc32 Mon Sep 17 00:00:00 2001 From: Anna Thomas Date: Thu, 6 Nov 2014 10:00:25 +0000 Subject: [PATCH] Functionality to get installed module info from the cli --- red/api/index.js | 23 +++++--- red/api/modules.js | 32 +++++++++++ red/nodes/index.js | 22 ++++---- red/nodes/registry.js | 126 ++++++++++++++++++++++++++++-------------- 4 files changed, 141 insertions(+), 62 deletions(-) create mode 100644 red/api/modules.js diff --git a/red/api/index.js b/red/api/index.js index f0958b3b0..c2d3bcbdb 100644 --- a/red/api/index.js +++ b/red/api/index.js @@ -19,6 +19,7 @@ var util = require('util'); var ui = require("./ui"); var nodes = require("./nodes"); +var modules = require("./modules"); var flows = require("./flows"); var library = require("./library"); @@ -27,12 +28,12 @@ var settings = require("../settings"); var errorHandler = function(err,req,res,next) { //TODO: standardize json response res.send(400,err.toString()); -} +}; function init(adminApp) { - + adminApp.use(express.json()); - + library.init(adminApp); // Editor @@ -42,11 +43,11 @@ function init(adminApp) { adminApp.get("/settings",ui.settings); adminApp.use("/",ui.editor); } - + // Flows adminApp.get("/flows",flows.get); adminApp.post("/flows",flows.post); - + // Nodes adminApp.get("/nodes",nodes.getAll); adminApp.post("/nodes",nodes.post); @@ -54,17 +55,21 @@ function init(adminApp) { adminApp.get("/nodes/:id",nodes.get); adminApp.put("/nodes/:id",nodes.put); adminApp.delete("/nodes/:id",nodes.delete); - + + // Modules + adminApp.get("/modules",modules.getAll); + adminApp.get("/modules/:id",modules.get); + // Library adminApp.post(new RegExp("/library/flows\/(.*)"),library.post); adminApp.get("/library/flows",library.getAll); adminApp.get(new RegExp("/library/flows\/(.*)"),library.get); - - + + // Error Handler adminApp.use(errorHandler); } module.exports = { init: init -} +}; diff --git a/red/api/modules.js b/red/api/modules.js new file mode 100644 index 000000000..426157941 --- /dev/null +++ b/red/api/modules.js @@ -0,0 +1,32 @@ +/** + * 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 redNodes = require("../nodes"); + +module.exports = { + getAll: function(req,res) { + res.json(redNodes.getModuleList()); + }, + get: function(req,res) { + var id = req.params.id; + var result = redNodes.getModule(id); + if (result) { + res.send(result); + } else { + res.send(404); + } + } +}; diff --git a/red/nodes/index.js b/red/nodes/index.js index 36c63ce3f..4567280d4 100644 --- a/red/nodes/index.js +++ b/red/nodes/index.js @@ -28,14 +28,14 @@ function registerType(type,constructor,opts) { if (opts && opts.credentials) { credentials.register(type,opts.credentials); } - registry.registerType(type,constructor); + registry.registerType(type,constructor); } /** * Called from a Node's constructor function, invokes the super-class * constructor and attaches any credentials to the node. * @param node the node object being created - * @param def the instance definition for the node + * @param def the instance definition for the node */ function createNode(node,def) { Node.call(node,def); @@ -95,40 +95,42 @@ module.exports = { // Lifecycle init: init, load: registry.load, - + // Node registry createNode: createNode, getNode: flows.get, - + addNode: registry.addNode, removeNode: removeNode, - + addModule: registry.addModule, removeModule: removeModule, - + enableNode: registry.enableNode, disableNode: disableNode, - + // Node type registry registerType: registerType, getType: registry.get, getNodeInfo: registry.getNodeInfo, getNodeModuleInfo: registry.getNodeModuleInfo, + getModule: registry.getModule, getNodeList: registry.getNodeList, + getModuleList: registry.getModuleList, getNodeConfigs: registry.getNodeConfigs, getNodeConfig: registry.getNodeConfig, clearRegistry: registry.clear, cleanNodeList: registry.cleanNodeList, - + // Flow handling loadFlows: flows.load, stopFlows: flows.stopFlows, setFlows: flows.setFlows, getFlows: flows.getFlows, - + // Credentials addCredentials: credentials.add, getCredentials: credentials.get, deleteCredentials: credentials.delete -} +}; diff --git a/red/nodes/registry.js b/red/nodes/registry.js index f2073aff8..82c069ea5 100644 --- a/red/nodes/registry.js +++ b/red/nodes/registry.js @@ -13,13 +13,13 @@ * 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"); -var crypto = require("crypto"); +var crypto = require("crypto"); var UglifyJS = require("uglify-js"); var events = require("../events"); @@ -53,10 +53,10 @@ var registry = (function() { var nodeConstructors = {}; var nodeTypeToId = {}; var nodeModules = {}; - + function saveNodeList() { var nodeList = {}; - + for (var i in nodeConfigs) { if (nodeConfigs.hasOwnProperty(i)) { var nodeConfig = nodeConfigs[i]; @@ -75,7 +75,7 @@ var registry = (function() { return when.reject("Settings unavailable"); } } - + return { init: function() { if (settings.available()) { @@ -95,19 +95,19 @@ var registry = (function() { nodeList = []; nodeConfigCache = null; }, - + addNodeSet: function(id,set) { if (!set.err) { set.types.forEach(function(t) { nodeTypeToId[t] = id; }); } - + if (set.module) { nodeModules[set.module] = nodeModules[set.module]||{nodes:[]}; nodeModules[set.module].nodes.push(id); } - + nodeConfigs[id] = set; nodeList.push(id); nodeConfigCache = null; @@ -159,11 +159,39 @@ var registry = (function() { var list = []; for (var id in nodeConfigs) { if (nodeConfigs.hasOwnProperty(id)) { - list.push(filterNodeInfo(nodeConfigs[id])) + list.push(filterNodeInfo(nodeConfigs[id])); } } return list; }, + getModuleList: function() { + var list = []; + for (var module in nodeModules) { + if (nodeModules.hasOwnProperty(module)) { + var nodes = nodeModules[module].nodes; + var m = { + name: module, + nodes: [] + }; + for (var i = 0; i < nodes.length; ++i) { + m.nodes.push(filterNodeInfo(nodeConfigs[nodes[i]])); + } + list.push(m); + } + } + return list; + }, + getModule: function(module) { + var nodes = nodeModules[module].nodes; + var m = { + name: module, + nodes: [] + }; + for (var i = 0; i < nodes.length; ++i) { + m.nodes.push(filterNodeInfo(nodeConfigs[nodes[i]])); + } + return m; + }, registerNodeConstructor: function(type,constructor) { if (nodeConstructors[type]) { throw new Error(type+" already registered"); @@ -175,8 +203,8 @@ var registry = (function() { 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 @@ -201,7 +229,7 @@ var registry = (function() { } return nodeConfigCache; }, - + getNodeConfig: function(id) { var config = nodeConfigs[id]; if (config) { @@ -214,7 +242,7 @@ var registry = (function() { return null; } }, - + getNodeConstructor: function(type) { var config = nodeConfigs[nodeTypeToId[type]]; if (!config || (config.enabled && !config.err)) { @@ -222,7 +250,7 @@ var registry = (function() { } return null; }, - + clear: function() { nodeConfigCache = null; nodeConfigs = {}; @@ -230,20 +258,25 @@ var registry = (function() { nodeConstructors = {}; nodeTypeToId = {}; }, - + getTypeId: function(type) { return nodeTypeToId[type]; }, - + getModuleInfo: function(type) { return nodeModules[type]; }, - + enableNodeSet: function(id) { if (!settings.available()) { throw new Error("Settings unavailable"); } - var config = nodeConfigs[id]; + var config; + if (nodeTypeToId[id]) { + config = nodeConfigs[nodeTypeToId[id]]; + } else { + config = nodeConfigs[id]; + } if (config) { delete config.err; config.enabled = true; @@ -258,12 +291,17 @@ var registry = (function() { } return filterNodeInfo(config); }, - + disableNodeSet: function(id) { if (!settings.available()) { throw new Error("Settings unavailable"); } - var config = nodeConfigs[id]; + var config; + if (nodeTypeToId[id]) { + config = nodeConfigs[nodeTypeToId[id]]; + } else { + config = nodeConfigs[id]; + } if (config) { // TODO: persist setting config.enabled = false; @@ -274,9 +312,9 @@ var registry = (function() { } return filterNodeInfo(config); }, - + saveNodeList: saveNodeList, - + cleanNodeList: function() { var removed = false; for (var id in nodeConfigs) { @@ -331,13 +369,13 @@ function getNodeFiles(dir) { } } valid = valid && fs.existsSync(path.join(dir,fn.replace(/\.js$/,".html"))) - + if (valid) { result.push(path.join(dir,fn)); } } } else if (stats.isDirectory()) { - // Ignore /.dirs/, /lib/ /node_modules/ + // 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") { @@ -385,7 +423,7 @@ function scanTreeForNodesModules(moduleName) { } } catch(err) { } - + dir = up; up = path.resolve(path.join(dir,"..")); } @@ -451,7 +489,7 @@ function loadNodeConfig(file,module,name) { } var info = registry.getNodeInfo(id); - + var isEnabled = true; if (info) { @@ -460,7 +498,7 @@ function loadNodeConfig(file,module,name) { } isEnabled = info.enabled; } - + var node = { id: id, file: file, @@ -468,7 +506,7 @@ function loadNodeConfig(file,module,name) { enabled: isEnabled, loaded:false } - + if (module) { node.name = module+":"+name; node.module = module; @@ -477,21 +515,21 @@ function loadNodeConfig(file,module,name) { } try { var content = fs.readFileSync(node.template,'utf8'); - + var types = []; - + var regExp = /