Added env.

This commit is contained in:
SamTV12345 2024-03-18 20:28:44 +01:00
parent ad3393d2fe
commit 8ca9621166

View file

@ -27,6 +27,8 @@
* limitations under the License. * limitations under the License.
*/ */
import {MapArrayType} from "../types/MapType";
const absolutePaths = require('./AbsolutePaths'); const absolutePaths = require('./AbsolutePaths');
const deepEqual = require('fast-deep-equal/es6'); const deepEqual = require('fast-deep-equal/es6');
const fs = require('fs'); const fs = require('fs');
@ -598,14 +600,16 @@ const coerceValue = (stringValue:string) => {
* *
* see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#The_replacer_parameter * see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#The_replacer_parameter
*/ */
const lookupEnvironmentVariables = (obj: object) => { const lookupEnvironmentVariables = (obj: MapArrayType<any>) => {
const stringifiedAndReplaced = JSON.stringify(obj, (key, value) => { function replaceEnvs(obj: MapArrayType<any>) {
for (let [key, value] of Object.entries(obj)) {
/* /*
* the first invocation of replacer() is with an empty key. Just go on, or * the first invocation of replacer() is with an empty key. Just go on, or
* we would zap the entire object. * we would zap the entire object.
*/ */
if (key === '') { if (key === '') {
return value; obj[key] = value;
continue
} }
/* /*
@ -616,10 +620,17 @@ const lookupEnvironmentVariables = (obj: object) => {
* The environment variable expansion syntax "${ENV_VAR}" is just a string * The environment variable expansion syntax "${ENV_VAR}" is just a string
* of specific form, after all. * of specific form, after all.
*/ */
if (typeof value !== 'string') { if (typeof value !== 'string' && typeof value !== 'object') {
return value; obj[key] = value;
continue
} }
if (typeof value === "object") {
replaceEnvs(value);
continue
}
/* /*
* Let's check if the string value looks like a variable expansion (e.g.: * Let's check if the string value looks like a variable expansion (e.g.:
* "${ENV_VAR}" or "${ENV_VAR:default_value}") * "${ENV_VAR}" or "${ENV_VAR:default_value}")
@ -629,8 +640,8 @@ const lookupEnvironmentVariables = (obj: object) => {
if (match == null) { if (match == null) {
// no match: use the value literally, without any substitution // no match: use the value literally, without any substitution
obj[key] = value;
return value; continue
} }
/* /*
@ -651,14 +662,16 @@ const lookupEnvironmentVariables = (obj: object) => {
* We have to return null, because if we just returned undefined, the * We have to return null, because if we just returned undefined, the
* configuration item "key" would be stripped from the returned object. * configuration item "key" would be stripped from the returned object.
*/ */
return null; obj[key] = null;
continue
} }
if ((envVarValue === undefined) && (defaultValue !== undefined)) { if ((envVarValue === undefined) && (defaultValue !== undefined)) {
logger.debug(`Environment variable "${envVarName}" not found for ` + logger.debug(`Environment variable "${envVarName}" not found for ` +
`configuration key "${key}". Falling back to default value.`); `configuration key "${key}". Falling back to default value.`);
return coerceValue(defaultValue); obj[key] = coerceValue(defaultValue);
continue
} }
// envVarName contained some value. // envVarName contained some value.
@ -670,12 +683,23 @@ const lookupEnvironmentVariables = (obj: object) => {
logger.debug( logger.debug(
`Configuration key "${key}" will be read from environment variable "${envVarName}"`); `Configuration key "${key}" will be read from environment variable "${envVarName}"`);
return coerceValue(envVarValue!); obj[key] = coerceValue(envVarValue!);
}); }
}
const newSettings = JSON.parse(stringifiedAndReplaced); replaceEnvs(obj);
return newSettings; // Add plugin ENV variables
for (const key of Object.keys(process.env)) {
if (key.startsWith('EP_')) {
// Add to object
obj[key] = process.env[key];
}
}
console.log(obj)
return obj;
}; };
/** /**
@ -718,9 +742,7 @@ const parseSettings = (settingsFilename:string, isSettings:boolean) => {
logger.info(`${settingsType} loaded from: ${settingsFilename}`); logger.info(`${settingsType} loaded from: ${settingsFilename}`);
const replacedSettings = lookupEnvironmentVariables(settings); return lookupEnvironmentVariables(settings);
return replacedSettings;
} catch (e:any) { } catch (e:any) {
logger.error(`There was an error processing your ${settingsType} ` + logger.error(`There was an error processing your ${settingsType} ` +
`file from ${settingsFilename}: ${e.message}`); `file from ${settingsFilename}: ${e.message}`);