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

Allow node modules to declare supported versions of node-red

This commit is contained in:
Nick O'Leary 2015-07-10 21:42:14 +01:00
parent 999cf66b27
commit 846ab08661
4 changed files with 43 additions and 30 deletions

View File

@ -50,6 +50,7 @@
"passport-oauth2-client-password":"0.1.2", "passport-oauth2-client-password":"0.1.2",
"oauth2orize":"1.0.1", "oauth2orize":"1.0.1",
"i18next":"1.7.10", "i18next":"1.7.10",
"semver": "4.3.6",
"node-red-node-feedparser":"0.0.*", "node-red-node-feedparser":"0.0.*",
"node-red-node-email":"0.0.*", "node-red-node-email":"0.0.*",
"node-red-node-twitter":"0.0.*" "node-red-node-twitter":"0.0.*"

View File

@ -17,6 +17,7 @@
var when = require("when"); var when = require("when");
var fs = require("fs"); var fs = require("fs");
var path = require("path"); var path = require("path");
var semver = require("semver");
var events = require("../../events"); var events = require("../../events");
@ -35,7 +36,7 @@ events.on("node-locales-dir", function(info) {
function init(_settings) { function init(_settings) {
settings = _settings; settings = _settings;
localfilesystem.init(settings); localfilesystem.init(settings);
RED = require('../../red'); RED = require('../../red');
} }
@ -45,7 +46,7 @@ function load(defaultNodesDir,disableNodePathScan) {
// We should expose that as an option at some point, although the // We should expose that as an option at some point, although the
// performance gains are minimal. // performance gains are minimal.
//return loadNodeFiles(registry.getModuleList()); //return loadNodeFiles(registry.getModuleList());
var nodeFiles = localfilesystem.getNodeFiles(defaultNodesDir,disableNodePathScan); var nodeFiles = localfilesystem.getNodeFiles(defaultNodesDir,disableNodePathScan);
return loadNodeFiles(nodeFiles); return loadNodeFiles(nodeFiles);
} }
@ -55,6 +56,11 @@ function loadNodeFiles(nodeFiles) {
for (var module in nodeFiles) { for (var module in nodeFiles) {
/* istanbul ignore else */ /* istanbul ignore else */
if (nodeFiles.hasOwnProperty(module)) { if (nodeFiles.hasOwnProperty(module)) {
if (nodeFiles[module].redVersion &&
!semver.satisfies(RED.version().replace("-git",""), nodeFiles[module].redVersion)) {
//TODO: log it
continue;
}
if (module == "node-red" || !registry.getModuleInfo(module)) { if (module == "node-red" || !registry.getModuleInfo(module)) {
var first = true; var first = true;
for (var node in nodeFiles[module].nodes) { for (var node in nodeFiles[module].nodes) {
@ -72,7 +78,7 @@ function loadNodeFiles(nodeFiles) {
} }
} }
var moduleFn = parts.slice(0,i+2).join("/"); var moduleFn = parts.slice(0,i+2).join("/");
try { try {
var stat = fs.statSync(moduleFn); var stat = fs.statSync(moduleFn);
} catch(err) { } catch(err) {
@ -80,7 +86,7 @@ function loadNodeFiles(nodeFiles) {
break; break;
} }
} }
try { try {
promises.push(loadNodeConfig(nodeFiles[module].nodes[node])) promises.push(loadNodeConfig(nodeFiles[module].nodes[node]))
} catch(err) { } catch(err) {
@ -106,7 +112,7 @@ function loadNodeConfig(fileInfo) {
var module = fileInfo.module; var module = fileInfo.module;
var name = fileInfo.name; var name = fileInfo.name;
var version = fileInfo.version; var version = fileInfo.version;
var id = module + "/" + name; var id = module + "/" + name;
var info = registry.getNodeInfo(id); var info = registry.getNodeInfo(id);
var isEnabled = true; var isEnabled = true;
@ -116,7 +122,7 @@ function loadNodeConfig(fileInfo) {
} }
isEnabled = info.enabled; isEnabled = info.enabled;
} }
var node = { var node = {
id: id, id: id,
module: module, module: module,
@ -130,7 +136,7 @@ function loadNodeConfig(fileInfo) {
if (fileInfo.hasOwnProperty("types")) { if (fileInfo.hasOwnProperty("types")) {
node.types = fileInfo.types; node.types = fileInfo.types;
} }
fs.readFile(node.template,'utf8', function(err,content) { fs.readFile(node.template,'utf8', function(err,content) {
if (err) { if (err) {
node.types = []; node.types = [];
@ -144,17 +150,17 @@ function loadNodeConfig(fileInfo) {
node.err = err.toString(); node.err = err.toString();
} }
resolve(node); resolve(node);
} else { } else {
var types = []; var types = [];
var regExp = /<script ([^>]*)data-template-name=['"]([^'"]*)['"]/gi; var regExp = /<script ([^>]*)data-template-name=['"]([^'"]*)['"]/gi;
var match = null; var match = null;
while((match = regExp.exec(content)) !== null) { while((match = regExp.exec(content)) !== null) {
types.push(match[2]); types.push(match[2]);
} }
node.types = types; node.types = types;
var langRegExp = /^<script[^>]* data-lang=['"](.+?)['"]/i; var langRegExp = /^<script[^>]* data-lang=['"](.+?)['"]/i;
regExp = /(<script[^>]* data-help-name=[\s\S]*?<\/script>)/gi; regExp = /(<script[^>]* data-help-name=[\s\S]*?<\/script>)/gi;
match = null; match = null;
@ -165,7 +171,7 @@ function loadNodeConfig(fileInfo) {
mainContent += content.substring(index,regExp.lastIndex-match[1].length); mainContent += content.substring(index,regExp.lastIndex-match[1].length);
index = regExp.lastIndex; index = regExp.lastIndex;
var help = content.substring(regExp.lastIndex-match[1].length,regExp.lastIndex); var help = content.substring(regExp.lastIndex-match[1].length,regExp.lastIndex);
var lang = "en-US"; var lang = "en-US";
if ((match = langRegExp.exec(help)) !== null) { if ((match = langRegExp.exec(help)) !== null) {
lang = match[1]; lang = match[1];
@ -173,7 +179,7 @@ function loadNodeConfig(fileInfo) {
if (!helpContent.hasOwnProperty(lang)) { if (!helpContent.hasOwnProperty(lang)) {
helpContent[lang] = ""; helpContent[lang] = "";
} }
helpContent[lang] += help; helpContent[lang] += help;
} }
mainContent += content.substring(index); mainContent += content.substring(index);
@ -215,9 +221,9 @@ function loadNodeConfig(fileInfo) {
// credentials: RED.credentials, // credentials: RED.credentials,
// events: RED.events, // events: RED.events,
// log: RED.log, // log: RED.log,
// //
// } // }
// //
//} //}
@ -240,7 +246,7 @@ function loadNodeSet(node) {
var loadPromise = null; var loadPromise = null;
var r = require(node.file); var r = require(node.file);
if (typeof r === "function") { if (typeof r === "function") {
var red = {}; var red = {};
for (var i in RED) { for (var i in RED) {
if (RED.hasOwnProperty(i) && !/^(init|start|stop)$/.test(i)) { if (RED.hasOwnProperty(i) && !/^(init|start|stop)$/.test(i)) {
@ -334,7 +340,7 @@ function addFile(file) {
nodes: {} nodes: {}
}; };
fileObj[nodeFiles.module].nodes[nodeFiles.name] = nodeFiles; fileObj[nodeFiles.module].nodes[nodeFiles.name] = nodeFiles;
return loadNodeFiles(fileObj); return loadNodeFiles(fileObj);
} else { } else {
var e = new Error(); var e = new Error();

View File

@ -58,7 +58,7 @@ function getLocalFile(file) {
}; };
} }
return null; return null;
} }
@ -141,7 +141,7 @@ function scanTreeForNodesModules(moduleName) {
userDir = path.join(settings.userDir,"node_modules"); userDir = path.join(settings.userDir,"node_modules");
results = results.concat(scanDirForNodesModules(userDir,moduleName)); results = results.concat(scanDirForNodesModules(userDir,moduleName));
} }
var up = path.resolve(path.join(dir,"..")); var up = path.resolve(path.join(dir,".."));
while (up !== dir) { while (up !== dir) {
var pm = path.join(dir,"node_modules"); var pm = path.join(dir,"node_modules");
@ -155,14 +155,14 @@ function scanTreeForNodesModules(moduleName) {
} }
function getModuleNodeFiles(module) { function getModuleNodeFiles(module) {
var moduleDir = module.dir; var moduleDir = module.dir;
var pkg = module.package; var pkg = module.package;
var nodes = pkg['node-red'].nodes||{}; var nodes = pkg['node-red'].nodes||{};
var results = []; var results = [];
var iconDirs = []; var iconDirs = [];
for (var n in nodes) { for (var n in nodes) {
/* istanbul ignore else */ /* istanbul ignore else */
if (nodes.hasOwnProperty(n)) { if (nodes.hasOwnProperty(n)) {
@ -186,24 +186,24 @@ function getModuleNodeFiles(module) {
} }
function getNodeFiles(_defaultNodesDir,disableNodePathScan) { function getNodeFiles(_defaultNodesDir,disableNodePathScan) {
if (_defaultNodesDir) { if (_defaultNodesDir) {
defaultNodesDir = _defaultNodesDir; defaultNodesDir = _defaultNodesDir;
} }
var dir; var dir;
// Find all of the nodes to load // Find all of the nodes to load
//console.log(defaultNodesDir); //console.log(defaultNodesDir);
var nodeFiles = getLocalNodeFiles(path.resolve(defaultNodesDir)); var nodeFiles = getLocalNodeFiles(path.resolve(defaultNodesDir));
//console.log(nodeFiles); //console.log(nodeFiles);
var defaultLocalesPath = path.resolve(path.join(defaultNodesDir,"core","locales")); var defaultLocalesPath = path.resolve(path.join(defaultNodesDir,"core","locales"));
events.emit("node-locales-dir", { events.emit("node-locales-dir", {
namespace:"node-red", namespace:"node-red",
dir: defaultLocalesPath, dir: defaultLocalesPath,
file: "messages.json" file: "messages.json"
}); });
if (settings.userDir) { if (settings.userDir) {
dir = path.join(settings.userDir,"nodes"); dir = path.join(settings.userDir,"nodes");
nodeFiles = nodeFiles.concat(getLocalNodeFiles(dir)); nodeFiles = nodeFiles.concat(getLocalNodeFiles(dir));
@ -217,7 +217,7 @@ function getNodeFiles(_defaultNodesDir,disableNodePathScan) {
nodeFiles = nodeFiles.concat(getLocalNodeFiles(dir[i])); nodeFiles = nodeFiles.concat(getLocalNodeFiles(dir[i]));
} }
} }
var nodeList = { var nodeList = {
"node-red": { "node-red": {
name: "node-red", name: "node-red",
@ -228,7 +228,7 @@ function getNodeFiles(_defaultNodesDir,disableNodePathScan) {
nodeFiles.forEach(function(node) { nodeFiles.forEach(function(node) {
nodeList["node-red"].nodes[node.name] = node; nodeList["node-red"].nodes[node.name] = node;
}); });
if (!disableNodePathScan) { if (!disableNodePathScan) {
var moduleFiles = scanTreeForNodesModules(); var moduleFiles = scanTreeForNodesModules();
moduleFiles.forEach(function(moduleFile) { moduleFiles.forEach(function(moduleFile) {
@ -238,6 +238,9 @@ function getNodeFiles(_defaultNodesDir,disableNodePathScan) {
version: moduleFile.package.version, version: moduleFile.package.version,
nodes: {} nodes: {}
}; };
if (moduleFile.package['node-red'].version) {
nodeList[moduleFile.package.name].redVersion = moduleFile.package['node-red'].version;
}
nodeModuleFiles.forEach(function(node) { nodeModuleFiles.forEach(function(node) {
nodeList[moduleFile.package.name].nodes[node.name] = node; nodeList[moduleFile.package.name].nodes[node.name] = node;
}); });
@ -249,7 +252,7 @@ function getNodeFiles(_defaultNodesDir,disableNodePathScan) {
function getModuleFiles(module) { function getModuleFiles(module) {
var nodeList = {}; var nodeList = {};
var moduleFiles = scanTreeForNodesModules(module); var moduleFiles = scanTreeForNodesModules(module);
if (moduleFiles.length === 0) { if (moduleFiles.length === 0) {
var err = new Error(log._("nodes.registry.localfilesystem.module-not-found", {module:module})); var err = new Error(log._("nodes.registry.localfilesystem.module-not-found", {module:module}));
@ -264,6 +267,9 @@ function getModuleFiles(module) {
version: moduleFile.package.version, version: moduleFile.package.version,
nodes: {} nodes: {}
}; };
if (moduleFile.package['node-red'].version) {
nodeList[moduleFile.package.name].redVersion = moduleFile.package['node-red'].version;
}
nodeModuleFiles.forEach(function(node) { nodeModuleFiles.forEach(function(node) {
nodeList[moduleFile.package.name].nodes[node.name] = node; nodeList[moduleFile.package.name].nodes[node.name] = node;
}); });

View File

@ -67,7 +67,7 @@ var RED = {
var p = require(path.join(process.env.NODE_RED_HOME,"package.json")).version; var p = require(path.join(process.env.NODE_RED_HOME,"package.json")).version;
/* istanbul ignore else */ /* istanbul ignore else */
if (fs.existsSync(path.join(process.env.NODE_RED_HOME,".git"))) { if (fs.existsSync(path.join(process.env.NODE_RED_HOME,".git"))) {
p += ".git"; p += "-git";
} }
return p; return p;
}, },