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

Added version number to modules

This commit is contained in:
Anna Thomas 2014-11-21 16:35:29 +00:00
parent a5064b3ab6
commit d7f249eac4
3 changed files with 74 additions and 18 deletions

View File

@ -95,7 +95,7 @@ var registry = (function() {
nodeList = []; nodeList = [];
nodeConfigCache = null; nodeConfigCache = null;
}, },
addNodeSet: function(id,set) { addNodeSet: function(id,set,version) {
if (!set.err) { if (!set.err) {
set.types.forEach(function(t) { set.types.forEach(function(t) {
nodeTypeToId[t] = id; nodeTypeToId[t] = id;
@ -105,6 +105,10 @@ var registry = (function() {
nodeModules[set.module] = nodeModules[set.module]||{nodes:[]}; nodeModules[set.module] = nodeModules[set.module]||{nodes:[]};
nodeModules[set.module].nodes.push(set.name); nodeModules[set.module].nodes.push(set.name);
if (version) {
nodeModules[set.module].version = version.replace(/(\r\n|\n|\r)/gm,"");
}
nodeConfigs[id] = set; nodeConfigs[id] = set;
nodeList.push(id); nodeList.push(id);
nodeConfigCache = null; nodeConfigCache = null;
@ -181,10 +185,11 @@ var registry = (function() {
}, },
getModuleInfo: function(module) { getModuleInfo: function(module) {
if (nodeModules[module]) { if (nodeModules[module]) {
console.log(nodeModules[module]);
var nodes = nodeModules[module].nodes; var nodes = nodeModules[module].nodes;
var m = { var m = {
name: module, name: module,
version: "TODO", version: nodeModules[module].version,
nodes: [] nodes: []
}; };
for (var i = 0; i < nodes.length; ++i) { for (var i = 0; i < nodes.length; ++i) {
@ -438,7 +443,7 @@ function scanTreeForNodesModules(moduleName) {
* @param moduleDir the root directory of the package * @param moduleDir the root directory of the package
* @param pkg the module's package.json object * @param pkg the module's package.json object
*/ */
function loadNodesFromModule(moduleDir,pkg) { function loadNodesFromModule(moduleDir,pkg,version) {
var nodes = pkg['node-red'].nodes||{}; var nodes = pkg['node-red'].nodes||{};
var results = []; var results = [];
var iconDirs = []; var iconDirs = [];
@ -446,7 +451,7 @@ function loadNodesFromModule(moduleDir,pkg) {
if (nodes.hasOwnProperty(n)) { if (nodes.hasOwnProperty(n)) {
var file = path.join(moduleDir,nodes[n]); var file = path.join(moduleDir,nodes[n]);
try { try {
results.push(loadNodeConfig(file,pkg.name,n)); results.push(loadNodeConfig(file,pkg.name,n,version));
} catch(err) { } catch(err) {
} }
var iconDir = path.join(moduleDir,path.dirname(nodes[n]),"icons"); var iconDir = path.join(moduleDir,path.dirname(nodes[n]),"icons");
@ -477,7 +482,7 @@ function loadNodesFromModule(moduleDir,pkg) {
* types: an array of node type names in this file * types: an array of node type names in this file
* } * }
*/ */
function loadNodeConfig(file,module,name) { function loadNodeConfig(file,module,name,version) {
var id = module + "/" + name; var id = module + "/" + name;
var info = registry.getNodeInfo(id); var info = registry.getNodeInfo(id);
@ -531,7 +536,7 @@ function loadNodeConfig(file,module,name) {
node.err = err.toString(); node.err = err.toString();
} }
} }
registry.addNodeSet(id,node); registry.addNodeSet(id,node,version);
return node; return node;
} }
@ -676,7 +681,7 @@ function addNode(file) {
return loadNodeList(nodes); return loadNodeList(nodes);
} }
function addModule(module) { function addModule(module,version) {
if (!settings.available()) { if (!settings.available()) {
throw new Error("Settings unavailable"); throw new Error("Settings unavailable");
} }
@ -691,7 +696,7 @@ function addModule(module) {
return when.reject(err); return when.reject(err);
} }
moduleFiles.forEach(function(moduleFile) { moduleFiles.forEach(function(moduleFile) {
nodes = nodes.concat(loadNodesFromModule(moduleFile.dir,moduleFile.package)); nodes = nodes.concat(loadNodesFromModule(moduleFile.dir,moduleFile.package,version));
}); });
return loadNodeList(nodes); return loadNodeList(nodes);
} }

View File

@ -33,10 +33,10 @@ function init(_server,_settings) {
settings = _settings; settings = _settings;
comms.init(_server,_settings); comms.init(_server,_settings);
nodeApp = express(); nodeApp = express();
app = express(); app = express();
if (settings.httpAdminRoot !== false) { if (settings.httpAdminRoot !== false) {
require("./api").init(app); require("./api").init(app);
} }
@ -44,7 +44,7 @@ function init(_server,_settings) {
function start() { function start() {
var defer = when.defer(); var defer = when.defer();
storage.init(settings).then(function() { storage.init(settings).then(function() {
settings.load(storage).then(function() { settings.load(storage).then(function() {
console.log("\nWelcome to Node-RED\n===================\n"); console.log("\nWelcome to Node-RED\n===================\n");
@ -95,7 +95,7 @@ function start() {
} }
} }
defer.resolve(); defer.resolve();
redNodes.loadFlows(); redNodes.loadFlows();
}).otherwise(function(err) { }).otherwise(function(err) {
console.log(err); console.log(err);
@ -105,7 +105,7 @@ function start() {
}).otherwise(function(err) { }).otherwise(function(err) {
defer.reject(err); defer.reject(err);
}); });
return defer.promise; return defer.promise;
} }
@ -138,7 +138,7 @@ function reportRemovedModules(removedNodes) {
return removedNodes; return removedNodes;
} }
function installModule(module) { function installModule(module) {
//TODO: ensure module is 'safe' //TODO: ensure module is 'safe'
return when.promise(function(resolve,reject) { return when.promise(function(resolve,reject) {
if (/[\s;]/.test(module)) { if (/[\s;]/.test(module)) {
@ -162,8 +162,10 @@ function installModule(module) {
reject(new Error("Install failed")); reject(new Error("Install failed"));
} }
} else { } else {
util.log("[red] Installed module: "+module); var grandchild = child_process.exec('npm view '+module+' version', function(err, stdin, stdout) {
resolve(redNodes.addModule(module).then(reportAddedModules)); util.log("[red] Installed module: "+module+":"+stdin);
resolve(redNodes.addModule(module,stdin).then(reportAddedModules));
});
} }
}); });
}); });
@ -200,11 +202,11 @@ function stop() {
comms.stop(); comms.stop();
} }
module.exports = { module.exports = {
init: init, init: init,
start: start, start: start,
stop: stop, stop: stop,
reportAddedModules: reportAddedModules, reportAddedModules: reportAddedModules,
reportRemovedModules: reportRemovedModules, reportRemovedModules: reportRemovedModules,
installModule: installModule, installModule: installModule,

View File

@ -710,6 +710,55 @@ describe('NodeRegistry', function() {
}); });
}); });
it('adds module with version number', 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","0.0.1").then(function(node) {
var module = typeRegistry.getModuleInfo("TestNodeModule");
module.should.have.property("name","TestNodeModule");
module.should.have.property("version","0.0.1");
done();
}).catch(function(e) {
done(e);
});
}).catch(function(e) {
done(e);
}).finally(function() {
readdirSync.restore();
pathJoin.restore();
});
});
it('rejects adding duplicate node modules', function(done) { it('rejects adding duplicate node modules', function(done) {
var fs = require("fs"); var fs = require("fs");