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

@ -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));
});
} }
}); });
}); });

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");