merge to latest

This commit is contained in:
Nick O'Leary
2018-05-23 12:45:29 +01:00
31 changed files with 444 additions and 105 deletions

View File

@@ -190,11 +190,11 @@
},
"$flowContext": {
"args": "string",
"desc": "Retrieves a flow context property."
"desc": "Retrieves a flow context property.\n\nThis is a Node-RED defined function."
},
"$globalContext": {
"args": "string",
"desc": "Retrieves a global context property."
"desc": "Retrieves a global context property.\n\nThis is a Node-RED defined function."
},
"$pad": {
"args": "string, width [, char]",
@@ -215,5 +215,9 @@
"$toMillis": {
"args": "timestamp",
"desc": "Convert a `timestamp` string in the ISO 8601 format to the number of milliseconds since the Unix Epoch (1 January, 1970 UTC) as a number. An error is thrown if the string is not in the correct format."
},
"$env": {
"args": "arg",
"desc": "Returns the value of an environment variable.\n\nThis is a Node-RED defined function."
}
}

View File

@@ -64,6 +64,17 @@ function copyObjectProperties(src,dst,copyList,blockList) {
}
}
}
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;
}
}
function createNodeApi(node) {
var red = {
@@ -73,6 +84,7 @@ function createNodeApi(node) {
events: runtime.events,
util: runtime.util,
version: runtime.version,
require: requireModule,
comms: {
publish: function(topic,data,retain) {
runtime.events.emit("comms",{

View File

@@ -88,7 +88,7 @@ function getLocalNodeFiles(dir) {
try {
files = fs.readdirSync(dir);
} catch(err) {
return result;
return {files: [], icons: []};
}
files.sort();
files.forEach(function(fn) {
@@ -296,6 +296,7 @@ function getNodeFiles(disableNodePathScan) {
nodeList[moduleFile.package.name] = {
name: moduleFile.package.name,
version: moduleFile.package.version,
path: moduleFile.dir,
local: moduleFile.local||false,
nodes: {},
icons: nodeModuleFiles.icons,

View File

@@ -339,6 +339,7 @@ function getModuleInfo(module) {
name: module,
version: moduleConfigs[module].version,
local: moduleConfigs[module].local,
path: moduleConfigs[module].path,
nodes: []
};
for (var i = 0; i < nodes.length; ++i) {
@@ -560,30 +561,6 @@ var icon_paths = {
var iconCache = {};
var defaultIcon = path.resolve(__dirname + '/../../public/icons/arrow-in.png');
function nodeIconDir(dir) {
icon_paths[dir.name] = icon_paths[dir.name] || [];
icon_paths[dir.name].push(path.resolve(dir.path));
if (dir.icons) {
if (!moduleConfigs[dir.name]) {
moduleConfigs[dir.name] = {
name: dir.name,
nodes: {},
icons: []
};
}
var module = moduleConfigs[dir.name];
if (module.icons === undefined) {
module.icons = [];
}
dir.icons.forEach(function(icon) {
if (module.icons.indexOf(icon) === -1) {
module.icons.push(icon);
}
});
}
}
function getNodeIconPath(module,icon) {
if (/\.\./.test(icon)) {
throw new Error();
@@ -624,7 +601,6 @@ function getNodeIcons() {
}
}
}
return iconList;
}

View File

@@ -37,7 +37,8 @@ function diffNodes(oldNode,newNode) {
return false;
}
var EnvVarPropertyRE = /^\$\((\S+)\)$/;
var EnvVarPropertyRE_old = /^\$\((\S+)\)$/;
var EnvVarPropertyRE = /^\${(\S+)}$/;
function mapEnvVarProperties(obj,prop) {
if (Buffer.isBuffer(obj[prop])) {
@@ -47,11 +48,9 @@ function mapEnvVarProperties(obj,prop) {
mapEnvVarProperties(obj[prop],i);
}
} else if (typeof obj[prop] === 'string') {
var m;
if ( (m = EnvVarPropertyRE.exec(obj[prop])) !== null) {
if (process.env.hasOwnProperty(m[1])) {
obj[prop] = process.env[m[1]];
}
if (obj[prop][0] === "$" && (EnvVarPropertyRE_old.test(obj[prop]) || EnvVarPropertyRE.test(obj[prop])) ) {
var envVar = obj[prop].substring(2,obj[prop].length-1);
obj[prop] = process.env.hasOwnProperty(envVar)?process.env[envVar]:obj[prop];
}
} else {
for (var p in obj[prop]) {

View File

@@ -73,7 +73,8 @@ var localfilesystem = {
var defaultPackage = {
"name": "node-red-project",
"description": "A Node-RED Project",
"version": "0.0.1"
"version": "0.0.1",
"private": true
};
return util.writeFile(packageFile,JSON.stringify(defaultPackage,"",4));
}

View File

@@ -303,6 +303,23 @@ function setMessageProperty(msg,prop,value,createMissing) {
}
}
function evaluteEnvProperty(value) {
if (/^\${[^}]+}$/.test(value)) {
// ${ENV_VAR}
value = value.substring(2,value.length-1);
value = process.env.hasOwnProperty(value)?process.env[value]:""
} else if (!/\${\S+}/.test(value)) {
// ENV_VAR
value = process.env.hasOwnProperty(value)?process.env[value]:""
} else {
// FOO${ENV_VAR}BAR
value = value.replace(/\${([^}]+)}/g, function(match, v) {
return process.env.hasOwnProperty(v)?process.env[v]:""
});
}
return value;
}
function evaluateNodeProperty(value, type, node, msg) {
if (type === 'str') {
return ""+value;
@@ -328,6 +345,8 @@ function evaluateNodeProperty(value, type, node, msg) {
} else if (type === 'jsonata') {
var expr = prepareJSONataExpression(value,node);
return evaluateJSONataExpression(expr,msg);
} else if (type === 'env') {
return evaluteEnvProperty(value);
}
return value;
}
@@ -340,6 +359,9 @@ function prepareJSONataExpression(value,node) {
expr.assign('globalContext',function(val) {
return node.context().global.get(val);
});
expr.assign('env', function(val) {
return process.env[val];
})
expr.registerFunction('clone', cloneMessage, '<(oa)-:o>');
expr._legacyMode = /(^|[^a-zA-Z0-9_'"])msg([^a-zA-Z0-9_'"]|$)/.test(value);
return expr;