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:
commit
0c699ae57f
@ -97,6 +97,9 @@ module.exports = {
|
||||
var result = null;
|
||||
if (req.get("accept") === "application/json") {
|
||||
result = redNodes.getNodeInfo(id);
|
||||
if (result) {
|
||||
result.version = redNodes.getModuleVersion(req.params.mod);
|
||||
}
|
||||
} else {
|
||||
result = redNodes.getNodeConfig(id);
|
||||
}
|
||||
@ -112,9 +115,15 @@ module.exports = {
|
||||
var result = redNodes.getModuleInfo(module);
|
||||
if (result) {
|
||||
res.send(result);
|
||||
} else {
|
||||
// check if module is actually a node-set
|
||||
var matching = getMatchingNodes(module);
|
||||
if (matching.length > 0) {
|
||||
res.json(matching);
|
||||
} else {
|
||||
res.send(404);
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
putSet: function(req,res) {
|
||||
@ -136,22 +145,7 @@ module.exports = {
|
||||
} else if (!node.err && node.enabled === body.enabled) {
|
||||
res.json(node);
|
||||
} else {
|
||||
if (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);
|
||||
res.json(putNode(id, body.enabled));
|
||||
}
|
||||
} catch(err) {
|
||||
res.send(400,err.toString());
|
||||
@ -172,9 +166,26 @@ module.exports = {
|
||||
var mod = req.params.mod;
|
||||
var module = redNodes.getModuleInfo(mod);
|
||||
if (!module) {
|
||||
var matching = getMatchingNodes(mod);
|
||||
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;
|
||||
for (var i = 0; i < nodes.length; ++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;
|
||||
}
|
||||
|
@ -124,6 +124,7 @@ module.exports = {
|
||||
|
||||
getModuleInfo: registry.getModuleInfo,
|
||||
getModuleList: registry.getModuleList,
|
||||
getModuleVersion: registry.getModuleVersion,
|
||||
|
||||
getNodeConfigs: registry.getNodeConfigs,
|
||||
getNodeConfig: registry.getNodeConfig,
|
||||
|
@ -275,6 +275,9 @@ var registry = (function() {
|
||||
return null;
|
||||
}
|
||||
},
|
||||
getModuleVersion: function(module) {
|
||||
return moduleConfigs[module].version;
|
||||
},
|
||||
registerNodeConstructor: function(type,constructor) {
|
||||
if (nodeConstructors[type]) {
|
||||
throw new Error(type+" already registered");
|
||||
@ -287,7 +290,6 @@ var registry = (function() {
|
||||
events.emit("type-registered",type);
|
||||
},
|
||||
|
||||
|
||||
/**
|
||||
* Gets all of the node template configs
|
||||
* @return all of the node templates in a single string
|
||||
@ -807,6 +809,7 @@ module.exports = {
|
||||
|
||||
getModuleInfo: registry.getModuleInfo,
|
||||
getModuleList: registry.getModuleList,
|
||||
getModuleVersion: registry.getModuleVersion,
|
||||
|
||||
getNodeConfigs: registry.getAllNodeConfigs,
|
||||
getNodeConfig: registry.getNodeConfig,
|
||||
|
@ -117,6 +117,9 @@ describe("nodes api", function() {
|
||||
var getNodeInfo = sinon.stub(redNodes,'getNodeInfo', function(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)
|
||||
.get('/nodes/node-red/123')
|
||||
.set('Accept', 'application/json')
|
||||
|
Loading…
Reference in New Issue
Block a user