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.
|
|
|
|
**/
|
|
|
|
|
2020-08-14 01:20:44 +02:00
|
|
|
var fs = require("fs-extra");
|
2015-04-07 17:02:15 +02:00
|
|
|
var path = require("path");
|
2015-07-10 22:42:14 +02:00
|
|
|
var semver = require("semver");
|
2015-04-07 17:02:15 +02:00
|
|
|
|
|
|
|
var localfilesystem = require("./localfilesystem");
|
|
|
|
var registry = require("./registry");
|
2018-12-04 16:59:43 +01:00
|
|
|
var registryUtil = require("./util")
|
|
|
|
var i18n = require("@node-red/util").i18n;
|
2020-12-02 10:25:10 +01:00
|
|
|
var log = require("@node-red/util").log;
|
2018-04-26 13:32:05 +02:00
|
|
|
|
2015-04-07 17:02:15 +02:00
|
|
|
var settings;
|
|
|
|
|
2015-11-17 22:12:43 +01:00
|
|
|
function init(_runtime) {
|
2020-12-02 10:25:10 +01:00
|
|
|
settings = _runtime.settings;
|
|
|
|
localfilesystem.init(settings);
|
|
|
|
registryUtil.init(_runtime);
|
2015-04-07 17:02:15 +02:00
|
|
|
}
|
|
|
|
|
2019-03-05 14:24:35 +01:00
|
|
|
function load(disableNodePathScan) {
|
2015-04-08 21:17:24 +02:00
|
|
|
// To skip node scan, the following line will use the stored node list.
|
|
|
|
// We should expose that as an option at some point, although the
|
|
|
|
// performance gains are minimal.
|
2020-12-10 17:01:55 +01:00
|
|
|
//return loadModuleFiles(registry.getModuleList());
|
2020-12-02 10:25:10 +01:00
|
|
|
log.info(log._("server.loading"));
|
2015-07-10 22:42:14 +02:00
|
|
|
|
2020-12-10 17:01:55 +01:00
|
|
|
var modules = localfilesystem.getNodeFiles(disableNodePathScan);
|
|
|
|
return loadModuleFiles(modules);
|
2015-04-07 17:02:15 +02:00
|
|
|
}
|
|
|
|
|
2020-12-10 17:01:55 +01:00
|
|
|
|
|
|
|
function loadModuleTypeFiles(module, type) {
|
|
|
|
const things = module[type];
|
|
|
|
var first = true;
|
2015-04-08 00:46:52 +02:00
|
|
|
var promises = [];
|
2020-12-10 17:01:55 +01:00
|
|
|
for (var thingName in things) {
|
|
|
|
/* istanbul ignore else */
|
|
|
|
if (things.hasOwnProperty(thingName)) {
|
|
|
|
if (module.name != "node-red" && first) {
|
|
|
|
// Check the module directory exists
|
|
|
|
first = false;
|
|
|
|
var fn = things[thingName].file;
|
|
|
|
var parts = fn.split("/");
|
|
|
|
var i = parts.length-1;
|
|
|
|
for (;i>=0;i--) {
|
|
|
|
if (parts[i] == "node_modules") {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var moduleFn = parts.slice(0,i+2).join("/");
|
|
|
|
|
|
|
|
try {
|
|
|
|
var stat = fs.statSync(moduleFn);
|
|
|
|
} catch(err) {
|
|
|
|
// Module not found, don't attempt to load its nodes
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
var promise;
|
|
|
|
if (type === "nodes") {
|
|
|
|
promise = loadNodeConfig(things[thingName]);
|
|
|
|
} else if (type === "plugins") {
|
|
|
|
promise = loadPluginConfig(things[thingName]);
|
|
|
|
}
|
|
|
|
promises.push(
|
|
|
|
promise.then(
|
|
|
|
(function() {
|
|
|
|
var m = module.name;
|
|
|
|
var n = thingName;
|
|
|
|
return function(nodeSet) {
|
|
|
|
things[n] = nodeSet;
|
|
|
|
return nodeSet;
|
|
|
|
}
|
|
|
|
})()
|
|
|
|
).catch(err => {console.log(err)})
|
|
|
|
);
|
|
|
|
} catch(err) {
|
|
|
|
console.log(err)
|
|
|
|
//
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return promises;
|
|
|
|
}
|
|
|
|
|
|
|
|
function loadModuleFiles(modules) {
|
|
|
|
var pluginPromises = [];
|
|
|
|
var nodePromises = [];
|
|
|
|
for (var module in modules) {
|
2015-04-07 17:02:15 +02:00
|
|
|
/* istanbul ignore else */
|
2020-12-10 17:01:55 +01:00
|
|
|
if (modules.hasOwnProperty(module)) {
|
|
|
|
if (modules[module].redVersion &&
|
|
|
|
!semver.satisfies((settings.version||"0.0.0").replace(/(\-[1-9A-Za-z-][0-9A-Za-z-\.]*)?(\+[0-9A-Za-z-\.]+)?$/,""), modules[module].redVersion)) {
|
2015-07-10 22:42:14 +02:00
|
|
|
//TODO: log it
|
2020-12-10 17:01:55 +01:00
|
|
|
log.warn("["+module+"] "+log._("server.node-version-mismatch",{version:modules[module].redVersion}));
|
|
|
|
modules[module].err = "version_mismatch";
|
2015-07-10 22:42:14 +02:00
|
|
|
continue;
|
|
|
|
}
|
2015-05-27 15:11:11 +02:00
|
|
|
if (module == "node-red" || !registry.getModuleInfo(module)) {
|
2020-12-10 17:01:55 +01:00
|
|
|
if (modules[module].nodes) {
|
|
|
|
nodePromises = nodePromises.concat(loadModuleTypeFiles(modules[module], "nodes"));
|
|
|
|
}
|
|
|
|
if (modules[module].plugins) {
|
|
|
|
pluginPromises = pluginPromises.concat(loadModuleTypeFiles(modules[module], "plugins"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var pluginList;
|
|
|
|
var nodeList;
|
2015-07-10 22:42:14 +02:00
|
|
|
|
2020-12-10 17:01:55 +01:00
|
|
|
return Promise.all(pluginPromises).then(function(results) {
|
|
|
|
pluginList = results.filter(r => !!r);
|
|
|
|
// Initial plugin load has happened. Ensure modules that provide
|
|
|
|
// plugins are in the registry now.
|
|
|
|
for (var module in modules) {
|
|
|
|
if (modules.hasOwnProperty(module)) {
|
|
|
|
if (modules[module].plugins && Object.keys(modules[module].plugins).length > 0) {
|
|
|
|
// Add the modules for plugins
|
|
|
|
if (!modules[module].err) {
|
|
|
|
registry.addModule(modules[module]);
|
2015-04-07 17:02:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-12-10 17:01:55 +01:00
|
|
|
return loadNodeSetList(pluginList);
|
|
|
|
}).then(function() {
|
|
|
|
return Promise.all(nodePromises);
|
|
|
|
}).then(function(results) {
|
|
|
|
nodeList = results.filter(r => !!r);
|
|
|
|
// Initial node load has happened. Ensure remaining modules are in the registry
|
|
|
|
for (var module in modules) {
|
|
|
|
if (modules.hasOwnProperty(module)) {
|
|
|
|
if (!modules[module].plugins || Object.keys(modules[module].plugins).length === 0) {
|
|
|
|
if (!modules[module].err) {
|
|
|
|
registry.addModule(modules[module]);
|
|
|
|
}
|
2018-04-26 13:32:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-12-10 17:01:55 +01:00
|
|
|
return loadNodeSetList(nodeList);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function loadPluginTemplate(plugin) {
|
|
|
|
return fs.readFile(plugin.template,'utf8').then(content => {
|
|
|
|
plugin.config = content;
|
|
|
|
return plugin;
|
|
|
|
}).catch(err => {
|
|
|
|
if (err.code === 'ENOENT') {
|
|
|
|
plugin.err = "Error: "+plugin.template+" does not exist";
|
|
|
|
} else {
|
|
|
|
plugin.err = err.toString();
|
|
|
|
}
|
|
|
|
return plugin;
|
2015-04-08 00:46:52 +02:00
|
|
|
});
|
2015-04-07 17:02:15 +02:00
|
|
|
}
|
|
|
|
|
2020-08-14 01:20:44 +02:00
|
|
|
async function loadNodeTemplate(node) {
|
|
|
|
return fs.readFile(node.template,'utf8').then(content => {
|
|
|
|
var types = [];
|
2015-07-10 22:42:14 +02:00
|
|
|
|
2020-08-14 01:20:44 +02:00
|
|
|
var regExp = /<script (?:[^>]*)data-template-name\s*=\s*['"]([^'"]*)['"]/gi;
|
|
|
|
var match = null;
|
2015-07-10 22:42:14 +02:00
|
|
|
|
2020-08-14 01:20:44 +02:00
|
|
|
while ((match = regExp.exec(content)) !== null) {
|
|
|
|
types.push(match[1]);
|
2015-04-08 21:17:24 +02:00
|
|
|
}
|
2020-08-14 01:20:44 +02:00
|
|
|
node.types = types;
|
2015-07-10 22:42:14 +02:00
|
|
|
|
2020-08-14 01:20:44 +02:00
|
|
|
var langRegExp = /^<script[^>]* data-lang\s*=\s*['"](.+?)['"]/i;
|
|
|
|
regExp = /(<script[^>]* data-help-name=[\s\S]*?<\/script>)/gi;
|
|
|
|
match = null;
|
|
|
|
var mainContent = "";
|
|
|
|
var helpContent = {};
|
|
|
|
var index = 0;
|
|
|
|
while ((match = regExp.exec(content)) !== null) {
|
|
|
|
mainContent += content.substring(index,regExp.lastIndex-match[1].length);
|
|
|
|
index = regExp.lastIndex;
|
|
|
|
var help = content.substring(regExp.lastIndex-match[1].length,regExp.lastIndex);
|
2015-07-10 22:42:14 +02:00
|
|
|
|
2020-08-14 01:20:44 +02:00
|
|
|
var lang = i18n.defaultLang;
|
|
|
|
if ((match = langRegExp.exec(help)) !== null) {
|
|
|
|
lang = match[1];
|
|
|
|
}
|
|
|
|
if (!helpContent.hasOwnProperty(lang)) {
|
|
|
|
helpContent[lang] = "";
|
|
|
|
}
|
2015-07-10 22:42:14 +02:00
|
|
|
|
2020-08-14 01:20:44 +02:00
|
|
|
helpContent[lang] += help;
|
|
|
|
}
|
|
|
|
mainContent += content.substring(index);
|
2015-07-10 22:42:14 +02:00
|
|
|
|
2020-08-14 01:20:44 +02:00
|
|
|
node.config = mainContent;
|
|
|
|
node.help = helpContent;
|
|
|
|
// 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";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return node
|
|
|
|
}).catch(err => {
|
2020-08-28 17:36:11 +02:00
|
|
|
// ENOENT means no html file. We can live with that. But any other error
|
|
|
|
// should be fatal
|
|
|
|
// node.err = "Error: "+node.template+" does not exist";
|
2021-07-12 17:09:02 +02:00
|
|
|
node.types = node.types || [];
|
2020-08-28 17:36:11 +02:00
|
|
|
if (err.code !== 'ENOENT') {
|
2020-08-14 01:20:44 +02:00
|
|
|
node.err = err.toString();
|
|
|
|
}
|
|
|
|
return node;
|
|
|
|
});
|
|
|
|
}
|
2015-07-10 22:42:14 +02:00
|
|
|
|
2020-08-14 01:20:44 +02:00
|
|
|
async function loadNodeLocales(node) {
|
|
|
|
if (node.module === 'node-red') {
|
|
|
|
// do not look up locales directory for core nodes
|
|
|
|
node.namespace = node.module;
|
|
|
|
return node
|
|
|
|
}
|
2020-12-10 17:01:55 +01:00
|
|
|
const baseFile = node.file||node.template;
|
|
|
|
return fs.stat(path.join(path.dirname(baseFile),"locales")).then(stat => {
|
2020-08-14 01:20:44 +02:00
|
|
|
node.namespace = node.id;
|
|
|
|
return i18n.registerMessageCatalog(node.id,
|
2020-12-10 17:01:55 +01:00
|
|
|
path.join(path.dirname(baseFile),"locales"),
|
|
|
|
path.basename(baseFile).replace(/\.[^.]+$/,".json"))
|
2020-08-14 01:20:44 +02:00
|
|
|
.then(() => node);
|
|
|
|
}).catch(err => {
|
|
|
|
node.namespace = node.module;
|
|
|
|
return node;
|
|
|
|
});
|
|
|
|
}
|
2015-07-10 22:42:14 +02:00
|
|
|
|
2020-08-14 01:20:44 +02:00
|
|
|
async function loadNodeConfig(fileInfo) {
|
|
|
|
var file = fileInfo.file;
|
|
|
|
var module = fileInfo.module;
|
|
|
|
var name = fileInfo.name;
|
|
|
|
var version = fileInfo.version;
|
2015-04-26 00:29:53 +02:00
|
|
|
|
2020-08-14 01:20:44 +02:00
|
|
|
var id = module + "/" + name;
|
|
|
|
var info = registry.getNodeInfo(id);
|
|
|
|
var isEnabled = true;
|
|
|
|
if (info) {
|
|
|
|
if (info.hasOwnProperty("loaded")) {
|
|
|
|
throw new Error(file+" already loaded");
|
|
|
|
}
|
2021-07-12 17:09:02 +02:00
|
|
|
isEnabled = !(info.enabled === false);
|
2020-08-14 01:20:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var node = {
|
2020-12-10 17:01:55 +01:00
|
|
|
type: "node",
|
2020-08-14 01:20:44 +02:00
|
|
|
id: id,
|
|
|
|
module: module,
|
|
|
|
name: name,
|
|
|
|
file: file,
|
|
|
|
template: file.replace(/\.js$/,".html"),
|
|
|
|
enabled: isEnabled,
|
|
|
|
loaded:false,
|
|
|
|
version: version,
|
|
|
|
local: fileInfo.local,
|
|
|
|
types: [],
|
|
|
|
config: "",
|
|
|
|
help: {}
|
|
|
|
};
|
|
|
|
if (fileInfo.hasOwnProperty("types")) {
|
|
|
|
node.types = fileInfo.types;
|
|
|
|
}
|
|
|
|
await loadNodeLocales(node)
|
|
|
|
if (!settings.disableEditor) {
|
|
|
|
return loadNodeTemplate(node);
|
|
|
|
}
|
|
|
|
return node;
|
2015-04-07 17:02:15 +02:00
|
|
|
}
|
|
|
|
|
2020-12-10 17:01:55 +01:00
|
|
|
async function loadPluginConfig(fileInfo) {
|
|
|
|
var file = fileInfo.file;
|
|
|
|
var module = fileInfo.module;
|
|
|
|
var name = fileInfo.name;
|
|
|
|
var version = fileInfo.version;
|
|
|
|
|
|
|
|
var id = module + "/" + name;
|
|
|
|
var isEnabled = true;
|
|
|
|
|
|
|
|
// TODO: registry.getPluginInfo
|
|
|
|
|
|
|
|
// var info = registry.getPluginInfo(id);
|
|
|
|
// if (info) {
|
|
|
|
// if (info.hasOwnProperty("loaded")) {
|
|
|
|
// throw new Error(file+" already loaded");
|
|
|
|
// }
|
|
|
|
// isEnabled = info.enabled;
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
if (!fs.existsSync(jsFile)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
var plugin = {
|
|
|
|
type: "plugin",
|
|
|
|
id: id,
|
|
|
|
module: module,
|
|
|
|
name: name,
|
|
|
|
enabled: isEnabled,
|
|
|
|
loaded:false,
|
|
|
|
version: version,
|
|
|
|
local: fileInfo.local,
|
|
|
|
plugins: [],
|
|
|
|
config: "",
|
|
|
|
help: {}
|
|
|
|
};
|
|
|
|
var jsFile = file.replace(/\.[^.]+$/,".js");
|
|
|
|
var htmlFile = file.replace(/\.[^.]+$/,".html");
|
|
|
|
if (fs.existsSync(jsFile)) {
|
|
|
|
plugin.file = jsFile;
|
|
|
|
}
|
|
|
|
if (fs.existsSync(htmlFile)) {
|
|
|
|
plugin.template = htmlFile;
|
|
|
|
}
|
|
|
|
await loadNodeLocales(plugin)
|
|
|
|
|
|
|
|
if (plugin.template && !settings.disableEditor) {
|
|
|
|
return loadPluginTemplate(plugin);
|
|
|
|
}
|
|
|
|
return plugin
|
|
|
|
}
|
|
|
|
|
2015-04-07 17:02:15 +02:00
|
|
|
/**
|
|
|
|
* Loads the specified node into the runtime
|
|
|
|
* @param node a node info object - see loadNodeConfig
|
|
|
|
* @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 loadNodeSet(node) {
|
|
|
|
if (!node.enabled) {
|
2018-04-25 00:49:57 +02:00
|
|
|
return Promise.resolve(node);
|
2015-05-27 15:11:11 +02:00
|
|
|
} else {
|
2015-04-07 17:02:15 +02:00
|
|
|
}
|
|
|
|
try {
|
|
|
|
var loadPromise = null;
|
|
|
|
var r = require(node.file);
|
2022-06-14 22:54:00 +02:00
|
|
|
r = r.__esModule ? r.default : r
|
2015-04-07 17:02:15 +02:00
|
|
|
if (typeof r === "function") {
|
2015-07-10 22:42:14 +02:00
|
|
|
|
2018-12-04 16:59:43 +01:00
|
|
|
var red = registryUtil.createNodeApi(node);
|
2015-04-26 00:29:53 +02:00
|
|
|
var promise = r(red);
|
2015-04-07 17:02:15 +02:00
|
|
|
if (promise != null && typeof promise.then === "function") {
|
|
|
|
loadPromise = promise.then(function() {
|
|
|
|
node.enabled = true;
|
|
|
|
node.loaded = true;
|
|
|
|
return node;
|
2018-01-29 10:50:41 +01:00
|
|
|
}).catch(function(err) {
|
2015-04-07 17:02:15 +02:00
|
|
|
node.err = err;
|
|
|
|
return node;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (loadPromise == null) {
|
|
|
|
node.enabled = true;
|
|
|
|
node.loaded = true;
|
2018-04-25 00:49:57 +02:00
|
|
|
loadPromise = Promise.resolve(node);
|
2015-04-07 17:02:15 +02:00
|
|
|
}
|
|
|
|
return loadPromise;
|
|
|
|
} catch(err) {
|
|
|
|
node.err = err;
|
2017-01-30 10:37:08 +01:00
|
|
|
var stack = err.stack;
|
|
|
|
var message;
|
|
|
|
if (stack) {
|
2021-03-09 02:57:29 +01:00
|
|
|
var filePath = node.file;
|
|
|
|
try {
|
|
|
|
filePath = fs.realpathSync(filePath);
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
// ignore canonicalization error
|
|
|
|
}
|
|
|
|
var i = stack.indexOf(filePath);
|
2017-01-30 10:37:08 +01:00
|
|
|
if (i > -1) {
|
2021-03-09 02:57:29 +01:00
|
|
|
var excerpt = stack.substring(i+filePath.length+1,i+filePath.length+20);
|
|
|
|
var m = /^(\d+)/.exec(excerpt);
|
2017-01-30 10:37:08 +01:00
|
|
|
if (m) {
|
|
|
|
node.err = err+" (line:"+m[1]+")";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-04-25 00:49:57 +02:00
|
|
|
return Promise.resolve(node);
|
2015-04-07 17:02:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-10 17:01:55 +01:00
|
|
|
async function loadPlugin(plugin) {
|
|
|
|
if (!plugin.file) {
|
|
|
|
// No runtime component - nothing to load
|
|
|
|
return plugin;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
var r = require(plugin.file);
|
|
|
|
if (typeof r === "function") {
|
|
|
|
|
|
|
|
var red = registryUtil.createNodeApi(plugin);
|
|
|
|
var promise = r(red);
|
|
|
|
if (promise != null && typeof promise.then === "function") {
|
|
|
|
return promise.then(function() {
|
|
|
|
plugin.enabled = true;
|
|
|
|
plugin.loaded = true;
|
|
|
|
return plugin;
|
|
|
|
}).catch(function(err) {
|
|
|
|
plugin.err = err;
|
|
|
|
return plugin;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
plugin.enabled = true;
|
|
|
|
plugin.loaded = true;
|
|
|
|
return plugin;
|
|
|
|
} catch(err) {
|
|
|
|
console.log(err);
|
|
|
|
plugin.err = err;
|
|
|
|
var stack = err.stack;
|
|
|
|
var message;
|
|
|
|
if (stack) {
|
|
|
|
var i = stack.indexOf(plugin.file);
|
|
|
|
if (i > -1) {
|
|
|
|
var excerpt = stack.substring(i+node.file.length+1,i+plugin.file.length+20);
|
|
|
|
var m = /^(\d+):(\d+)/.exec(excerpt);
|
|
|
|
if (m) {
|
|
|
|
plugin.err = err+" (line:"+m[1]+")";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return plugin;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-07 17:02:15 +02:00
|
|
|
function loadNodeSetList(nodes) {
|
|
|
|
var promises = [];
|
|
|
|
nodes.forEach(function(node) {
|
|
|
|
if (!node.err) {
|
2020-12-10 17:01:55 +01:00
|
|
|
if (node.type === "plugin") {
|
|
|
|
promises.push(loadPlugin(node).catch(err => {}));
|
|
|
|
} else {
|
|
|
|
promises.push(loadNodeSet(node).catch(err => {}));
|
|
|
|
}
|
2015-04-07 17:02:15 +02:00
|
|
|
} else {
|
|
|
|
promises.push(node);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-11-30 15:38:48 +01:00
|
|
|
return Promise.all(promises).then(function() {
|
2015-04-07 17:02:15 +02:00
|
|
|
if (settings.available()) {
|
|
|
|
return registry.saveNodeList();
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function addModule(module) {
|
|
|
|
if (!settings.available()) {
|
|
|
|
throw new Error("Settings unavailable");
|
|
|
|
}
|
|
|
|
var nodes = [];
|
2020-11-25 20:07:30 +01:00
|
|
|
var existingInfo = registry.getModuleInfo(module);
|
|
|
|
if (existingInfo) {
|
2015-05-21 00:46:49 +02:00
|
|
|
// TODO: nls
|
|
|
|
var e = new Error("module_already_loaded");
|
2015-05-27 15:11:11 +02:00
|
|
|
e.code = "module_already_loaded";
|
2018-04-25 00:49:57 +02:00
|
|
|
return Promise.reject(e);
|
2015-04-07 17:02:15 +02:00
|
|
|
}
|
|
|
|
try {
|
2020-11-25 20:07:30 +01:00
|
|
|
var moduleFiles = {};
|
|
|
|
var moduleStack = [module];
|
|
|
|
while(moduleStack.length > 0) {
|
|
|
|
var moduleToLoad = moduleStack.shift();
|
|
|
|
var files = localfilesystem.getModuleFiles(moduleToLoad);
|
|
|
|
if (files[moduleToLoad]) {
|
|
|
|
moduleFiles[moduleToLoad] = files[moduleToLoad];
|
|
|
|
if (moduleFiles[moduleToLoad].dependencies) {
|
|
|
|
log.debug(`Loading dependencies for ${module}`)
|
|
|
|
for (var i=0; i<moduleFiles[moduleToLoad].dependencies.length; i++) {
|
|
|
|
var dep = moduleFiles[moduleToLoad].dependencies[i]
|
|
|
|
if (!registry.getModuleInfo(dep)) {
|
|
|
|
log.debug(` - load ${dep}`)
|
|
|
|
moduleStack.push(dep);
|
|
|
|
} else {
|
|
|
|
log.debug(` - already loaded ${dep}`)
|
|
|
|
registry.addModuleDependency(dep,moduleToLoad)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-26 14:44:38 +01:00
|
|
|
return loadModuleFiles(moduleFiles).then(() => module)
|
2015-04-07 17:02:15 +02:00
|
|
|
} catch(err) {
|
2018-04-25 00:49:57 +02:00
|
|
|
return Promise.reject(err);
|
2015-04-07 17:02:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-26 00:29:53 +02:00
|
|
|
function loadNodeHelp(node,lang) {
|
|
|
|
var base = path.basename(node.template);
|
2018-04-18 18:09:31 +02:00
|
|
|
var localePath;
|
2018-03-03 02:39:11 +01:00
|
|
|
if (node.module === 'node-red') {
|
|
|
|
var cat_dir = path.dirname(node.template);
|
|
|
|
var cat = path.basename(cat_dir);
|
|
|
|
var dir = path.dirname(cat_dir);
|
2018-09-28 17:48:25 +02:00
|
|
|
localePath = path.join(dir, "..", "locales", lang, cat, base)
|
2018-03-03 02:39:11 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
var dir = path.dirname(node.template);
|
|
|
|
localePath = path.join(dir,"locales",lang,base);
|
|
|
|
}
|
2015-04-26 00:29:53 +02:00
|
|
|
try {
|
|
|
|
// TODO: make this async
|
|
|
|
var content = fs.readFileSync(localePath, "utf8")
|
|
|
|
return content;
|
|
|
|
} catch(err) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getNodeHelp(node,lang) {
|
|
|
|
if (!node.help[lang]) {
|
|
|
|
var help = loadNodeHelp(node,lang);
|
|
|
|
if (help == null) {
|
|
|
|
var langParts = lang.split("-");
|
|
|
|
if (langParts.length == 2) {
|
|
|
|
help = loadNodeHelp(node,langParts[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (help) {
|
|
|
|
node.help[lang] = help;
|
2018-04-26 13:32:05 +02:00
|
|
|
} else if (lang === i18n.defaultLang) {
|
2017-01-19 08:55:57 +01:00
|
|
|
return null;
|
2015-04-26 00:29:53 +02:00
|
|
|
} else {
|
2018-04-26 13:32:05 +02:00
|
|
|
node.help[lang] = getNodeHelp(node, i18n.defaultLang);
|
2015-04-26 00:29:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return node.help[lang];
|
|
|
|
}
|
|
|
|
|
2015-04-07 17:02:15 +02:00
|
|
|
module.exports = {
|
|
|
|
init: init,
|
|
|
|
load: load,
|
|
|
|
addModule: addModule,
|
2015-04-26 00:29:53 +02:00
|
|
|
loadNodeSet: loadNodeSet,
|
|
|
|
getNodeHelp: getNodeHelp
|
2015-04-07 17:02:15 +02:00
|
|
|
}
|