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

Remove add/removeNode by filename api

Only support add/remove by module name
This commit is contained in:
Nick O'Leary 2015-03-26 11:39:35 +00:00
parent dd7f4f6752
commit cc1d080a5a
6 changed files with 3 additions and 291 deletions

View File

@ -42,9 +42,7 @@ module.exports = {
} }
var node = req.body; var node = req.body;
var promise; var promise;
if (node.file) { if (node.module) {
promise = redNodes.addNode(node.file).then(server.reportAddedModules);
} else if (node.module) {
var module = redNodes.getNodeModuleInfo(node.module); var module = redNodes.getNodeModuleInfo(node.module);
if (module) { if (module) {
res.send(400,"Module already loaded"); res.send(400,"Module already loaded");
@ -115,15 +113,9 @@ module.exports = {
var result = redNodes.getModuleInfo(module); var result = redNodes.getModuleInfo(module);
if (result) { if (result) {
res.send(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 { } else {
res.send(404); res.send(404);
} }
}
}, },
putSet: function(req,res) { putSet: function(req,res) {
@ -164,24 +156,7 @@ 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) {
var matching = getMatchingNodes(mod); return res.send(404);
if (matching.length === 1) {
// One match, assume correct
res.json(putNode(matching[0], 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;
@ -213,20 +188,6 @@ 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(node, enabled) { function putNode(node, enabled) {
var info; var info;

View File

@ -111,9 +111,6 @@ module.exports = {
getNode: flows.get, getNode: flows.get,
eachNode: flows.eachNode, eachNode: flows.eachNode,
addNode: registry.addNode,
removeNode: removeNode,
addModule: registry.addModule, addModule: registry.addModule,
removeModule: removeModule, removeModule: removeModule,

View File

@ -814,19 +814,6 @@ function loadNodeList(nodes) {
}); });
} }
function addNode(file) {
if (!settings.available()) {
throw new Error("Settings unavailable");
}
var nodes = [];
try {
nodes.push(loadNodeConfig(file,"node-red",path.basename(file).replace(/^\d+-/,"").replace(/\.js$/,""),settings.version));
} catch(err) {
return when.reject(err);
}
return loadNodeList(nodes);
}
function addModule(module) { function addModule(module) {
if (!settings.available()) { if (!settings.available()) {
throw new Error("Settings unavailable"); throw new Error("Settings unavailable");
@ -866,8 +853,6 @@ module.exports = {
getNodeConfigs: registry.getAllNodeConfigs, getNodeConfigs: registry.getAllNodeConfigs,
getNodeConfig: registry.getNodeConfig, getNodeConfig: registry.getNodeConfig,
addNode: addNode,
removeNode: registry.removeNode,
enableNode: registry.enableNodeSet, enableNode: registry.enableNodeSet,
disableNode: registry.disableNodeSet, disableNode: registry.disableNodeSet,

View File

@ -812,63 +812,6 @@ describe("nodes api", function() {
}); });
}); });
describe('no-ops if a node specified by type name already in the right state', function() {
function run(state,done) {
var node = {id:"123",enabled:state,types:['a']};
var settingsAvailable = sinon.stub(settings,'available', function() {
return true;
});
var getModuleInfo = sinon.stub(redNodes,'getModuleInfo',function(id) {
return null;
});
var enableNode = sinon.stub(redNodes,'enableNode',function(id) {
node.enabled = true;
return node;
});
var disableNode = sinon.stub(redNodes,'disableNode',function(id) {
node.enabled = false;
return node;
});
var getNodeList = sinon.stub(redNodes,'getNodeList', function() {
return [{name: 'foo', enabled: state}, {name: 'bar'}];
});
var getModuleVersion = sinon.stub(redNodes,'getModuleVersion', function() {
return '0.0.1';
});
request(app)
.put('/nodes/foo')
.send({enabled:state})
.expect(200)
.end(function(err,res) {
settingsAvailable.restore();
getModuleInfo.restore();
var enableNodeCalled = enableNode.called;
var disableNodeCalled = disableNode.called;
enableNode.restore();
disableNode.restore();
getNodeList.restore();
getModuleVersion.restore();
if (err) {
throw err;
}
enableNodeCalled.should.be.false;
disableNodeCalled.should.be.false;
res.body.should.have.property("name","foo");
res.body.should.have.property("version");
res.body.should.have.property("enabled",state);
done();
});
}
it('already enabled', function(done) {
run(true,done);
});
it('already disabled', function(done) {
run(false,done);
});
});
describe('does not no-op if err on a node in module', function() { describe('does not no-op if err on a node in module', function() {
function run(state,done) { function run(state,done) {
var node = {id:"123",enabled:state,types:['a'],err:"foo"}; var node = {id:"123",enabled:state,types:['a'],err:"foo"};

View File

@ -147,33 +147,15 @@ describe("red/nodes/index", function() {
return randomNodeInfo; return randomNodeInfo;
} }
}); });
sinon.stub(registry,"removeNode",function(id) {
return randomNodeInfo;
});
sinon.stub(registry,"disableNode",function(id) { sinon.stub(registry,"disableNode",function(id) {
return randomNodeInfo; return randomNodeInfo;
}); });
}); });
after(function() { after(function() {
registry.getNodeInfo.restore(); registry.getNodeInfo.restore();
registry.removeNode.restore();
registry.disableNode.restore(); registry.disableNode.restore();
}); });
it(': allows an unused node type to be removed',function(done) {
index.init(settings, storage);
index.registerType('test', TestNode);
index.loadFlows().then(function() {
var info = index.removeNode("5678");
registry.removeNode.calledOnce.should.be.true;
registry.removeNode.calledWith("5678").should.be.true;
info.should.eql(randomNodeInfo);
done();
}).otherwise(function(err) {
done(err);
});
});
it(': allows an unused node type to be disabled',function(done) { it(': allows an unused node type to be disabled',function(done) {
index.init(settings, storage); index.init(settings, storage);
index.registerType('test', TestNode); index.registerType('test', TestNode);
@ -188,21 +170,6 @@ describe("red/nodes/index", function() {
}); });
}); });
it(': prevents removing a node type that is in use',function(done) {
index.init(settings, storage);
index.registerType('test', TestNode);
index.loadFlows().then(function() {
/*jshint immed: false */
(function() {
index.removeNode("test");
}).should.throw();
done();
}).otherwise(function(err) {
done(err);
});
});
it(': prevents disabling a node type that is in use',function(done) { it(': prevents disabling a node type that is in use',function(done) {
index.init(settings, storage); index.init(settings, storage);
index.registerType('test', TestNode); index.registerType('test', TestNode);
@ -218,20 +185,6 @@ describe("red/nodes/index", function() {
}); });
}); });
it(': prevents removing a node type that is unknown',function(done) {
index.init(settings, storage);
index.registerType('test', TestNode);
index.loadFlows().then(function() {
/*jshint immed: false */
(function() {
index.removeNode("doesnotexist");
}).should.throw();
done();
}).otherwise(function(err) {
done(err);
});
});
it(': prevents disabling a node type that is unknown',function(done) { it(': prevents disabling a node type that is unknown',function(done) {
index.init(settings, storage); index.init(settings, storage);
index.registerType('test', TestNode); index.registerType('test', TestNode);

View File

@ -343,60 +343,6 @@ describe('red/nodes/registry', function() {
}); });
it('allows nodes to be added by filename', function(done) {
var settings = {
available: function() { return true; },
set: function(s,v) {return when.resolve();},
get: function(s) { return null;}
}
typeRegistry.init(settings);
typeRegistry.load("wontexist",true).then(function(){
var list = typeRegistry.getNodeList();
list.should.be.an.Array.and.be.empty;
// TODO: Needs module and name params for loadNodeConfig
typeRegistry.addNode(resourcesDir + "TestNode1/TestNode1.js").then(function(node) {
list = typeRegistry.getNodeList();
list[0].should.have.property("id","node-red/TestNode1");
list[0].should.have.property("name","TestNode1");
list[0].should.have.property("module","node-red");
list[0].should.have.property("types",["test-node-1"]);
list[0].should.have.property("enabled",true);
list[0].should.not.have.property("err");
node.should.be.an.Array.and.have.lengthOf(1);
node.should.eql(list);
done();
}).catch(function(e) {
done(e);
});
}).catch(function(e) {
done(e);
});
});
it('fails to add non-existent filename', function(done) {
typeRegistry.init(settingsWithStorage);
typeRegistry.load("wontexist",true).then(function(){
var list = typeRegistry.getNodeList();
list.should.be.an.Array.and.be.empty;
typeRegistry.addNode(resourcesDir + "DoesNotExist/DoesNotExist.js").then(function(nodes) {
nodes.should.be.an.Array.and.have.lengthOf(1);
nodes[0].should.have.property("id");
nodes[0].should.have.property("types",[]);
nodes[0].should.have.property("err");
done();
}).otherwise(function(e) {
done(e);
});
}).catch(function(e) {
done(e);
});
});
it('returns node info by type or id', function(done) { it('returns node info by type or id', function(done) {
typeRegistry.init(settings); typeRegistry.init(settings);
typeRegistry.load(resourcesDir + "TestNode1",true).then(function() { typeRegistry.load(resourcesDir + "TestNode1",true).then(function() {
@ -548,75 +494,6 @@ describe('red/nodes/registry', function() {
}); });
}); });
it('rejects adding duplicate nodes', function(done) {
typeRegistry.init(settingsWithStorage);
typeRegistry.load(resourcesDir + "TestNode1",true).then(function(){
var list = typeRegistry.getNodeList();
list.should.be.an.Array.and.have.lengthOf(1);
typeRegistry.addNode({file:resourcesDir + "TestNode1" + path.sep + "TestNode1.js"}).then(function(node) {
done(new Error("duplicate node loaded"));
}).otherwise(function(e) {
var list = typeRegistry.getNodeList();
list.should.be.an.Array.and.have.lengthOf(1);
done();
});
}).catch(function(e) {
done(e);
});
});
it('removes nodes from the registry', function(done) {
typeRegistry.init(settingsWithStorage);
typeRegistry.load(resourcesDir + "TestNode1",true).then(function() {
var list = typeRegistry.getNodeList();
list.should.be.an.Array.and.have.lengthOf(1);
list[0].should.have.property("id","node-red/TestNode1");
list[0].should.have.property("name","TestNode1");
list[0].should.have.property("module","node-red");
list[0].should.have.property("types",["test-node-1"]);
list[0].should.have.property("enabled",true);
list[0].should.have.property("loaded",true);
typeRegistry.getNodeConfigs().length.should.be.greaterThan(0);
var info = typeRegistry.removeNode(list[0].id);
info.should.have.property("id",list[0].id);
info.should.have.property("enabled",false);
info.should.have.property("loaded",false);
typeRegistry.getNodeList().should.be.an.Array.and.be.empty;
typeRegistry.getNodeConfigs().length.should.equal(0);
var nodeConstructor = typeRegistry.get("test-node-1");
(typeof nodeConstructor).should.be.equal("undefined");
done();
}).catch(function(e) {
done(e);
});
});
it('rejects removing unknown nodes from the registry', function(done) {
typeRegistry.init(settings);
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) { it('scans the node_modules path for node files', function(done) {
var fs = require("fs"); var fs = require("fs");
var path = require("path"); var path = require("path");
@ -1073,10 +950,6 @@ describe('red/nodes/registry', function() {
(function() { (function() {
typeRegistry.removeModule("123"); typeRegistry.removeModule("123");
}).should.throw("Settings unavailable"); }).should.throw("Settings unavailable");
/*jshint immed: false */
(function() {
typeRegistry.addNode("123");
}).should.throw("Settings unavailable");
done(); done();
}); });