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

Functionality to get installed module info from the cli

This commit is contained in:
Anna Thomas 2014-11-06 10:00:25 +00:00
parent 266a644ca6
commit 3e24601518
4 changed files with 141 additions and 62 deletions

View File

@ -19,6 +19,7 @@ var util = require('util');
var ui = require("./ui"); var ui = require("./ui");
var nodes = require("./nodes"); var nodes = require("./nodes");
var modules = require("./modules");
var flows = require("./flows"); var flows = require("./flows");
var library = require("./library"); var library = require("./library");
@ -27,12 +28,12 @@ var settings = require("../settings");
var errorHandler = function(err,req,res,next) { var errorHandler = function(err,req,res,next) {
//TODO: standardize json response //TODO: standardize json response
res.send(400,err.toString()); res.send(400,err.toString());
} };
function init(adminApp) { function init(adminApp) {
adminApp.use(express.json()); adminApp.use(express.json());
library.init(adminApp); library.init(adminApp);
// Editor // Editor
@ -42,11 +43,11 @@ function init(adminApp) {
adminApp.get("/settings",ui.settings); adminApp.get("/settings",ui.settings);
adminApp.use("/",ui.editor); adminApp.use("/",ui.editor);
} }
// Flows // Flows
adminApp.get("/flows",flows.get); adminApp.get("/flows",flows.get);
adminApp.post("/flows",flows.post); adminApp.post("/flows",flows.post);
// Nodes // Nodes
adminApp.get("/nodes",nodes.getAll); adminApp.get("/nodes",nodes.getAll);
adminApp.post("/nodes",nodes.post); adminApp.post("/nodes",nodes.post);
@ -54,17 +55,21 @@ function init(adminApp) {
adminApp.get("/nodes/:id",nodes.get); adminApp.get("/nodes/:id",nodes.get);
adminApp.put("/nodes/:id",nodes.put); adminApp.put("/nodes/:id",nodes.put);
adminApp.delete("/nodes/:id",nodes.delete); adminApp.delete("/nodes/:id",nodes.delete);
// Modules
adminApp.get("/modules",modules.getAll);
adminApp.get("/modules/:id",modules.get);
// Library // Library
adminApp.post(new RegExp("/library/flows\/(.*)"),library.post); adminApp.post(new RegExp("/library/flows\/(.*)"),library.post);
adminApp.get("/library/flows",library.getAll); adminApp.get("/library/flows",library.getAll);
adminApp.get(new RegExp("/library/flows\/(.*)"),library.get); adminApp.get(new RegExp("/library/flows\/(.*)"),library.get);
// Error Handler // Error Handler
adminApp.use(errorHandler); adminApp.use(errorHandler);
} }
module.exports = { module.exports = {
init: init init: init
} };

32
red/api/modules.js Normal file
View File

@ -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);
}
}
};

View File

@ -28,14 +28,14 @@ function registerType(type,constructor,opts) {
if (opts && opts.credentials) { if (opts && opts.credentials) {
credentials.register(type,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 * Called from a Node's constructor function, invokes the super-class
* constructor and attaches any credentials to the node. * constructor and attaches any credentials to the node.
* @param node the node object being created * @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) { function createNode(node,def) {
Node.call(node,def); Node.call(node,def);
@ -95,40 +95,42 @@ module.exports = {
// Lifecycle // Lifecycle
init: init, init: init,
load: registry.load, load: registry.load,
// Node registry // Node registry
createNode: createNode, createNode: createNode,
getNode: flows.get, getNode: flows.get,
addNode: registry.addNode, addNode: registry.addNode,
removeNode: removeNode, removeNode: removeNode,
addModule: registry.addModule, addModule: registry.addModule,
removeModule: removeModule, removeModule: removeModule,
enableNode: registry.enableNode, enableNode: registry.enableNode,
disableNode: disableNode, disableNode: disableNode,
// Node type registry // Node type registry
registerType: registerType, registerType: registerType,
getType: registry.get, getType: registry.get,
getNodeInfo: registry.getNodeInfo, getNodeInfo: registry.getNodeInfo,
getNodeModuleInfo: registry.getNodeModuleInfo, getNodeModuleInfo: registry.getNodeModuleInfo,
getModule: registry.getModule,
getNodeList: registry.getNodeList, getNodeList: registry.getNodeList,
getModuleList: registry.getModuleList,
getNodeConfigs: registry.getNodeConfigs, getNodeConfigs: registry.getNodeConfigs,
getNodeConfig: registry.getNodeConfig, getNodeConfig: registry.getNodeConfig,
clearRegistry: registry.clear, clearRegistry: registry.clear,
cleanNodeList: registry.cleanNodeList, cleanNodeList: registry.cleanNodeList,
// Flow handling // Flow handling
loadFlows: flows.load, loadFlows: flows.load,
stopFlows: flows.stopFlows, stopFlows: flows.stopFlows,
setFlows: flows.setFlows, setFlows: flows.setFlows,
getFlows: flows.getFlows, getFlows: flows.getFlows,
// Credentials // Credentials
addCredentials: credentials.add, addCredentials: credentials.add,
getCredentials: credentials.get, getCredentials: credentials.get,
deleteCredentials: credentials.delete deleteCredentials: credentials.delete
} };

View File

@ -13,13 +13,13 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
**/ **/
var util = require("util"); var util = require("util");
var when = require("when"); var when = require("when");
var whenNode = require('when/node'); var whenNode = require('when/node');
var fs = require("fs"); var fs = require("fs");
var path = require("path"); var path = require("path");
var crypto = require("crypto"); var crypto = require("crypto");
var UglifyJS = require("uglify-js"); var UglifyJS = require("uglify-js");
var events = require("../events"); var events = require("../events");
@ -53,10 +53,10 @@ var registry = (function() {
var nodeConstructors = {}; var nodeConstructors = {};
var nodeTypeToId = {}; var nodeTypeToId = {};
var nodeModules = {}; var nodeModules = {};
function saveNodeList() { function saveNodeList() {
var nodeList = {}; var nodeList = {};
for (var i in nodeConfigs) { for (var i in nodeConfigs) {
if (nodeConfigs.hasOwnProperty(i)) { if (nodeConfigs.hasOwnProperty(i)) {
var nodeConfig = nodeConfigs[i]; var nodeConfig = nodeConfigs[i];
@ -75,7 +75,7 @@ var registry = (function() {
return when.reject("Settings unavailable"); return when.reject("Settings unavailable");
} }
} }
return { return {
init: function() { init: function() {
if (settings.available()) { if (settings.available()) {
@ -95,19 +95,19 @@ var registry = (function() {
nodeList = []; nodeList = [];
nodeConfigCache = null; nodeConfigCache = null;
}, },
addNodeSet: function(id,set) { addNodeSet: function(id,set) {
if (!set.err) { if (!set.err) {
set.types.forEach(function(t) { set.types.forEach(function(t) {
nodeTypeToId[t] = id; nodeTypeToId[t] = id;
}); });
} }
if (set.module) { if (set.module) {
nodeModules[set.module] = nodeModules[set.module]||{nodes:[]}; nodeModules[set.module] = nodeModules[set.module]||{nodes:[]};
nodeModules[set.module].nodes.push(id); nodeModules[set.module].nodes.push(id);
} }
nodeConfigs[id] = set; nodeConfigs[id] = set;
nodeList.push(id); nodeList.push(id);
nodeConfigCache = null; nodeConfigCache = null;
@ -159,11 +159,39 @@ var registry = (function() {
var list = []; var list = [];
for (var id in nodeConfigs) { for (var id in nodeConfigs) {
if (nodeConfigs.hasOwnProperty(id)) { if (nodeConfigs.hasOwnProperty(id)) {
list.push(filterNodeInfo(nodeConfigs[id])) list.push(filterNodeInfo(nodeConfigs[id]));
} }
} }
return list; 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) { registerNodeConstructor: function(type,constructor) {
if (nodeConstructors[type]) { if (nodeConstructors[type]) {
throw new Error(type+" already registered"); throw new Error(type+" already registered");
@ -175,8 +203,8 @@ var registry = (function() {
nodeConstructors[type] = constructor; nodeConstructors[type] = constructor;
events.emit("type-registered",type); events.emit("type-registered",type);
}, },
/** /**
* Gets all of the node template configs * Gets all of the node template configs
* @return all of the node templates in a single string * @return all of the node templates in a single string
@ -201,7 +229,7 @@ var registry = (function() {
} }
return nodeConfigCache; return nodeConfigCache;
}, },
getNodeConfig: function(id) { getNodeConfig: function(id) {
var config = nodeConfigs[id]; var config = nodeConfigs[id];
if (config) { if (config) {
@ -214,7 +242,7 @@ var registry = (function() {
return null; return null;
} }
}, },
getNodeConstructor: function(type) { getNodeConstructor: function(type) {
var config = nodeConfigs[nodeTypeToId[type]]; var config = nodeConfigs[nodeTypeToId[type]];
if (!config || (config.enabled && !config.err)) { if (!config || (config.enabled && !config.err)) {
@ -222,7 +250,7 @@ var registry = (function() {
} }
return null; return null;
}, },
clear: function() { clear: function() {
nodeConfigCache = null; nodeConfigCache = null;
nodeConfigs = {}; nodeConfigs = {};
@ -230,20 +258,25 @@ var registry = (function() {
nodeConstructors = {}; nodeConstructors = {};
nodeTypeToId = {}; nodeTypeToId = {};
}, },
getTypeId: function(type) { getTypeId: function(type) {
return nodeTypeToId[type]; return nodeTypeToId[type];
}, },
getModuleInfo: function(type) { getModuleInfo: function(type) {
return nodeModules[type]; return nodeModules[type];
}, },
enableNodeSet: function(id) { enableNodeSet: function(id) {
if (!settings.available()) { if (!settings.available()) {
throw new Error("Settings unavailable"); throw new Error("Settings unavailable");
} }
var config = nodeConfigs[id]; var config;
if (nodeTypeToId[id]) {
config = nodeConfigs[nodeTypeToId[id]];
} else {
config = nodeConfigs[id];
}
if (config) { if (config) {
delete config.err; delete config.err;
config.enabled = true; config.enabled = true;
@ -258,12 +291,17 @@ var registry = (function() {
} }
return filterNodeInfo(config); return filterNodeInfo(config);
}, },
disableNodeSet: function(id) { disableNodeSet: function(id) {
if (!settings.available()) { if (!settings.available()) {
throw new Error("Settings unavailable"); throw new Error("Settings unavailable");
} }
var config = nodeConfigs[id]; var config;
if (nodeTypeToId[id]) {
config = nodeConfigs[nodeTypeToId[id]];
} else {
config = nodeConfigs[id];
}
if (config) { if (config) {
// TODO: persist setting // TODO: persist setting
config.enabled = false; config.enabled = false;
@ -274,9 +312,9 @@ var registry = (function() {
} }
return filterNodeInfo(config); return filterNodeInfo(config);
}, },
saveNodeList: saveNodeList, saveNodeList: saveNodeList,
cleanNodeList: function() { cleanNodeList: function() {
var removed = false; var removed = false;
for (var id in nodeConfigs) { for (var id in nodeConfigs) {
@ -331,13 +369,13 @@ function getNodeFiles(dir) {
} }
} }
valid = valid && fs.existsSync(path.join(dir,fn.replace(/\.js$/,".html"))) valid = valid && fs.existsSync(path.join(dir,fn.replace(/\.js$/,".html")))
if (valid) { if (valid) {
result.push(path.join(dir,fn)); result.push(path.join(dir,fn));
} }
} }
} else if (stats.isDirectory()) { } else if (stats.isDirectory()) {
// Ignore /.dirs/, /lib/ /node_modules/ // Ignore /.dirs/, /lib/ /node_modules/
if (!/^(\..*|lib|icons|node_modules|test)$/.test(fn)) { if (!/^(\..*|lib|icons|node_modules|test)$/.test(fn)) {
result = result.concat(getNodeFiles(path.join(dir,fn))); result = result.concat(getNodeFiles(path.join(dir,fn)));
} else if (fn === "icons") { } else if (fn === "icons") {
@ -385,7 +423,7 @@ function scanTreeForNodesModules(moduleName) {
} }
} catch(err) { } catch(err) {
} }
dir = up; dir = up;
up = path.resolve(path.join(dir,"..")); up = path.resolve(path.join(dir,".."));
} }
@ -451,7 +489,7 @@ function loadNodeConfig(file,module,name) {
} }
var info = registry.getNodeInfo(id); var info = registry.getNodeInfo(id);
var isEnabled = true; var isEnabled = true;
if (info) { if (info) {
@ -460,7 +498,7 @@ function loadNodeConfig(file,module,name) {
} }
isEnabled = info.enabled; isEnabled = info.enabled;
} }
var node = { var node = {
id: id, id: id,
file: file, file: file,
@ -468,7 +506,7 @@ function loadNodeConfig(file,module,name) {
enabled: isEnabled, enabled: isEnabled,
loaded:false loaded:false
} }
if (module) { if (module) {
node.name = module+":"+name; node.name = module+":"+name;
node.module = module; node.module = module;
@ -477,21 +515,21 @@ function loadNodeConfig(file,module,name) {
} }
try { try {
var content = fs.readFileSync(node.template,'utf8'); var content = fs.readFileSync(node.template,'utf8');
var types = []; var types = [];
var regExp = /<script ([^>]*)data-template-name=['"]([^'"]*)['"]/gi; var regExp = /<script ([^>]*)data-template-name=['"]([^'"]*)['"]/gi;
var match = null; var match = null;
while((match = regExp.exec(content)) !== null) { while((match = regExp.exec(content)) !== null) {
types.push(match[2]); types.push(match[2]);
} }
node.types = types; node.types = types;
node.config = content; node.config = content;
// TODO: parse out the javascript portion of the template // TODO: parse out the javascript portion of the template
node.script = ""; node.script = "";
for (var i=0;i<node.types.length;i++) { for (var i=0;i<node.types.length;i++) {
if (registry.getTypeId(node.types[i])) { if (registry.getTypeId(node.types[i])) {
node.err = node.types[i]+" already registered"; node.err = node.types[i]+" already registered";
@ -525,7 +563,7 @@ function load(defaultNodesDir,disableNodePathScan) {
} else { } else {
nodeFiles = getNodeFiles(__dirname+"/../../nodes"); nodeFiles = getNodeFiles(__dirname+"/../../nodes");
} }
if (settings.nodesDir) { if (settings.nodesDir) {
var dir = settings.nodesDir; var dir = settings.nodesDir;
if (typeof settings.nodesDir == "string") { if (typeof settings.nodesDir == "string") {
@ -540,10 +578,10 @@ function load(defaultNodesDir,disableNodePathScan) {
try { try {
nodes.push(loadNodeConfig(file)); nodes.push(loadNodeConfig(file));
} catch(err) { } catch(err) {
// //
} }
}); });
// TODO: disabling npm module loading if defaultNodesDir set // TODO: disabling npm module loading if defaultNodesDir set
// This indicates a test is being run - don't want to pick up // This indicates a test is being run - don't want to pick up
// unexpected nodes. // unexpected nodes.
@ -561,18 +599,18 @@ function load(defaultNodesDir,disableNodePathScan) {
promises.push(loadNodeModule(node)); promises.push(loadNodeModule(node));
} }
}); });
//resolve([]); //resolve([]);
when.settle(promises).then(function(results) { when.settle(promises).then(function(results) {
// Trigger a load of the configs to get it precached // Trigger a load of the configs to get it precached
registry.getAllNodeConfigs(); registry.getAllNodeConfigs();
if (settings.available()) { if (settings.available()) {
resolve(registry.saveNodeList()); resolve(registry.saveNodeList());
} else { } else {
resolve(); resolve();
} }
}); });
}); });
} }
@ -582,7 +620,7 @@ function load(defaultNodesDir,disableNodePathScan) {
* @return a promise that resolves to an update node info object. The object * @return a promise that resolves to an update node info object. The object
* has the following properties added: * has the following properties added:
* err: any error encountered whilst loading the node * err: any error encountered whilst loading the node
* *
*/ */
function loadNodeModule(node) { function loadNodeModule(node) {
var nodeDir = path.dirname(node.file); var nodeDir = path.dirname(node.file);
@ -627,7 +665,7 @@ function loadNodeList(nodes) {
promises.push(node); promises.push(node);
} }
}); });
return when.settle(promises).then(function(results) { return when.settle(promises).then(function(results) {
return registry.saveNodeList().then(function() { return registry.saveNodeList().then(function() {
var list = results.map(function(r) { var list = results.map(function(r) {
@ -643,7 +681,7 @@ function addNode(file) {
throw new Error("Settings unavailable"); throw new Error("Settings unavailable");
} }
var nodes = []; var nodes = [];
try { try {
nodes.push(loadNodeConfig(file)); nodes.push(loadNodeConfig(file));
} catch(err) { } catch(err) {
return when.reject(err); return when.reject(err);
@ -679,14 +717,16 @@ module.exports = {
get: registry.getNodeConstructor, get: registry.getNodeConstructor,
getNodeInfo: registry.getNodeInfo, getNodeInfo: registry.getNodeInfo,
getNodeModuleInfo: registry.getModuleInfo, getNodeModuleInfo: registry.getModuleInfo,
getModule: registry.getModule,
getNodeList: registry.getNodeList, getNodeList: registry.getNodeList,
getModuleList: registry.getModuleList,
getNodeConfigs: registry.getAllNodeConfigs, getNodeConfigs: registry.getAllNodeConfigs,
getNodeConfig: registry.getNodeConfig, getNodeConfig: registry.getNodeConfig,
addNode: addNode, addNode: addNode,
removeNode: registry.removeNode, removeNode: registry.removeNode,
enableNode: registry.enableNodeSet, enableNode: registry.enableNodeSet,
disableNode: registry.disableNodeSet, disableNode: registry.disableNodeSet,
addModule: addModule, addModule: addModule,
removeModule: registry.removeModule, removeModule: registry.removeModule,
cleanNodeList: registry.cleanNodeList cleanNodeList: registry.cleanNodeList