/** * Copyright JS Foundation and other contributors, http://js.foundation * * 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 fs = require("fs-extra"); var path = require("path"); var semver = require("semver"); var localfilesystem = require("./localfilesystem"); var registry = require("./registry"); var registryUtil = require("./util") var i18n = require("@node-red/util").i18n; var log = require("@node-red/util").log; var settings; function init(_runtime) { settings = _runtime.settings; localfilesystem.init(settings); registryUtil.init(_runtime); } function load(disableNodePathScan) { // 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. //return loadModuleFiles(registry.getModuleList()); log.info(log._("server.loading")); var modules = localfilesystem.getNodeFiles(disableNodePathScan); return loadModuleFiles(modules); } function loadModuleTypeFiles(module, type) { const things = module[type]; var first = true; var promises = []; 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) { /* istanbul ignore else */ 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)) { //TODO: log it log.warn("["+module+"] "+log._("server.node-version-mismatch",{version:modules[module].redVersion})); modules[module].err = "version_mismatch"; continue; } if (module == "node-red" || !registry.getModuleInfo(module)) { 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; 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]); } } } } 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]); } } } } 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; }); } async function loadNodeTemplate(node) { return fs.readFile(node.template,'utf8').then(content => { var types = []; var regExp = /