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

Add remove node api

This commit is contained in:
Nick O'Leary 2014-08-07 22:20:06 +01:00
parent 43ad8706aa
commit 58c2f5dd3d
5 changed files with 77 additions and 16 deletions

View File

@ -54,6 +54,9 @@ function init(_settings,storage) {
function removeNode(info) {
var nodeInfo = registry.getNodeInfo(info);
if (!nodeInfo) {
throw new Error("Unrecognised type/id: "+info);
}
var inUse = {};
flows.each(function(n) {
inUse[n.type] = (inUse[n.type]||0)+1;
@ -66,7 +69,7 @@ function removeNode(info) {
});
if (nodesInUse.length > 0) {
var msg = nodesInUse.join(", ");
throw Error("Type in use: "+msg);
throw new Error("Type in use: "+msg);
}
return registry.removeNode(nodeInfo.id);
}

View File

@ -61,18 +61,19 @@ var registry = (function() {
},
removeNode: function(id) {
var config = nodeConfigs[id];
if (config) {
delete nodeConfigs[id];
var i = nodeList.indexOf(id);
if (i > -1) {
nodeList.splice(i,1);
}
config.types.forEach(function(t) {
delete nodeConstructors[t];
delete nodeTypeToId[t];
});
nodeConfigCache = null;
if (!config) {
throw new Error("Unrecognised id: "+id);
}
delete nodeConfigs[id];
var i = nodeList.indexOf(id);
if (i > -1) {
nodeList.splice(i,1);
}
config.types.forEach(function(t) {
delete nodeConstructors[t];
delete nodeTypeToId[t];
});
nodeConfigCache = null;
return filterNodeInfo(config);
},
getNodeInfo: function(typeOrId) {

View File

@ -70,6 +70,10 @@ function createServer(_server,_settings) {
}
redNodes.addNode(node).then(function(info) {
comms.publish("node/added",info,false);
util.log("[red] Added node types:");
for (var i=0;i<info.types.length;i++) {
util.log("[red] - "+info.types[i]);
}
res.json(info);
}).otherwise(function(err) {
res.send(400,err.toString());
@ -80,8 +84,28 @@ function createServer(_server,_settings) {
}
);
app.delete("/nodes/:id",
function(req,res) {
var id = req.params.id;
try {
var info = redNodes.removeNode(id);
comms.publish("node/removed",info,false);
util.log("[red] Removed node types:");
for (var i=0;i<info.types.length;i++) {
util.log("[red] - "+info.types[i]);
}
res.json(info);
} catch(err) {
res.send(400,err.toString());
};
},
function(err,req,res,next) {
res.send(400,err);
}
);
app.get("/nodes/:id", function(req,res) {
var type = req.params.id;
var id = req.params.id;
var config = redNodes.getNodeConfig(id);
if (config) {
res.send(config);

View File

@ -139,6 +139,8 @@ describe("red/nodes/index", function() {
sinon.stub(registry,"getNodeInfo",function(id) {
if (id == "test") {
return {id:"1234",types:["test"]};
} else if (id == "doesnotexist") {
return null;
} else {
return randomNodeInfo;
}
@ -164,8 +166,7 @@ describe("red/nodes/index", function() {
}).otherwise(function(err) {
done(err);
});
});
});
it(': prevents removing a node type that is in use',function(done) {
index.init({}, storage);
@ -180,8 +181,23 @@ describe("red/nodes/index", function() {
}).otherwise(function(err) {
done(err);
});
});
it(': prevents removing a node type that is unknown',function(done) {
index.init({}, storage);
index.registerType('test', TestNode);
index.loadFlows().then(function() {
/*jshint immed: false */
(function() {
index.removeNode("doesnotexist");
}).should.throw();
done();
}).otherwise(function(err) {
done(err);
});
});
});

View File

@ -384,7 +384,24 @@ describe('NodeRegistry', function() {
}).catch(function(e) {
done(e);
});
});
it('rejects removing unknown nodes from the registry', function(done) {
typeRegistry.init({});
typeRegistry.load("wontexist",true).then(function() {
var list = typeRegistry.getNodeList();
list.should.be.an.Array.and.be.empty;
/*jshint immed: false */
(function() {
typeRegistry.removeNode("1234");
}).should.throw();
done();
}).catch(function(e) {
done(e);
});
});
it('scans the node_modules path for node files', function(done) {