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,12 +28,12 @@ var settings = require("../settings");
var errorHandler = function(err,req,res,next) {
//TODO: standardize json response
res.send(400,err.toString());
}
};
function init(adminApp) {
adminApp.use(express.json());
library.init(adminApp);
// Editor
@ -42,11 +43,11 @@ function init(adminApp) {
adminApp.get("/settings",ui.settings);
adminApp.use("/",ui.editor);
}
// Flows
adminApp.get("/flows",flows.get);
adminApp.post("/flows",flows.post);
// Nodes
adminApp.get("/nodes",nodes.getAll);
adminApp.post("/nodes",nodes.post);
@ -54,17 +55,21 @@ function init(adminApp) {
adminApp.get("/nodes/:id",nodes.get);
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);
adminApp.get(new RegExp("/library/flows\/(.*)"),library.get);
// Error Handler
adminApp.use(errorHandler);
}
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

@ -28,14 +28,14 @@ function registerType(type,constructor,opts) {
if (opts && opts.credentials) {
credentials.register(type,opts.credentials);
}
registry.registerType(type,constructor);
registry.registerType(type,constructor);
}
/**
* Called from a Node's constructor function, invokes the super-class
* constructor and attaches any credentials to the node.
* @param node the node object being created
* @param def the instance definition for the node
* @param def the instance definition for the node
*/
function createNode(node,def) {
Node.call(node,def);
@ -95,40 +95,42 @@ module.exports = {
// Lifecycle
init: init,
load: registry.load,
// Node registry
createNode: createNode,
getNode: flows.get,
addNode: registry.addNode,
removeNode: removeNode,
addModule: registry.addModule,
removeModule: removeModule,
enableNode: registry.enableNode,
disableNode: disableNode,
// Node type registry
registerType: registerType,
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,
cleanNodeList: registry.cleanNodeList,
// Flow handling
loadFlows: flows.load,
stopFlows: flows.stopFlows,
setFlows: flows.setFlows,
getFlows: flows.getFlows,
// Credentials
addCredentials: credentials.add,
getCredentials: credentials.get,
deleteCredentials: credentials.delete
}
};

View File

@ -13,13 +13,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
**/
var util = require("util");
var when = require("when");
var whenNode = require('when/node');
var fs = require("fs");
var path = require("path");
var crypto = require("crypto");
var crypto = require("crypto");
var UglifyJS = require("uglify-js");
var events = require("../events");
@ -33,7 +33,7 @@ function filterNodeInfo(n) {
name: n.name,
types: n.types,
enabled: n.enabled
}
};
if (n.hasOwnProperty("loaded")) {
r.loaded = n.loaded;
}
@ -53,10 +53,10 @@ var registry = (function() {
var nodeConstructors = {};
var nodeTypeToId = {};
var nodeModules = {};
function saveNodeList() {
var nodeList = {};
for (var i in nodeConfigs) {
if (nodeConfigs.hasOwnProperty(i)) {
var nodeConfig = nodeConfigs[i];
@ -75,7 +75,7 @@ var registry = (function() {
return when.reject("Settings unavailable");
}
}
return {
init: function() {
if (settings.available()) {
@ -95,19 +95,19 @@ var registry = (function() {
nodeList = [];
nodeConfigCache = null;
},
addNodeSet: function(id,set) {
if (!set.err) {
set.types.forEach(function(t) {
nodeTypeToId[t] = id;
});
}
if (set.module) {
nodeModules[set.module] = nodeModules[set.module]||{nodes:[]};
nodeModules[set.module].nodes.push(id);
}
nodeConfigs[id] = set;
nodeList.push(id);
nodeConfigCache = null;
@ -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");
@ -175,8 +203,8 @@ var registry = (function() {
nodeConstructors[type] = constructor;
events.emit("type-registered",type);
},
/**
* Gets all of the node template configs
* @return all of the node templates in a single string
@ -201,7 +229,7 @@ var registry = (function() {
}
return nodeConfigCache;
},
getNodeConfig: function(id) {
var config = nodeConfigs[id];
if (config) {
@ -214,7 +242,7 @@ var registry = (function() {
return null;
}
},
getNodeConstructor: function(type) {
var config = nodeConfigs[nodeTypeToId[type]];
if (!config || (config.enabled && !config.err)) {
@ -222,7 +250,7 @@ var registry = (function() {
}
return null;
},
clear: function() {
nodeConfigCache = null;
nodeConfigs = {};
@ -230,20 +258,25 @@ var registry = (function() {
nodeConstructors = {};
nodeTypeToId = {};
},
getTypeId: function(type) {
return nodeTypeToId[type];
},
getModuleInfo: function(type) {
return nodeModules[type];
},
enableNodeSet: function(id) {
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;
@ -258,12 +291,17 @@ var registry = (function() {
}
return filterNodeInfo(config);
},
disableNodeSet: function(id) {
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;
@ -274,9 +312,9 @@ var registry = (function() {
}
return filterNodeInfo(config);
},
saveNodeList: saveNodeList,
cleanNodeList: function() {
var removed = false;
for (var id in nodeConfigs) {
@ -291,7 +329,7 @@ var registry = (function() {
saveNodeList();
}
}
}
};
})();
@ -330,14 +368,14 @@ 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));
}
}
} else if (stats.isDirectory()) {
// Ignore /.dirs/, /lib/ /node_modules/
// Ignore /.dirs/, /lib/ /node_modules/
if (!/^(\..*|lib|icons|node_modules|test)$/.test(fn)) {
result = result.concat(getNodeFiles(path.join(dir,fn)));
} else if (fn === "icons") {
@ -385,7 +423,7 @@ function scanTreeForNodesModules(moduleName) {
}
} catch(err) {
}
dir = up;
up = path.resolve(path.join(dir,".."));
}
@ -451,7 +489,7 @@ function loadNodeConfig(file,module,name) {
}
var info = registry.getNodeInfo(id);
var isEnabled = true;
if (info) {
@ -460,38 +498,38 @@ function loadNodeConfig(file,module,name) {
}
isEnabled = info.enabled;
}
var node = {
id: id,
file: file,
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');
var types = [];
var regExp = /<script ([^>]*)data-template-name=['"]([^'"]*)['"]/gi;
var match = null;
while((match = regExp.exec(content)) !== null) {
types.push(match[2]);
}
node.types = types;
node.config = content;
// TODO: parse out the javascript portion of the template
node.script = "";
for (var i=0;i<node.types.length;i++) {
if (registry.getTypeId(node.types[i])) {
node.err = node.types[i]+" already registered";
@ -525,7 +563,7 @@ function load(defaultNodesDir,disableNodePathScan) {
} else {
nodeFiles = getNodeFiles(__dirname+"/../../nodes");
}
if (settings.nodesDir) {
var dir = settings.nodesDir;
if (typeof settings.nodesDir == "string") {
@ -540,10 +578,10 @@ function load(defaultNodesDir,disableNodePathScan) {
try {
nodes.push(loadNodeConfig(file));
} catch(err) {
//
//
}
});
// TODO: disabling npm module loading if defaultNodesDir set
// This indicates a test is being run - don't want to pick up
// unexpected nodes.
@ -561,18 +599,18 @@ function load(defaultNodesDir,disableNodePathScan) {
promises.push(loadNodeModule(node));
}
});
//resolve([]);
when.settle(promises).then(function(results) {
// Trigger a load of the configs to get it precached
registry.getAllNodeConfigs();
if (settings.available()) {
resolve(registry.saveNodeList());
} else {
resolve();
}
});
});
});
}
@ -582,7 +620,7 @@ function load(defaultNodesDir,disableNodePathScan) {
* @return a promise that resolves to an update node info object. The object
* has the following properties added:
* err: any error encountered whilst loading the node
*
*
*/
function loadNodeModule(node) {
var nodeDir = path.dirname(node.file);
@ -627,7 +665,7 @@ function loadNodeList(nodes) {
promises.push(node);
}
});
return when.settle(promises).then(function(results) {
return registry.saveNodeList().then(function() {
var list = results.map(function(r) {
@ -643,7 +681,7 @@ function addNode(file) {
throw new Error("Settings unavailable");
}
var nodes = [];
try {
try {
nodes.push(loadNodeConfig(file));
} catch(err) {
return when.reject(err);
@ -679,15 +717,17 @@ 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,
removeNode: registry.removeNode,
enableNode: registry.enableNodeSet,
disableNode: registry.disableNodeSet,
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

@ -29,9 +29,9 @@ afterEach(function() {
});
describe('NodeRegistry', function() {
var resourcesDir = __dirname+ path.sep + "resources" + path.sep;
function stubSettings(s,available) {
s.available = function() {return available;}
s.set = function(s,v) { return when.resolve()},
@ -40,17 +40,17 @@ describe('NodeRegistry', function() {
}
var settings = stubSettings({},false);
var settingsWithStorage = stubSettings({},true);
it('automatically registers new nodes',function() {
var testNode = RedNodes.getNode('123');
should.not.exist(n);
var n = new RedNode({id:'123',type:'abc'});
var newNode = RedNodes.getNode('123');
should.strictEqual(n,newNode);
});
it('handles nodes that export a function', function(done) {
typeRegistry.init(settings);
typeRegistry.load(resourcesDir + "TestNode1",true).then(function() {
@ -61,18 +61,18 @@ describe('NodeRegistry', function() {
list[0].should.have.property("types",["test-node-1"]);
list[0].should.have.property("enabled",true);
list[0].should.not.have.property("err");
var nodeConstructor = typeRegistry.get("test-node-1");
nodeConstructor.should.be.type("function");
done();
}).catch(function(e) {
done(e);
});
});
it('handles nodes that export a function returning a resolving promise', function(done) {
typeRegistry.init(settings);
typeRegistry.load(resourcesDir + "TestNode2",true).then(function() {
@ -85,14 +85,14 @@ describe('NodeRegistry', function() {
list[0].should.not.have.property("err");
var nodeConstructor = typeRegistry.get("test-node-2");
nodeConstructor.should.be.type("function");
done();
}).catch(function(e) {
done(e);
});
});
it('handles nodes that export a function returning a rejecting promise', function(done) {
typeRegistry.init(settings);
typeRegistry.load(resourcesDir + "TestNode3",true).then(function() {
@ -106,14 +106,14 @@ describe('NodeRegistry', function() {
var nodeConstructor = typeRegistry.get("test-node-3");
(nodeConstructor === null).should.be.true;
done();
}).catch(function(e) {
done(e);
});
});
it('handles files containing multiple nodes', function(done) {
typeRegistry.init(settings);
typeRegistry.load(resourcesDir + "MultipleNodes1",true).then(function() {
@ -124,19 +124,19 @@ describe('NodeRegistry', function() {
list[0].should.have.property("types",["test-node-multiple-1a","test-node-multiple-1b"]);
list[0].should.have.property("enabled",true);
list[0].should.not.have.property("err");
var nodeConstructor = typeRegistry.get("test-node-multiple-1a");
nodeConstructor.should.be.type("function");
nodeConstructor = typeRegistry.get("test-node-multiple-1b");
nodeConstructor.should.be.type("function");
done();
}).catch(function(e) {
done(e);
});
});
it('handles nested directories', function(done) {
typeRegistry.init(settings);
typeRegistry.load(resourcesDir + "NestedDirectoryNode",true).then(function() {
@ -152,7 +152,7 @@ describe('NodeRegistry', function() {
done(e);
});
});
it('emits type-registered and node-icon-dir events', function(done) {
var eventEmitSpy = sinon.spy(events,"emit");
typeRegistry.init(settings);
@ -163,16 +163,16 @@ describe('NodeRegistry', function() {
list[0].should.have.property("types",["nested-node-1"]);
list[0].should.have.property("enabled",true);
list[0].should.not.have.property("err");
eventEmitSpy.callCount.should.equal(2);
eventEmitSpy.firstCall.args[0].should.be.equal("node-icon-dir");
eventEmitSpy.firstCall.args[1].should.be.equal(
resourcesDir + "NestedDirectoryNode" + path.sep + "NestedNode" + path.sep + "icons");
eventEmitSpy.secondCall.args[0].should.be.equal("type-registered");
eventEmitSpy.secondCall.args[1].should.be.equal("nested-node-1");
done();
}).catch(function(e) {
done(e);
@ -180,25 +180,25 @@ describe('NodeRegistry', function() {
eventEmitSpy.restore();
});
});
it('rejects a duplicate node type registration', function(done) {
typeRegistry.init(stubSettings({
nodesDir:[resourcesDir + "TestNode1",resourcesDir + "DuplicateTestNode"]
},false));
typeRegistry.load("wontexist",true).then(function() {
var list = typeRegistry.getNodeList();
list.should.be.an.Array.and.have.lengthOf(2);
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);
list[0].should.not.have.property("err");
list[1].should.have.property("id");
list[1].id.should.not.equal(list[0].id);
list[1].should.have.property("name","TestNode1.js");
list[1].should.have.property("types",["test-node-1"]);
list[1].should.have.property("enabled",true);
@ -208,13 +208,13 @@ describe('NodeRegistry', function() {
var nodeConstructor = typeRegistry.get("test-node-1");
// Verify the duplicate node hasn't replaced the original one
nodeConstructor.name.should.be.equal("TestNode");
done();
}).catch(function(e) {
done(e);
});
});
it('handles nodesDir as a string', function(done) {
typeRegistry.init(stubSettings({
@ -228,9 +228,9 @@ describe('NodeRegistry', function() {
}).catch(function(e) {
done("Loading of non-existing nodesDir should succeed");
});
});
it('handles invalid nodesDir',function(done) {
typeRegistry.init(stubSettings({
@ -244,7 +244,7 @@ describe('NodeRegistry', function() {
done("Loading of non-existing nodesDir should succeed");
});
});
it('returns nothing for an unregistered type config', function() {
typeRegistry.init(settings);
typeRegistry.load("wontexist",true).then(function(){
@ -254,7 +254,7 @@ describe('NodeRegistry', function() {
done(e);
});
});
it('excludes node files listed in nodesExcludes',function(done) {
typeRegistry.init(stubSettings({
nodesExcludes: [ "TestNode1.js" ],
@ -269,19 +269,19 @@ describe('NodeRegistry', function() {
done(e);
});
});
it('returns the node configurations', function(done) {
typeRegistry.init(stubSettings({
nodesDir:[resourcesDir + "TestNode1",resourcesDir + "TestNode2"]
},false));
typeRegistry.load("wontexist",true).then(function() {
var list = typeRegistry.getNodeList();
var nodeConfigs = typeRegistry.getNodeConfigs();
// TODO: this is brittle...
nodeConfigs.should.equal("<script type=\"text/x-red\" data-template-name=\"test-node-1\"></script>\n<script type=\"text/x-red\" data-help-name=\"test-node-1\"></script>\n<script type=\"text/javascript\">RED.nodes.registerType('test-node-1',{});</script>\n<style></style>\n<p>this should be filtered out</p>\n<script type=\"text/x-red\" data-template-name=\"test-node-2\"></script>\n<script type=\"text/x-red\" data-help-name=\"test-node-2\"></script>\n<script type=\"text/javascript\">RED.nodes.registerType('test-node-2',{});</script>\n<style></style>\n");
var nodeId = list[0].id;
var nodeConfig = typeRegistry.getNodeConfig(nodeId);
nodeConfig.should.equal("<script type=\"text/x-red\" data-template-name=\"test-node-1\"></script>\n<script type=\"text/x-red\" data-help-name=\"test-node-1\"></script>\n<script type=\"text/javascript\">RED.nodes.registerType('test-node-1',{});</script>\n<style></style>\n<p>this should be filtered out</p>\n");
@ -290,7 +290,7 @@ describe('NodeRegistry', function() {
done(e);
});
});
it('stores the node list', function(done) {
var settings = {
nodesDir:[resourcesDir + "TestNode1",resourcesDir + "TestNode2",resourcesDir + "TestNode3"],
@ -303,39 +303,39 @@ describe('NodeRegistry', function() {
typeRegistry.load("wontexist",true).then(function() {
var list = typeRegistry.getNodeList();
list.should.be.Array.and.have.length(3);
settingsSave.callCount.should.equal(1);
settingsSave.firstCall.args[0].should.be.equal("nodes");
var savedList = settingsSave.firstCall.args[1];
savedList[list[0].id].name == list[0].name;
savedList[list[1].id].name == list[1].name;
savedList[list[2].id].name == list[2].name;
savedList[list[0].id].should.not.have.property("err");
savedList[list[1].id].should.not.have.property("err");
savedList[list[2].id].should.not.have.property("err");
done();
}).catch(function(e) {
done(e);
}).finally(function() {
settingsSave.restore();
});
});
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;
typeRegistry.addNode(resourcesDir + "TestNode1/TestNode1.js").then(function(node) {
list = typeRegistry.getNodeList();
list[0].should.have.property("id");
@ -343,20 +343,20 @@ describe('NodeRegistry', function() {
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(){
@ -371,47 +371,149 @@ describe('NodeRegistry', function() {
}).otherwise(function(e) {
done(e);
});
}).catch(function(e) {
done(e);
});
});
it('returns node info by type or id', function(done) {
typeRegistry.init(settings);
typeRegistry.load(resourcesDir + "TestNode1",true).then(function() {
var list = typeRegistry.getNodeList();
list.should.be.an.Array.and.have.lengthOf(1);
var id = list[0].id;
var type = list[0].types[0];
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);
list[0].should.not.have.property("err");
var info = typeRegistry.getNodeInfo(id);
list[0].should.eql(info);
var info2 = typeRegistry.getNodeInfo(type);
list[0].should.eql(info2);
done();
}).catch(function(e) {
done(e);
});
});
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);
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) {
@ -419,12 +521,12 @@ describe('NodeRegistry', function() {
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() {
@ -437,33 +539,33 @@ describe('NodeRegistry', function() {
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");
@ -474,11 +576,11 @@ describe('NodeRegistry', function() {
done(e);
});
});
it('scans the node_modules path for node files', function(done) {
var fs = require("fs");
var path = require("path");
var eventEmitSpy = sinon.spy(events,"emit");
var pathJoin = (function() {
var _join = path.join;
@ -492,7 +594,7 @@ describe('NodeRegistry', function() {
return _join.apply(this,arguments);
});
})();
var readdirSync = (function() {
var originalReaddirSync = fs.readdirSync;
var callCount = 0;
@ -505,7 +607,7 @@ describe('NodeRegistry', function() {
return result;
});
})();
typeRegistry.init(settings);
typeRegistry.load("wontexist",false).then(function(){
var list = typeRegistry.getNodeList();
@ -521,17 +623,17 @@ describe('NodeRegistry', function() {
list[1].should.have.property("types",["test-node-mod-2"]);
list[1].should.have.property("enabled",true);
list[1].should.have.property("err");
eventEmitSpy.callCount.should.equal(2);
eventEmitSpy.firstCall.args[0].should.be.equal("node-icon-dir");
eventEmitSpy.firstCall.args[1].should.be.equal(
resourcesDir + "TestNodeModule" + path.sep+ "node_modules" + path.sep + "TestNodeModule" + path.sep + "icons");
eventEmitSpy.secondCall.args[0].should.be.equal("type-registered");
eventEmitSpy.secondCall.args[1].should.be.equal("test-node-mod-1");
done();
}).catch(function(e) {
done(e);
@ -541,7 +643,7 @@ describe('NodeRegistry', function() {
eventEmitSpy.restore();
});
});
it('allows nodes to be added by module name', function(done) {
var fs = require("fs");
var path = require("path");
@ -558,7 +660,7 @@ describe('NodeRegistry', function() {
return _join.apply(this,arguments);
});
})();
var readdirSync = (function() {
var originalReaddirSync = fs.readdirSync;
var callCount = 0;
@ -575,7 +677,7 @@ describe('NodeRegistry', function() {
typeRegistry.load("wontexist",true).then(function(){
var list = typeRegistry.getNodeList();
list.should.be.an.Array.and.be.empty;
typeRegistry.addModule("TestNodeModule").then(function(node) {
list = typeRegistry.getNodeList();
list.should.be.an.Array.and.have.lengthOf(2);
@ -590,14 +692,14 @@ describe('NodeRegistry', function() {
list[1].should.have.property("types",["test-node-mod-2"]);
list[1].should.have.property("enabled",true);
list[1].should.have.property("err");
node.should.eql(list);
done();
}).catch(function(e) {
done(e);
});
}).catch(function(e) {
done(e);
}).finally(function() {
@ -605,8 +707,8 @@ describe('NodeRegistry', function() {
pathJoin.restore();
});
});
it('rejects adding duplicate node modules', function(done) {
var fs = require("fs");
var path = require("path");
@ -623,7 +725,7 @@ describe('NodeRegistry', function() {
return _join.apply(this,arguments);
});
})();
var readdirSync = (function() {
var originalReaddirSync = fs.readdirSync;
var callCount = 0;
@ -653,26 +755,26 @@ describe('NodeRegistry', function() {
pathJoin.restore();
});
});
it('fails to add non-existent module name', function(done) {
typeRegistry.init(settingsWithStorage);
typeRegistry.load("wontexist",true).then(function(){
var list = typeRegistry.getNodeList();
list.should.be.an.Array.and.be.empty;
typeRegistry.addModule("DoesNotExistModule").then(function(node) {
done(new Error("ENOENT not thrown"));
}).otherwise(function(e) {
e.code.should.eql("MODULE_NOT_FOUND");
done();
});
}).catch(function(e) {
done(e);
});
});
it('removes nodes from the registry by module', function(done) {
var fs = require("fs");
var path = require("path");
@ -689,7 +791,7 @@ describe('NodeRegistry', function() {
return _join.apply(this,arguments);
});
})();
var readdirSync = (function() {
var originalReaddirSync = fs.readdirSync;
var callCount = 0;
@ -708,14 +810,14 @@ describe('NodeRegistry', function() {
var list = typeRegistry.getNodeList();
list.should.be.an.Array.and.have.lengthOf(2);
var res = typeRegistry.removeModule("TestNodeModule");
res.should.be.an.Array.and.have.lengthOf(2);
res[0].should.have.a.property("id",list[0].id);
res[1].should.have.a.property("id",list[1].id);
list = typeRegistry.getNodeList();
list.should.be.an.Array.and.be.empty;
done();
}).catch(function(e) {
done(e);
@ -723,29 +825,29 @@ describe('NodeRegistry', function() {
readdirSync.restore();
pathJoin.restore();
});
});
it('fails to remove non-existent module name', 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.removeModule("DoesNotExistModule");
}).should.throw();
done();
}).catch(function(e) {
done(e);
});
});
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();
@ -753,28 +855,28 @@ describe('NodeRegistry', function() {
list[0].should.have.property("id");
list[0].should.have.property("name","TestNode1.js");
list[0].should.have.property("enabled",true);
var nodeConfig = typeRegistry.getNodeConfigs();
nodeConfig.length.should.be.greaterThan(0);
var info = typeRegistry.disableNode(list[0].id);
info.should.have.property("id",list[0].id);
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].id);
info2.should.have.property("id",list[0].id);
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);
@ -783,23 +885,66 @@ describe('NodeRegistry', function() {
done(e);
});
});
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() {
var list = typeRegistry.getNodeList();
list.should.be.an.Array.and.be.empty;
/*jshint immed: false */
(function() {
typeRegistry.disableNode("123");
}).should.throw();
/*jshint immed: false */
(function() {
typeRegistry.enableNode("123");
}).should.throw();
done();
}).catch(function(e) {
done(e);