2015-04-07 17:02:15 +02:00
|
|
|
/**
|
2017-01-11 16:24:33 +01:00
|
|
|
* Copyright JS Foundation and other contributors, http://js.foundation
|
2015-04-07 17:02:15 +02:00
|
|
|
*
|
|
|
|
* 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 when = require("when");
|
|
|
|
var fs = require("fs");
|
|
|
|
var path = require("path");
|
|
|
|
|
2015-11-17 22:12:43 +01:00
|
|
|
var events;
|
2018-04-23 15:24:51 +02:00
|
|
|
var log;
|
|
|
|
var i18n;
|
2015-04-07 17:02:15 +02:00
|
|
|
|
|
|
|
var settings;
|
|
|
|
var disableNodePathScan = false;
|
2017-11-30 14:13:35 +01:00
|
|
|
var iconFileExtensions = [".png", ".gif"];
|
2015-04-07 17:02:15 +02:00
|
|
|
|
2015-12-07 00:13:52 +01:00
|
|
|
function init(runtime) {
|
2015-11-17 22:12:43 +01:00
|
|
|
settings = runtime.settings;
|
|
|
|
events = runtime.events;
|
2018-04-23 15:24:51 +02:00
|
|
|
log = runtime.log;
|
|
|
|
i18n = runtime.i18n;
|
2015-04-07 17:02:15 +02:00
|
|
|
}
|
|
|
|
|
2017-03-08 11:00:00 +01:00
|
|
|
function isIncluded(name) {
|
|
|
|
if (settings.nodesIncludes) {
|
|
|
|
for (var i=0;i<settings.nodesIncludes.length;i++) {
|
|
|
|
if (settings.nodesIncludes[i] == name) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-06-09 00:17:45 +02:00
|
|
|
function isExcluded(name) {
|
|
|
|
if (settings.nodesExcludes) {
|
2015-05-27 15:11:11 +02:00
|
|
|
for (var i=0;i<settings.nodesExcludes.length;i++) {
|
2015-06-09 00:17:45 +02:00
|
|
|
if (settings.nodesExcludes[i] == name) {
|
|
|
|
return true;
|
2015-05-27 15:11:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-06-09 00:17:45 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
function getLocalFile(file) {
|
2017-03-08 11:00:00 +01:00
|
|
|
if (!isIncluded(path.basename(file)) || isExcluded(path.basename(file))) {
|
2015-06-09 00:17:45 +02:00
|
|
|
return null;
|
|
|
|
}
|
2015-11-16 12:31:55 +01:00
|
|
|
try {
|
|
|
|
fs.statSync(file.replace(/\.js$/,".html"));
|
2015-05-27 15:11:11 +02:00
|
|
|
return {
|
|
|
|
file: file,
|
|
|
|
module: "node-red",
|
|
|
|
name: path.basename(file).replace(/^\d+-/,"").replace(/\.js$/,""),
|
|
|
|
version: settings.version
|
|
|
|
};
|
2015-11-16 12:31:55 +01:00
|
|
|
} catch(err) {
|
|
|
|
return null;
|
2015-05-27 15:11:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-07 17:02:15 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Synchronously walks the directory looking for node files.
|
|
|
|
* Emits 'node-icon-dir' events for an icon dirs found
|
|
|
|
* @param dir the directory to search
|
|
|
|
* @return an array of fully-qualified paths to .js files
|
|
|
|
*/
|
|
|
|
function getLocalNodeFiles(dir) {
|
2017-04-10 16:41:20 +02:00
|
|
|
dir = path.resolve(dir);
|
|
|
|
|
2015-04-07 17:02:15 +02:00
|
|
|
var result = [];
|
|
|
|
var files = [];
|
|
|
|
try {
|
|
|
|
files = fs.readdirSync(dir);
|
|
|
|
} catch(err) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
files.sort();
|
|
|
|
files.forEach(function(fn) {
|
|
|
|
var stats = fs.statSync(path.join(dir,fn));
|
|
|
|
if (stats.isFile()) {
|
|
|
|
if (/\.js$/.test(fn)) {
|
2015-05-27 15:11:11 +02:00
|
|
|
var info = getLocalFile(path.join(dir,fn));
|
|
|
|
if (info) {
|
|
|
|
result.push(info);
|
2015-04-07 17:02:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (stats.isDirectory()) {
|
|
|
|
// Ignore /.dirs/, /lib/ /node_modules/
|
2015-04-26 00:29:53 +02:00
|
|
|
if (!/^(\..*|lib|icons|node_modules|test|locales)$/.test(fn)) {
|
2015-04-07 17:02:15 +02:00
|
|
|
result = result.concat(getLocalNodeFiles(path.join(dir,fn)));
|
|
|
|
} else if (fn === "icons") {
|
2017-11-30 14:13:35 +01:00
|
|
|
var iconList = scanIconDir(path.join(dir,fn));
|
|
|
|
events.emit("node-icon-dir",{name:'node-red',path:path.join(dir,fn),icons:iconList});
|
2015-04-07 17:02:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
function scanDirForNodesModules(dir,moduleName) {
|
|
|
|
var results = [];
|
2018-01-24 16:07:43 +01:00
|
|
|
var scopeName;
|
2015-04-07 17:02:15 +02:00
|
|
|
try {
|
|
|
|
var files = fs.readdirSync(dir);
|
2018-01-24 16:07:43 +01:00
|
|
|
if (moduleName) {
|
|
|
|
var m = /^(?:(@[^/]+)[/])?([^@/]+)/.exec(moduleName);
|
|
|
|
if (m) {
|
|
|
|
scopeName = m[1];
|
|
|
|
moduleName = m[2];
|
|
|
|
}
|
|
|
|
}
|
2015-04-07 17:02:15 +02:00
|
|
|
for (var i=0;i<files.length;i++) {
|
|
|
|
var fn = files[i];
|
2016-05-06 11:16:41 +02:00
|
|
|
if (/^@/.test(fn)) {
|
2018-01-24 16:07:43 +01:00
|
|
|
if (scopeName && scopeName === fn) {
|
|
|
|
// Looking for a specific scope/module
|
|
|
|
results = results.concat(scanDirForNodesModules(path.join(dir,fn),moduleName));
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
results = results.concat(scanDirForNodesModules(path.join(dir,fn),moduleName));
|
|
|
|
}
|
2016-05-06 11:16:41 +02:00
|
|
|
} else {
|
2017-03-08 11:00:00 +01:00
|
|
|
if (isIncluded(fn) && !isExcluded(fn) && (!moduleName || fn == moduleName)) {
|
2016-05-06 11:16:41 +02:00
|
|
|
var pkgfn = path.join(dir,fn,"package.json");
|
|
|
|
try {
|
|
|
|
var pkg = require(pkgfn);
|
|
|
|
if (pkg['node-red']) {
|
|
|
|
var moduleDir = path.join(dir,fn);
|
|
|
|
results.push({dir:moduleDir,package:pkg});
|
|
|
|
}
|
|
|
|
} catch(err) {
|
|
|
|
if (err.code != "MODULE_NOT_FOUND") {
|
|
|
|
// TODO: handle unexpected error
|
|
|
|
}
|
2015-04-07 17:02:15 +02:00
|
|
|
}
|
2016-05-06 11:16:41 +02:00
|
|
|
if (fn == moduleName) {
|
|
|
|
break;
|
2015-04-07 17:02:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch(err) {
|
|
|
|
}
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Scans the node_modules path for nodes
|
|
|
|
* @param moduleName the name of the module to be found
|
|
|
|
* @return a list of node modules: {dir,package}
|
|
|
|
*/
|
|
|
|
function scanTreeForNodesModules(moduleName) {
|
2015-12-07 00:13:52 +01:00
|
|
|
var dir = settings.coreNodesDir;
|
2015-04-07 17:02:15 +02:00
|
|
|
var results = [];
|
|
|
|
var userDir;
|
|
|
|
|
|
|
|
if (settings.userDir) {
|
|
|
|
userDir = path.join(settings.userDir,"node_modules");
|
2016-08-04 17:49:36 +02:00
|
|
|
results = scanDirForNodesModules(userDir,moduleName);
|
|
|
|
results.forEach(function(r) { r.local = true; });
|
2015-04-07 17:02:15 +02:00
|
|
|
}
|
2015-07-10 22:42:14 +02:00
|
|
|
|
2015-12-07 00:29:58 +01:00
|
|
|
if (dir) {
|
|
|
|
var up = path.resolve(path.join(dir,".."));
|
|
|
|
while (up !== dir) {
|
|
|
|
var pm = path.join(dir,"node_modules");
|
|
|
|
if (pm != userDir) {
|
|
|
|
results = results.concat(scanDirForNodesModules(pm,moduleName));
|
|
|
|
}
|
|
|
|
dir = up;
|
|
|
|
up = path.resolve(path.join(dir,".."));
|
2015-04-07 17:02:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getModuleNodeFiles(module) {
|
2015-07-10 22:42:14 +02:00
|
|
|
|
2015-04-07 17:02:15 +02:00
|
|
|
var moduleDir = module.dir;
|
|
|
|
var pkg = module.package;
|
2015-07-10 22:42:14 +02:00
|
|
|
|
2015-04-07 17:02:15 +02:00
|
|
|
var nodes = pkg['node-red'].nodes||{};
|
|
|
|
var results = [];
|
|
|
|
var iconDirs = [];
|
2015-07-10 22:42:14 +02:00
|
|
|
|
2015-04-07 17:02:15 +02:00
|
|
|
for (var n in nodes) {
|
|
|
|
/* istanbul ignore else */
|
|
|
|
if (nodes.hasOwnProperty(n)) {
|
|
|
|
var file = path.join(moduleDir,nodes[n]);
|
|
|
|
results.push({
|
|
|
|
file: file,
|
|
|
|
module: pkg.name,
|
|
|
|
name: n,
|
|
|
|
version: pkg.version
|
|
|
|
});
|
|
|
|
var iconDir = path.join(moduleDir,path.dirname(nodes[n]),"icons");
|
|
|
|
if (iconDirs.indexOf(iconDir) == -1) {
|
2015-11-16 12:31:55 +01:00
|
|
|
try {
|
|
|
|
fs.statSync(iconDir);
|
2017-11-30 14:13:35 +01:00
|
|
|
var iconList = scanIconDir(iconDir);
|
|
|
|
events.emit("node-icon-dir",{name:pkg.name,path:iconDir,icons:iconList});
|
2015-04-07 17:02:15 +02:00
|
|
|
iconDirs.push(iconDir);
|
2015-11-16 12:31:55 +01:00
|
|
|
} catch(err) {
|
2015-04-07 17:02:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-03-03 00:34:24 +01:00
|
|
|
var examplesDir = path.join(moduleDir,"examples");
|
|
|
|
try {
|
|
|
|
fs.statSync(examplesDir)
|
|
|
|
events.emit("node-examples-dir",{name:pkg.name,path:examplesDir});
|
|
|
|
} catch(err) {
|
|
|
|
}
|
2015-04-07 17:02:15 +02:00
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
2015-12-07 00:13:52 +01:00
|
|
|
function getNodeFiles(disableNodePathScan) {
|
2015-04-07 17:02:15 +02:00
|
|
|
var dir;
|
|
|
|
// Find all of the nodes to load
|
2015-12-07 00:29:58 +01:00
|
|
|
var nodeFiles = [];
|
2015-07-10 22:42:14 +02:00
|
|
|
|
2017-11-30 14:13:35 +01:00
|
|
|
var dir = path.resolve(__dirname + '/../../../../public/icons');
|
|
|
|
var iconList = scanIconDir(dir);
|
|
|
|
events.emit("node-icon-dir",{name:'node-red',path:dir,icons:iconList});
|
|
|
|
|
2015-12-07 00:29:58 +01:00
|
|
|
if (settings.coreNodesDir) {
|
|
|
|
nodeFiles = getLocalNodeFiles(path.resolve(settings.coreNodesDir));
|
2017-04-10 16:41:20 +02:00
|
|
|
var defaultLocalesPath = path.join(settings.coreNodesDir,"core","locales");
|
2015-12-07 00:29:58 +01:00
|
|
|
i18n.registerMessageCatalog("node-red",defaultLocalesPath,"messages.json");
|
|
|
|
}
|
2015-07-10 22:42:14 +02:00
|
|
|
|
2015-04-07 17:02:15 +02:00
|
|
|
if (settings.userDir) {
|
2018-01-05 15:23:47 +01:00
|
|
|
dir = path.join(settings.userDir,"lib","icons");
|
|
|
|
iconList = scanIconDir(dir);
|
|
|
|
if (iconList.length > 0) {
|
|
|
|
events.emit("node-icon-dir",{name:'Library',path:dir,icons:iconList});
|
|
|
|
}
|
|
|
|
|
2015-04-07 17:02:15 +02:00
|
|
|
dir = path.join(settings.userDir,"nodes");
|
|
|
|
nodeFiles = nodeFiles.concat(getLocalNodeFiles(dir));
|
|
|
|
}
|
|
|
|
if (settings.nodesDir) {
|
|
|
|
dir = settings.nodesDir;
|
|
|
|
if (typeof settings.nodesDir == "string") {
|
|
|
|
dir = [dir];
|
|
|
|
}
|
|
|
|
for (var i=0;i<dir.length;i++) {
|
|
|
|
nodeFiles = nodeFiles.concat(getLocalNodeFiles(dir[i]));
|
|
|
|
}
|
|
|
|
}
|
2015-07-10 22:42:14 +02:00
|
|
|
|
2015-04-07 17:02:15 +02:00
|
|
|
var nodeList = {
|
|
|
|
"node-red": {
|
|
|
|
name: "node-red",
|
|
|
|
version: settings.version,
|
|
|
|
nodes: {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nodeFiles.forEach(function(node) {
|
|
|
|
nodeList["node-red"].nodes[node.name] = node;
|
|
|
|
});
|
2015-07-10 22:42:14 +02:00
|
|
|
|
2015-04-07 17:02:15 +02:00
|
|
|
if (!disableNodePathScan) {
|
|
|
|
var moduleFiles = scanTreeForNodesModules();
|
|
|
|
moduleFiles.forEach(function(moduleFile) {
|
|
|
|
var nodeModuleFiles = getModuleNodeFiles(moduleFile);
|
|
|
|
nodeList[moduleFile.package.name] = {
|
|
|
|
name: moduleFile.package.name,
|
|
|
|
version: moduleFile.package.version,
|
2016-08-04 17:49:36 +02:00
|
|
|
local: moduleFile.local||false,
|
2015-04-07 17:02:15 +02:00
|
|
|
nodes: {}
|
|
|
|
};
|
2015-07-10 22:42:14 +02:00
|
|
|
if (moduleFile.package['node-red'].version) {
|
|
|
|
nodeList[moduleFile.package.name].redVersion = moduleFile.package['node-red'].version;
|
|
|
|
}
|
2015-04-07 17:02:15 +02:00
|
|
|
nodeModuleFiles.forEach(function(node) {
|
2016-08-04 17:49:36 +02:00
|
|
|
node.local = moduleFile.local||false;
|
2015-04-07 17:02:15 +02:00
|
|
|
nodeList[moduleFile.package.name].nodes[node.name] = node;
|
|
|
|
});
|
|
|
|
nodeFiles = nodeFiles.concat(nodeModuleFiles);
|
|
|
|
});
|
2015-12-07 00:13:52 +01:00
|
|
|
} else {
|
|
|
|
console.log("node path scan disabled");
|
2015-04-07 17:02:15 +02:00
|
|
|
}
|
|
|
|
return nodeList;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getModuleFiles(module) {
|
|
|
|
var nodeList = {};
|
2015-07-10 22:42:14 +02:00
|
|
|
|
2015-04-07 17:02:15 +02:00
|
|
|
var moduleFiles = scanTreeForNodesModules(module);
|
|
|
|
if (moduleFiles.length === 0) {
|
2015-05-21 00:46:49 +02:00
|
|
|
var err = new Error(log._("nodes.registry.localfilesystem.module-not-found", {module:module}));
|
2015-04-07 17:02:15 +02:00
|
|
|
err.code = 'MODULE_NOT_FOUND';
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
|
|
|
|
moduleFiles.forEach(function(moduleFile) {
|
|
|
|
var nodeModuleFiles = getModuleNodeFiles(moduleFile);
|
|
|
|
nodeList[moduleFile.package.name] = {
|
|
|
|
name: moduleFile.package.name,
|
|
|
|
version: moduleFile.package.version,
|
|
|
|
nodes: {}
|
|
|
|
};
|
2015-07-10 22:42:14 +02:00
|
|
|
if (moduleFile.package['node-red'].version) {
|
|
|
|
nodeList[moduleFile.package.name].redVersion = moduleFile.package['node-red'].version;
|
|
|
|
}
|
2015-04-07 17:02:15 +02:00
|
|
|
nodeModuleFiles.forEach(function(node) {
|
|
|
|
nodeList[moduleFile.package.name].nodes[node.name] = node;
|
2016-11-16 15:12:31 +01:00
|
|
|
nodeList[moduleFile.package.name].nodes[node.name].local = moduleFile.local || false;
|
2015-04-07 17:02:15 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
return nodeList;
|
|
|
|
}
|
|
|
|
|
2017-11-30 14:13:35 +01:00
|
|
|
function scanIconDir(dir) {
|
|
|
|
var iconList = [];
|
|
|
|
try {
|
|
|
|
var files = fs.readdirSync(dir);
|
|
|
|
files.forEach(function(file) {
|
|
|
|
var stats = fs.statSync(path.join(dir, file));
|
|
|
|
if (stats.isFile() && iconFileExtensions.indexOf(path.extname(file)) !== -1) {
|
|
|
|
iconList.push(file);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} catch(err) {
|
|
|
|
}
|
|
|
|
return iconList;
|
|
|
|
}
|
2015-04-07 17:02:15 +02:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
init: init,
|
|
|
|
getNodeFiles: getNodeFiles,
|
2015-05-27 15:11:11 +02:00
|
|
|
getLocalFile: getLocalFile,
|
2015-04-07 17:02:15 +02:00
|
|
|
getModuleFiles: getModuleFiles
|
|
|
|
}
|