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

Merge pull request #511 from anna2130/nr-cli-enhancements

Updates to match cli changes
This commit is contained in:
Nick O'Leary 2014-12-17 13:10:49 +00:00
commit 0c699ae57f
4 changed files with 76 additions and 21 deletions

View File

@ -97,6 +97,9 @@ module.exports = {
var result = null; var result = null;
if (req.get("accept") === "application/json") { if (req.get("accept") === "application/json") {
result = redNodes.getNodeInfo(id); result = redNodes.getNodeInfo(id);
if (result) {
result.version = redNodes.getModuleVersion(req.params.mod);
}
} else { } else {
result = redNodes.getNodeConfig(id); result = redNodes.getNodeConfig(id);
} }
@ -113,7 +116,13 @@ module.exports = {
if (result) { if (result) {
res.send(result); res.send(result);
} else { } else {
res.send(404); // check if module is actually a node-set
var matching = getMatchingNodes(module);
if (matching.length > 0) {
res.json(matching);
} else {
res.send(404);
}
} }
}, },
@ -128,7 +137,7 @@ module.exports = {
return; return;
} }
try { try {
var id = req.params.mod+"/"+req.params.set; var id = req.params.mod + "/" + req.params.set;
var node = redNodes.getNodeInfo(id); var node = redNodes.getNodeInfo(id);
var info; var info;
if (!node) { if (!node) {
@ -136,22 +145,7 @@ module.exports = {
} else if (!node.err && node.enabled === body.enabled) { } else if (!node.err && node.enabled === body.enabled) {
res.json(node); res.json(node);
} else { } else {
if (body.enabled) { res.json(putNode(id, body.enabled));
info = redNodes.enableNode(id);
} else {
info = redNodes.disableNode(id);
}
if (info.enabled == body.enabled && !info.err) {
comms.publish("node/"+(body.enabled?"enabled":"disabled"),info,false);
util.log("[red] "+(body.enabled?"Enabled":"Disabled")+" node types:");
for (var i=0;i<info.types.length;i++) {
util.log("[red] - "+info.types[i]);
}
} else if (body.enabled && info.err) {
util.log("[red] Failed to enable node:");
util.log("[red] - "+info.name+" : "+info.err);
}
res.json(info);
} }
} catch(err) { } catch(err) {
res.send(400,err.toString()); res.send(400,err.toString());
@ -172,9 +166,26 @@ module.exports = {
var mod = req.params.mod; var mod = req.params.mod;
var module = redNodes.getModuleInfo(mod); var module = redNodes.getModuleInfo(mod);
if (!module) { if (!module) {
res.send(404); var matching = getMatchingNodes(mod);
return; if (matching.length === 1) {
// One match, assume correct
res.json(putNode(matching[0].id, body.enabled));
return;
} else if (matching.length > 1) {
// Multiple matches, need clarification
result = {
multipleMatches: true,
matches: matching
};
res.json(result);
return;
} else {
// Doesn't exist
res.send(404);
return;
}
} }
var nodes = module.nodes; var nodes = module.nodes;
for (var i = 0; i < nodes.length; ++i) { for (var i = 0; i < nodes.length; ++i) {
var node = nodes[i]; var node = nodes[i];
@ -203,3 +214,40 @@ module.exports = {
} }
} }
}; };
function getMatchingNodes(node) {
var nodes = redNodes.getNodeList();
var matching = [];
nodes.forEach(function(n) {
if (n.name === node) {
n.version = redNodes.getModuleVersion(n.module);
matching.push(n);
}
});
return matching;
}
function putNode(id, enabled) {
var info;
if (enabled) {
info = redNodes.enableNode(id);
} else {
info = redNodes.disableNode(id);
}
if (info.enabled === enabled && !info.err) {
comms.publish("node/"+(enabled?"enabled":"disabled"),info,false);
util.log("[red] "+(enabled?"Enabled":"Disabled")+" node types:");
for (var i=0;i<info.types.length;i++) {
util.log("[red] - "+info.types[i]);
}
} else if (enabled && info.err) {
util.log("[red] Failed to enable node:");
util.log("[red] - "+info.name+" : "+info.err);
}
return info;
}

View File

@ -124,6 +124,7 @@ module.exports = {
getModuleInfo: registry.getModuleInfo, getModuleInfo: registry.getModuleInfo,
getModuleList: registry.getModuleList, getModuleList: registry.getModuleList,
getModuleVersion: registry.getModuleVersion,
getNodeConfigs: registry.getNodeConfigs, getNodeConfigs: registry.getNodeConfigs,
getNodeConfig: registry.getNodeConfig, getNodeConfig: registry.getNodeConfig,

View File

@ -275,6 +275,9 @@ var registry = (function() {
return null; return null;
} }
}, },
getModuleVersion: function(module) {
return moduleConfigs[module].version;
},
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");
@ -287,7 +290,6 @@ var registry = (function() {
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
@ -807,6 +809,7 @@ module.exports = {
getModuleInfo: registry.getModuleInfo, getModuleInfo: registry.getModuleInfo,
getModuleList: registry.getModuleList, getModuleList: registry.getModuleList,
getModuleVersion: registry.getModuleVersion,
getNodeConfigs: registry.getAllNodeConfigs, getNodeConfigs: registry.getAllNodeConfigs,
getNodeConfig: registry.getNodeConfig, getNodeConfig: registry.getNodeConfig,

View File

@ -117,6 +117,9 @@ describe("nodes api", function() {
var getNodeInfo = sinon.stub(redNodes,'getNodeInfo', function(id) { var getNodeInfo = sinon.stub(redNodes,'getNodeInfo', function(id) {
return {"node-red/123":{id:"node-red/123"}}[id]; return {"node-red/123":{id:"node-red/123"}}[id];
}); });
var getModuleVersion = sinon.stub(redNodes,'getModuleVersion', function(module) {
return {"node-red": {version: "0.0.1"}}[module];
});
request(app) request(app)
.get('/nodes/node-red/123') .get('/nodes/node-red/123')
.set('Accept', 'application/json') .set('Accept', 'application/json')