mirror of
https://github.com/node-red/node-red.git
synced 2025-03-01 10:36:34 +00:00
add global environment variable feature
This commit is contained in:
@@ -777,6 +777,16 @@ const flowAPI = {
|
||||
}
|
||||
|
||||
|
||||
function getGlobalConfig() {
|
||||
let gconf = null;
|
||||
eachNode((n) => {
|
||||
if (n.type === "global-config") {
|
||||
gconf = n;
|
||||
}
|
||||
});
|
||||
return gconf;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
init: init,
|
||||
|
||||
@@ -790,6 +800,9 @@ module.exports = {
|
||||
get:getNode,
|
||||
eachNode: eachNode,
|
||||
|
||||
|
||||
getGlobalConfig: getGlobalConfig,
|
||||
|
||||
/**
|
||||
* Gets the current flow configuration
|
||||
*/
|
||||
|
@@ -18,7 +18,9 @@ var redUtil = require("@node-red/util").util;
|
||||
var Log = require("@node-red/util").log;
|
||||
var subflowInstanceRE = /^subflow:(.+)$/;
|
||||
var typeRegistry = require("@node-red/registry");
|
||||
const credentials = require("../nodes/credentials");
|
||||
|
||||
let _runtime = null;
|
||||
|
||||
var envVarExcludes = {};
|
||||
|
||||
@@ -263,15 +265,55 @@ function parseConfig(config) {
|
||||
return flow;
|
||||
}
|
||||
|
||||
function getGlobalEnv(name) {
|
||||
const nodes = _runtime.nodes;
|
||||
if (!nodes) {
|
||||
return null;
|
||||
}
|
||||
const gconf = nodes.getGlobalConfig();
|
||||
const env = gconf ? gconf.env : null;
|
||||
|
||||
if (env) {
|
||||
const cred = (gconf ? credentials.get(gconf.id) : null) || {
|
||||
map: {}
|
||||
};
|
||||
const map = cred.map;
|
||||
|
||||
for (let i = 0; i < env.length; i++) {
|
||||
const item = env[i];
|
||||
if (item.name === name) {
|
||||
if (item.type === "cred") {
|
||||
return {
|
||||
name: name,
|
||||
value: map[name],
|
||||
type: "cred"
|
||||
};
|
||||
}
|
||||
return item;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
init: function(runtime) {
|
||||
_runtime = runtime;
|
||||
envVarExcludes = {};
|
||||
if (runtime.settings.hasOwnProperty('envVarExcludes') && Array.isArray(runtime.settings.envVarExcludes)) {
|
||||
runtime.settings.envVarExcludes.forEach(v => envVarExcludes[v] = true);
|
||||
}
|
||||
},
|
||||
getEnvVar: function(k) {
|
||||
return !envVarExcludes[k]?process.env[k]:undefined
|
||||
if (!envVarExcludes[k]) {
|
||||
const item = getGlobalEnv(k);
|
||||
if (item) {
|
||||
const val = redUtil.evaluateNodeProperty(item.value, item.type, null, null, null);
|
||||
return val;
|
||||
}
|
||||
return process.env[k];
|
||||
}
|
||||
return undefined;
|
||||
},
|
||||
diffNodes: diffNodes,
|
||||
mapEnvVarProperties: mapEnvVarProperties,
|
||||
|
@@ -383,6 +383,11 @@ var api = module.exports = {
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (nodeType === "global-config") {
|
||||
if (JSON.stringify(savedCredentials.map) !== JSON.stringify(newCreds.map)) {
|
||||
savedCredentials.map = newCreds.map;
|
||||
dirty = true;
|
||||
}
|
||||
} else {
|
||||
var dashedType = nodeType.replace(/\s+/g, '-');
|
||||
var definition = credentialsDef[dashedType];
|
||||
|
@@ -205,6 +205,7 @@ module.exports = {
|
||||
getNode: flows.get,
|
||||
eachNode: flows.eachNode,
|
||||
getContext: context.get,
|
||||
getGlobalConfig: flows.getGlobalConfig,
|
||||
|
||||
clearContext: context.clear,
|
||||
|
||||
|
Reference in New Issue
Block a user