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

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

WIP: Command Line Tool API
This commit is contained in:
Nick O'Leary 2014-11-17 13:34:24 +00:00
commit 71bd5cd9e9
6 changed files with 486 additions and 185 deletions

View File

@ -19,6 +19,7 @@ var util = require('util');
var ui = require("./ui");
var nodes = require("./nodes");
var plugins = require("./plugins");
var flows = require("./flows");
var library = require("./library");
@ -27,7 +28,7 @@ var settings = require("../settings");
var errorHandler = function(err,req,res,next) {
//TODO: standardize json response
res.send(400,err.toString());
}
};
function init(adminApp) {
@ -55,6 +56,10 @@ function init(adminApp) {
adminApp.put("/nodes/:id",nodes.put);
adminApp.delete("/nodes/:id",nodes.delete);
// Plugins
adminApp.get("/plugins",plugins.getAll);
adminApp.get("/plugins/:id",plugins.get);
// Library
adminApp.post(new RegExp("/library/flows\/(.*)"),library.post);
adminApp.get("/library/flows",library.getAll);
@ -67,4 +72,4 @@ function init(adminApp) {
module.exports = {
init: init
}
};

32
red/api/plugins.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.getPluginList());
},
get: function(req,res) {
var id = req.params.id;
var result = redNodes.getPluginInfo(id);
if (result) {
res.send(result);
} else {
res.send(404);
}
}
};

View File

@ -114,7 +114,9 @@ module.exports = {
getType: registry.get,
getNodeInfo: registry.getNodeInfo,
getNodeModuleInfo: registry.getNodeModuleInfo,
getPluginInfo: registry.getPluginInfo,
getNodeList: registry.getNodeList,
getPluginList: registry.getPluginList,
getNodeConfigs: registry.getNodeConfigs,
getNodeConfig: registry.getNodeConfig,
clearRegistry: registry.clear,
@ -130,5 +132,5 @@ module.exports = {
addCredentials: credentials.add,
getCredentials: credentials.get,
deleteCredentials: credentials.delete
}
};

View File

@ -33,7 +33,7 @@ function filterNodeInfo(n) {
name: n.name,
types: n.types,
enabled: n.enabled
}
};
if (n.hasOwnProperty("loaded")) {
r.loaded = n.loaded;
}
@ -159,11 +159,39 @@ var registry = (function() {
var list = [];
for (var id in nodeConfigs) {
if (nodeConfigs.hasOwnProperty(id)) {
list.push(filterNodeInfo(nodeConfigs[id]))
list.push(filterNodeInfo(nodeConfigs[id]));
}
}
return list;
},
getPluginList: function() {
var list = [];
for (var plugin in nodeModules) {
if (nodeModules.hasOwnProperty(plugin)) {
var nodes = nodeModules[plugin].nodes;
var m = {
name: plugin,
nodes: []
};
for (var i = 0; i < nodes.length; ++i) {
m.nodes.push(filterNodeInfo(nodeConfigs[nodes[i]]));
}
list.push(m);
}
}
return list;
},
getPluginInfo: function(plugin) {
var nodes = nodeModules[plugin].nodes;
var m = {
name: plugin,
nodes: []
};
for (var i = 0; i < nodes.length; ++i) {
m.nodes.push(filterNodeInfo(nodeConfigs[nodes[i]]));
}
return m;
},
registerNodeConstructor: function(type,constructor) {
if (nodeConstructors[type]) {
throw new Error(type+" already registered");
@ -243,7 +271,12 @@ var registry = (function() {
if (!settings.available()) {
throw new Error("Settings unavailable");
}
var config = nodeConfigs[id];
var config;
if (nodeTypeToId[id]) {
config = nodeConfigs[nodeTypeToId[id]];
} else {
config = nodeConfigs[id];
}
if (config) {
delete config.err;
config.enabled = true;
@ -263,7 +296,12 @@ var registry = (function() {
if (!settings.available()) {
throw new Error("Settings unavailable");
}
var config = nodeConfigs[id];
var config;
if (nodeTypeToId[id]) {
config = nodeConfigs[nodeTypeToId[id]];
} else {
config = nodeConfigs[id];
}
if (config) {
// TODO: persist setting
config.enabled = false;
@ -291,7 +329,7 @@ var registry = (function() {
saveNodeList();
}
}
}
};
})();
@ -330,7 +368,7 @@ 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) {
result.push(path.join(dir,fn));
@ -467,13 +505,13 @@ function loadNodeConfig(file,module,name) {
template: file.replace(/\.js$/,".html"),
enabled: isEnabled,
loaded:false
}
};
if (module) {
node.name = module+":"+name;
node.module = module;
} else {
node.name = path.basename(file)
node.name = path.basename(file);
}
try {
var content = fs.readFileSync(node.template,'utf8');
@ -679,7 +717,9 @@ module.exports = {
get: registry.getNodeConstructor,
getNodeInfo: registry.getNodeInfo,
getNodeModuleInfo: registry.getModuleInfo,
getPluginInfo: registry.getPluginInfo,
getNodeList: registry.getNodeList,
getPluginList: registry.getPluginList,
getNodeConfigs: registry.getAllNodeConfigs,
getNodeConfig: registry.getNodeConfig,
addNode: addNode,
@ -690,4 +730,4 @@ module.exports = {
addModule: addModule,
removeModule: registry.removeModule,
cleanNodeList: registry.cleanNodeList
}
};

View File

@ -0,0 +1,77 @@
/**
* 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 should = require("should");
var request = require('supertest');
var express = require('express');
var sinon = require('sinon');
var when = require('when');
var app = express();
var redNodes = require("../../../red/nodes");
var server = require("../../../red/server");
var settings = require("../../../red/settings");
var plugins = require("../../../red/api/plugins");
describe("plugins api", function() {
var app;
before(function() {
app = express();
app.use(express.json());
app.get("/plugins",plugins.getAll);
app.get("/plugins/:id",plugins.get);
});
describe('get plugins', function() {
it('returns plugins list', function(done) {
var getPluginList = sinon.stub(redNodes,'getPluginList', function() {
return [1,2,3];
});
request(app)
.get('/plugins')
.expect(200)
.end(function(err,res) {
getPluginList.restore();
if (err) {
throw err;
}
res.body.should.be.an.Array.and.have.lengthOf(3);
done();
});
});
it('returns an individual plugin info', function(done) {
var getPluginInfo = sinon.stub(redNodes,'getPluginInfo', function(id) {
return {"name":"123", "nodes":[1,2,3]};
});
request(app)
.get('/plugins/123')
.expect(200)
.end(function(err,res) {
getPluginInfo.restore();
if (err) {
throw err;
}
res.body.should.have.property("name","123");
res.body.should.have.property("nodes",[1,2,3]);
done();
});
});
});
});

View File

@ -405,6 +405,108 @@ describe('NodeRegistry', function() {
});
it('returns plugins list', function(done) {
var fs = require("fs");
var path = require("path");
var pathJoin = (function() {
var _join = path.join;
return sinon.stub(path,"join",function() {
if (arguments.length == 3 && arguments[2] == "package.json") {
return _join(resourcesDir,"TestNodeModule" + path.sep + "node_modules" + path.sep,arguments[1],arguments[2]);
}
if (arguments.length == 2 && arguments[1] == "TestNodeModule") {
return _join(resourcesDir,"TestNodeModule" + path.sep + "node_modules" + path.sep,arguments[1]);
}
return _join.apply(this,arguments);
});
})();
var readdirSync = (function() {
var originalReaddirSync = fs.readdirSync;
var callCount = 0;
return sinon.stub(fs,"readdirSync",function(dir) {
var result = [];
if (callCount == 1) {
result = originalReaddirSync(resourcesDir + "TestNodeModule" + path.sep + "node_modules");
}
callCount++;
return result;
});
})();
typeRegistry.init(settingsWithStorage);
typeRegistry.load("wontexist",true).then(function(){
typeRegistry.addModule("TestNodeModule").then(function() {
var list = typeRegistry.getPluginList();
list.should.be.an.Array.and.have.lengthOf(1);
list[0].should.have.property("name", "TestNodeModule");
list[0].should.have.property("nodes");
list[0].nodes.should.be.an.Array.and.have.lengthOf(2);
done();
}).catch(function(e) {
done(e);
});
}).catch(function(e) {
done(e);
}).finally(function() {
readdirSync.restore();
pathJoin.restore();
});
});
it('returns plugin info', function(done) {
var fs = require("fs");
var path = require("path");
var pathJoin = (function() {
var _join = path.join;
return sinon.stub(path,"join",function() {
if (arguments.length == 3 && arguments[2] == "package.json") {
return _join(resourcesDir,"TestNodeModule" + path.sep + "node_modules" + path.sep,arguments[1],arguments[2]);
}
if (arguments.length == 2 && arguments[1] == "TestNodeModule") {
return _join(resourcesDir,"TestNodeModule" + path.sep + "node_modules" + path.sep,arguments[1]);
}
return _join.apply(this,arguments);
});
})();
var readdirSync = (function() {
var originalReaddirSync = fs.readdirSync;
var callCount = 0;
return sinon.stub(fs,"readdirSync",function(dir) {
var result = [];
if (callCount == 1) {
result = originalReaddirSync(resourcesDir + "TestNodeModule" + path.sep + "node_modules");
}
callCount++;
return result;
});
})();
typeRegistry.init(settingsWithStorage);
typeRegistry.load("wontexist",true).then(function(){
typeRegistry.addModule("TestNodeModule").then(function(nodes) {
var list = typeRegistry.getPluginList();
var plugin = typeRegistry.getPluginInfo(list[0].name);
plugin.should.have.property("name", list[0].name);
plugin.should.have.property("nodes", nodes);
done();
}).catch(function(e) {
done(e);
});
}).catch(function(e) {
done(e);
}).finally(function() {
readdirSync.restore();
pathJoin.restore();
});
});
it('rejects adding duplicate nodes', function(done) {
typeRegistry.init(settingsWithStorage);
@ -745,7 +847,7 @@ describe('NodeRegistry', function() {
});
it('allows nodes to be enabled and disabled', function(done) {
it('allows nodes to be enabled and disabled by hex-id', function(done) {
typeRegistry.init(settingsWithStorage);
typeRegistry.load(resourcesDir+path.sep+"TestNode1",true).then(function() {
var list = typeRegistry.getNodeList();
@ -784,6 +886,49 @@ describe('NodeRegistry', function() {
});
});
it('allows nodes to be enabled and disabled by node-type', function(done) {
typeRegistry.init(settingsWithStorage);
typeRegistry.load(resourcesDir+path.sep+"TestNode1",true).then(function() {
var list = typeRegistry.getNodeList();
list.should.be.an.Array.and.have.lengthOf(1);
list[0].should.have.property("id");
list[0].should.have.property("name","TestNode1.js");
list[0].should.have.property("types",["test-node-1"]);
list[0].should.have.property("enabled",true);
var nodeConfig = typeRegistry.getNodeConfigs();
nodeConfig.length.should.be.greaterThan(0);
var info = typeRegistry.disableNode(list[0].types[0]);
info.should.have.property("id",list[0].id);
info.should.have.property("types",list[0].types);
info.should.have.property("enabled",false);
var list2 = typeRegistry.getNodeList();
list2.should.be.an.Array.and.have.lengthOf(1);
list2[0].should.have.property("enabled",false);
typeRegistry.getNodeConfigs().length.should.equal(0);
var info2 = typeRegistry.enableNode(list[0].types[0]);
info2.should.have.property("id",list[0].id);
info2.should.have.property("types",list[0].types);
info2.should.have.property("enabled",true);
var list3 = typeRegistry.getNodeList();
list3.should.be.an.Array.and.have.lengthOf(1);
list3[0].should.have.property("enabled",true);
var nodeConfig2 = typeRegistry.getNodeConfigs();
nodeConfig2.should.eql(nodeConfig);
done();
}).catch(function(e) {
done(e);
});
});
it('fails to enable/disable non-existent nodes', function(done) {
typeRegistry.init(settings);
typeRegistry.load("wontexist",true).then(function() {