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-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-08-04 23:23:06 +02:00
|
|
|
var i18n = require("@node-red/util").i18n; // TODO: separate module
|
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
|
|
|
var runtime;
|
2015-04-07 17:02:15 +02:00
|
|
|
|
2015-11-17 22:12:43 +01:00
|
|
|
function init(_runtime) {
|
|
|
|
runtime = _runtime;
|
|
|
|
settings = runtime.settings;
|
|
|
|
localfilesystem.init(runtime);
|
2015-04-07 17:02:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function load(defaultNodesDir,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.
|
|
|
|
//return loadNodeFiles(registry.getModuleList());
|
2016-10-12 23:30:32 +02:00
|
|
|
runtime.log.info(runtime.log._("server.loading"));
|
2015-07-10 22:42:14 +02:00
|
|
|
|
2015-04-07 17:02:15 +02:00
|
|
|
var nodeFiles = localfilesystem.getNodeFiles(defaultNodesDir,disableNodePathScan);
|
|
|
|
return loadNodeFiles(nodeFiles);
|
|
|
|
}
|
|
|
|
|
2015-11-24 23:38:42 +01:00
|
|
|
function copyObjectProperties(src,dst,copyList,blockList) {
|
|
|
|
if (!src) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (copyList && !blockList) {
|
|
|
|
copyList.forEach(function(i) {
|
|
|
|
if (src.hasOwnProperty(i)) {
|
|
|
|
var propDescriptor = Object.getOwnPropertyDescriptor(src,i);
|
|
|
|
Object.defineProperty(dst,i,propDescriptor);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else if (!copyList && blockList) {
|
|
|
|
for (var i in src) {
|
|
|
|
if (src.hasOwnProperty(i) && blockList.indexOf(i) === -1) {
|
|
|
|
var propDescriptor = Object.getOwnPropertyDescriptor(src,i);
|
|
|
|
Object.defineProperty(dst,i,propDescriptor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-05-21 23:08:04 +02:00
|
|
|
function requireModule(name) {
|
|
|
|
var moduleInfo = registry.getModuleInfo(name);
|
|
|
|
if (moduleInfo && moduleInfo.path) {
|
|
|
|
var relPath = path.relative(__dirname, moduleInfo.path);
|
|
|
|
return require(relPath);
|
|
|
|
} else {
|
|
|
|
var err = new Error(`Cannot find module '${name}'`);
|
|
|
|
err.code = "MODULE_NOT_FOUND";
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
2015-11-24 23:38:42 +01:00
|
|
|
|
|
|
|
function createNodeApi(node) {
|
|
|
|
var red = {
|
|
|
|
nodes: {},
|
|
|
|
log: {},
|
|
|
|
settings: {},
|
2016-02-13 00:18:08 +01:00
|
|
|
events: runtime.events,
|
2015-11-24 23:38:42 +01:00
|
|
|
util: runtime.util,
|
|
|
|
version: runtime.version,
|
2018-05-23 13:45:29 +02:00
|
|
|
require: requireModule,
|
2018-04-23 12:21:02 +02:00
|
|
|
comms: {
|
|
|
|
publish: function(topic,data,retain) {
|
|
|
|
runtime.events.emit("comms",{
|
|
|
|
topic: topic,
|
|
|
|
data: data,
|
|
|
|
retain: retain
|
|
|
|
})
|
|
|
|
}
|
2018-04-24 16:01:49 +02:00
|
|
|
},
|
|
|
|
library: {
|
|
|
|
register: function(type) {
|
|
|
|
return runtime.library.register(node.id,type);
|
|
|
|
}
|
2018-04-23 12:21:02 +02:00
|
|
|
}
|
2015-11-24 23:38:42 +01:00
|
|
|
}
|
2016-04-28 12:23:42 +02:00
|
|
|
copyObjectProperties(runtime.nodes,red.nodes,["createNode","getNode","eachNode","addCredentials","getCredentials","deleteCredentials" ]);
|
|
|
|
red.nodes.registerType = function(type,constructor,opts) {
|
|
|
|
runtime.nodes.registerType(node.id,type,constructor,opts);
|
|
|
|
}
|
2015-11-24 23:38:42 +01:00
|
|
|
copyObjectProperties(runtime.log,red.log,null,["init"]);
|
|
|
|
copyObjectProperties(runtime.settings,red.settings,null,["init","load","reset"]);
|
|
|
|
if (runtime.adminApi) {
|
|
|
|
red.auth = runtime.adminApi.auth;
|
|
|
|
red.httpAdmin = runtime.adminApi.adminApp;
|
2017-01-09 23:22:49 +01:00
|
|
|
red.httpNode = runtime.nodeApp;
|
2017-12-20 17:04:32 +01:00
|
|
|
red.server = runtime.server;
|
2015-11-24 23:38:42 +01:00
|
|
|
} else {
|
2017-01-09 23:22:49 +01:00
|
|
|
//TODO: runtime.adminApi is always stubbed if not enabled, so this block
|
|
|
|
// is unused - but may be needed for the unit tests
|
2015-11-24 23:38:42 +01:00
|
|
|
red.auth = {
|
|
|
|
needsPermission: function() {}
|
|
|
|
};
|
|
|
|
// TODO: stub out httpAdmin/httpNode/server
|
|
|
|
}
|
|
|
|
red["_"] = function() {
|
|
|
|
var args = Array.prototype.slice.call(arguments, 0);
|
2016-03-06 21:43:19 +01:00
|
|
|
if (args[0].indexOf(":") === -1) {
|
|
|
|
args[0] = node.namespace+":"+args[0];
|
|
|
|
}
|
2018-04-26 13:32:05 +02:00
|
|
|
return i18n._.apply(null,args);
|
2015-11-24 23:38:42 +01:00
|
|
|
}
|
|
|
|
return red;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-04-07 17:02:15 +02:00
|
|
|
function loadNodeFiles(nodeFiles) {
|
2015-04-08 00:46:52 +02:00
|
|
|
var promises = [];
|
2018-04-26 13:32:05 +02:00
|
|
|
var nodes = [];
|
2015-04-07 17:02:15 +02:00
|
|
|
for (var module in nodeFiles) {
|
|
|
|
/* istanbul ignore else */
|
|
|
|
if (nodeFiles.hasOwnProperty(module)) {
|
2015-07-10 22:42:14 +02:00
|
|
|
if (nodeFiles[module].redVersion &&
|
2017-05-15 14:57:35 +02:00
|
|
|
!semver.satisfies(runtime.version().replace(/(\-[1-9A-Za-z-][0-9A-Za-z-\.]*)?(\+[0-9A-Za-z-\.]+)?$/,""), nodeFiles[module].redVersion)) {
|
2015-07-10 22:42:14 +02:00
|
|
|
//TODO: log it
|
2016-07-28 16:43:26 +02:00
|
|
|
runtime.log.warn("["+module+"] "+runtime.log._("server.node-version-mismatch",{version:nodeFiles[module].redVersion}));
|
2018-04-26 13:32:05 +02:00
|
|
|
nodeFiles[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)) {
|
2015-04-08 21:17:24 +02:00
|
|
|
var first = true;
|
2015-04-07 17:02:15 +02:00
|
|
|
for (var node in nodeFiles[module].nodes) {
|
|
|
|
/* istanbul ignore else */
|
|
|
|
if (nodeFiles[module].nodes.hasOwnProperty(node)) {
|
2015-04-08 21:17:24 +02:00
|
|
|
if (module != "node-red" && first) {
|
|
|
|
// Check the module directory exists
|
|
|
|
first = false;
|
|
|
|
var fn = nodeFiles[module].nodes[node].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("/");
|
2015-07-10 22:42:14 +02:00
|
|
|
|
2015-04-08 21:17:24 +02:00
|
|
|
try {
|
|
|
|
var stat = fs.statSync(moduleFn);
|
|
|
|
} catch(err) {
|
|
|
|
// Module not found, don't attempt to load its nodes
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-07-10 22:42:14 +02:00
|
|
|
|
2015-04-07 17:02:15 +02:00
|
|
|
try {
|
2018-04-26 13:32:05 +02:00
|
|
|
promises.push(loadNodeConfig(nodeFiles[module].nodes[node]).then((function() {
|
|
|
|
var m = module;
|
|
|
|
var n = node;
|
|
|
|
return function(nodeSet) {
|
|
|
|
nodeFiles[m].nodes[n] = nodeSet;
|
|
|
|
nodes.push(nodeSet);
|
|
|
|
}
|
|
|
|
})()));
|
2015-04-07 17:02:15 +02:00
|
|
|
} catch(err) {
|
|
|
|
//
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-04-08 00:46:52 +02:00
|
|
|
return when.settle(promises).then(function(results) {
|
2018-04-26 13:32:05 +02:00
|
|
|
for (var module in nodeFiles) {
|
|
|
|
if (nodeFiles.hasOwnProperty(module)) {
|
|
|
|
if (!nodeFiles[module].err) {
|
|
|
|
registry.addModule(nodeFiles[module]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-04-08 00:46:52 +02:00
|
|
|
return loadNodeSetList(nodes);
|
|
|
|
});
|
2015-04-07 17:02:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function loadNodeConfig(fileInfo) {
|
2018-04-25 00:49:57 +02:00
|
|
|
return new Promise(function(resolve) {
|
2015-04-08 00:46:52 +02:00
|
|
|
var file = fileInfo.file;
|
|
|
|
var module = fileInfo.module;
|
|
|
|
var name = fileInfo.name;
|
|
|
|
var version = fileInfo.version;
|
2015-07-10 22:42:14 +02:00
|
|
|
|
2015-04-08 00:46:52 +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");
|
2015-04-07 17:02:15 +02:00
|
|
|
}
|
2015-04-08 00:46:52 +02:00
|
|
|
isEnabled = info.enabled;
|
2015-04-07 17:02:15 +02:00
|
|
|
}
|
2015-07-10 22:42:14 +02:00
|
|
|
|
2015-04-08 00:46:52 +02:00
|
|
|
var node = {
|
|
|
|
id: id,
|
|
|
|
module: module,
|
|
|
|
name: name,
|
|
|
|
file: file,
|
|
|
|
template: file.replace(/\.js$/,".html"),
|
|
|
|
enabled: isEnabled,
|
2015-04-08 21:17:24 +02:00
|
|
|
loaded:false,
|
2016-08-04 17:49:36 +02:00
|
|
|
version: version,
|
|
|
|
local: fileInfo.local
|
2015-04-08 00:46:52 +02:00
|
|
|
};
|
2015-04-08 21:17:24 +02:00
|
|
|
if (fileInfo.hasOwnProperty("types")) {
|
|
|
|
node.types = fileInfo.types;
|
|
|
|
}
|
2015-07-10 22:42:14 +02:00
|
|
|
|
2015-04-08 00:46:52 +02:00
|
|
|
fs.readFile(node.template,'utf8', function(err,content) {
|
|
|
|
if (err) {
|
|
|
|
node.types = [];
|
|
|
|
if (err.code === 'ENOENT') {
|
2015-04-08 21:17:24 +02:00
|
|
|
if (!node.types) {
|
|
|
|
node.types = [];
|
|
|
|
}
|
2016-11-16 23:46:01 +01:00
|
|
|
node.err = "Error: "+node.template+" does not exist";
|
2015-04-08 00:46:52 +02:00
|
|
|
} else {
|
2015-04-08 21:17:24 +02:00
|
|
|
node.types = [];
|
2015-04-08 00:46:52 +02:00
|
|
|
node.err = err.toString();
|
|
|
|
}
|
2015-04-26 00:29:53 +02:00
|
|
|
resolve(node);
|
2015-07-10 22:42:14 +02:00
|
|
|
} else {
|
2015-04-08 00:46:52 +02:00
|
|
|
var types = [];
|
2015-07-10 22:42:14 +02:00
|
|
|
|
2018-01-16 00:20:20 +01:00
|
|
|
var regExp = /<script (?:[^>]*)data-template-name\s*=\s*['"]([^'"]*)['"]/gi;
|
2015-04-08 00:46:52 +02:00
|
|
|
var match = null;
|
2015-07-10 22:42:14 +02:00
|
|
|
|
2016-11-16 23:46:01 +01:00
|
|
|
while ((match = regExp.exec(content)) !== null) {
|
2018-01-16 00:20:20 +01:00
|
|
|
types.push(match[1]);
|
2015-04-08 00:46:52 +02:00
|
|
|
}
|
|
|
|
node.types = types;
|
2015-07-10 22:42:14 +02:00
|
|
|
|
2018-01-16 00:20:20 +01:00
|
|
|
var langRegExp = /^<script[^>]* data-lang\s*=\s*['"](.+?)['"]/i;
|
2015-04-26 00:29:53 +02:00
|
|
|
regExp = /(<script[^>]* data-help-name=[\s\S]*?<\/script>)/gi;
|
|
|
|
match = null;
|
|
|
|
var mainContent = "";
|
|
|
|
var helpContent = {};
|
|
|
|
var index = 0;
|
2016-11-16 23:46:01 +01:00
|
|
|
while ((match = regExp.exec(content)) !== null) {
|
2015-04-26 00:29:53 +02:00
|
|
|
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
|
|
|
|
2018-04-26 13:32:05 +02:00
|
|
|
var lang = i18n.defaultLang;
|
2015-04-26 00:29:53 +02:00
|
|
|
if ((match = langRegExp.exec(help)) !== null) {
|
|
|
|
lang = match[1];
|
|
|
|
}
|
|
|
|
if (!helpContent.hasOwnProperty(lang)) {
|
|
|
|
helpContent[lang] = "";
|
|
|
|
}
|
2015-07-10 22:42:14 +02:00
|
|
|
|
2015-04-26 00:29:53 +02:00
|
|
|
helpContent[lang] += help;
|
|
|
|
}
|
|
|
|
mainContent += content.substring(index);
|
|
|
|
|
|
|
|
node.config = mainContent;
|
|
|
|
node.help = helpContent;
|
2015-04-08 00:46:52 +02:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
}
|
2018-02-07 13:59:58 +01:00
|
|
|
if (node.module === 'node-red') {
|
|
|
|
// do not look up locales directory for core nodes
|
|
|
|
node.namespace = node.module;
|
|
|
|
resolve(node);
|
|
|
|
return;
|
|
|
|
}
|
2015-04-26 00:29:53 +02:00
|
|
|
fs.stat(path.join(path.dirname(file),"locales"),function(err,stat) {
|
|
|
|
if (!err) {
|
|
|
|
node.namespace = node.id;
|
2018-04-26 13:32:05 +02:00
|
|
|
i18n.registerMessageCatalog(node.id,
|
2015-04-26 00:29:53 +02:00
|
|
|
path.join(path.dirname(file),"locales"),
|
|
|
|
path.basename(file,".js")+".json")
|
|
|
|
.then(function() {
|
|
|
|
resolve(node);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
node.namespace = node.module;
|
|
|
|
resolve(node);
|
|
|
|
}
|
|
|
|
});
|
2015-04-08 00:46:52 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
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) {
|
|
|
|
var nodeDir = path.dirname(node.file);
|
|
|
|
var nodeFn = path.basename(node.file);
|
|
|
|
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);
|
|
|
|
if (typeof r === "function") {
|
2015-07-10 22:42:14 +02:00
|
|
|
|
2015-11-24 23:38:42 +01:00
|
|
|
var red = 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) {
|
|
|
|
var i = stack.indexOf(node.file);
|
|
|
|
if (i > -1) {
|
|
|
|
var excerpt = stack.substring(i+node.file.length+1,i+node.file.length+20);
|
|
|
|
var m = /^(\d+):(\d+)/.exec(excerpt);
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function loadNodeSetList(nodes) {
|
|
|
|
var promises = [];
|
|
|
|
nodes.forEach(function(node) {
|
|
|
|
if (!node.err) {
|
|
|
|
promises.push(loadNodeSet(node));
|
|
|
|
} else {
|
|
|
|
promises.push(node);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return when.settle(promises).then(function() {
|
|
|
|
if (settings.available()) {
|
|
|
|
return registry.saveNodeList();
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function addModule(module) {
|
|
|
|
if (!settings.available()) {
|
|
|
|
throw new Error("Settings unavailable");
|
|
|
|
}
|
|
|
|
var nodes = [];
|
|
|
|
if (registry.getModuleInfo(module)) {
|
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 {
|
|
|
|
var moduleFiles = localfilesystem.getModuleFiles(module);
|
|
|
|
return loadNodeFiles(moduleFiles);
|
|
|
|
} 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
|
|
|
}
|