1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Allow properties to be specified by environment variables

A property set to $(ABC) will be substituted with the environment
variable ABC - if it exists. If the property doesn't exist, the property
is left unchanged.
This commit is contained in:
Nick O'Leary 2015-10-19 14:44:54 +01:00
parent 86aa7c97be
commit f626ee060a

View File

@ -27,10 +27,42 @@ function getID() {
return (1+Math.random()*4294967295).toString(16);
}
var EnvVarPropertyRE = /^\$\((\S+)\)$/;
function mapEnvVarProperties(obj,prop) {
if (Buffer.isBuffer(obj[prop])) {
return;
} else if (Array.isArray(obj[prop])) {
for (var i=0;i<obj[prop].length;i++) {
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]];
}
}
} else {
for (var p in obj[prop]) {
if (obj[prop].hasOwnProperty) {
mapEnvVarProperties(obj[prop],p);
}
}
}
}
function createNode(type,config) {
var nn = null;
var m;
var nt = typeRegistry.get(type);
if (nt) {
for (var p in config) {
if (config.hasOwnProperty(p)) {
mapEnvVarProperties(config,p);
}
}
try {
nn = new nt(clone(config));
}