🐛 🔧 fix and adjust helpers

This commit is contained in:
Ben Hesseldieck 2021-04-05 14:46:02 +02:00
parent 5dea2ee7de
commit c5fb08f600
2 changed files with 40 additions and 35 deletions

View file

@ -83,36 +83,25 @@ export async function getVersions(): Promise<IPackageVersions> {
} }
/** /**
* Retrieves configuration values from env * Extracts configuration schema for key
* *
* @param {string} configKey * @param {string} configKey
* @param {IDataObject} currentSchema * @param {IDataObject} configSchema
* @returns {string | boolean | number | undefined | void} * @returns {IDataObject} schema of the configKey
*/ */
function getConfigFromEnv(configKey: string, currentSchema: IDataObject): string | boolean | number | undefined | void { function extractSchemaForKey(configKey: string, configSchema: IDataObject): IDataObject {
const configKeyParts = configKey.split('.'); const configKeyParts = configKey.split('.');
for (const key of configKeyParts) { for (const key of configKeyParts) {
if (currentSchema[key] === undefined) { if (configSchema[key] === undefined) {
throw new Error(`Key "${key}" of ConfigKey "${configKey}" does not exist`); throw new Error(`Key "${key}" of ConfigKey "${configKey}" does not exist`);
} else if ((currentSchema[key]! as IDataObject).properties === undefined) { } else if ((configSchema[key]! as IDataObject).properties === undefined) {
currentSchema = currentSchema[key] as IDataObject; configSchema = configSchema[key] as IDataObject;
} else { } else {
currentSchema = (currentSchema[key] as IDataObject).properties as IDataObject; configSchema = (configSchema[key] as IDataObject).properties as IDataObject;
} }
} }
return configSchema;
// Check if environment variable is defined for config key
if (currentSchema.env === undefined) {
// No environment variable defined, so return value from config
return config.get(configKey);
}
// Check if special file enviroment variable exists
if (process.env[currentSchema.env + '_FILE'] === undefined) {
// Does not exist, so return value from config
return config.get(configKey);
}
} }
/** /**
@ -125,18 +114,26 @@ function getConfigFromEnv(configKey: string, currentSchema: IDataObject): string
export async function getConfigValue(configKey: string): Promise<string | boolean | number | undefined> { export async function getConfigValue(configKey: string): Promise<string | boolean | number | undefined> {
// Get the environment variable // Get the environment variable
const configSchema = config.getSchema(); const configSchema = config.getSchema();
const currentSchema = configSchema.properties as IDataObject; const currentSchema = extractSchemaForKey(configKey, configSchema.properties as IDataObject);
const envConfig = getConfigFromEnv(configKey, currentSchema); // Check if environment variable is defined for config key
if (envConfig) { if (currentSchema.env === undefined) {
return envConfig; // No environment variable defined, so return value from config
return config.get(configKey);
}
// Check if special file enviroment variable exists
const fileEnvironmentVariable = process.env[currentSchema.env + '_FILE'];
if (fileEnvironmentVariable === undefined) {
// Does not exist, so return value from config
return config.get(configKey);
} }
let data; let data;
try { try {
data = await fsReadFileAsync(process.env[currentSchema.env + '_FILE']!, 'utf8') as string; data = await fsReadFileAsync(fileEnvironmentVariable, 'utf8') as string;
} catch (error) { } catch (error) {
if (error.code === 'ENOENT') { if (error.code === 'ENOENT') {
throw new Error(`The file "${process.env[currentSchema.env + '_FILE']}" could not be found.`); throw new Error(`The file "${fileEnvironmentVariable}" could not be found.`);
} }
throw error; throw error;
@ -155,18 +152,26 @@ export async function getConfigValue(configKey: string): Promise<string | boolea
export function getConfigValueSync(configKey: string): string | boolean | number | undefined { export function getConfigValueSync(configKey: string): string | boolean | number | undefined {
// Get the environment variable // Get the environment variable
const configSchema = config.getSchema(); const configSchema = config.getSchema();
const currentSchema = configSchema.properties as IDataObject; const currentSchema = extractSchemaForKey(configKey, configSchema.properties as IDataObject);
const envConfig = getConfigFromEnv(configKey, currentSchema); // Check if environment variable is defined for config key
if (envConfig) { if (currentSchema.env === undefined) {
return envConfig; // No environment variable defined, so return value from config
return config.get(configKey);
}
// Check if special file enviroment variable exists
const fileEnvironmentVariable = process.env[currentSchema.env + '_FILE'];
if (fileEnvironmentVariable === undefined) {
// Does not exist, so return value from config
return config.get(configKey);
} }
let data; let data;
try { try {
data = fsReadFileSync(process.env[currentSchema.env + '_FILE']!, 'utf8') as string; data = fsReadFileSync(fileEnvironmentVariable, 'utf8') as string;
} catch (error) { } catch (error) {
if (error.code === 'ENOENT') { if (error.code === 'ENOENT') {
throw new Error(`The file "${process.env[currentSchema.env + '_FILE']}" could not be found.`); throw new Error(`The file "${fileEnvironmentVariable}" could not be found.`);
} }
throw error; throw error;

View file

@ -1,7 +1,7 @@
import { import {
DatabaseType, DatabaseType,
GenericHelpers, } from '../index';
} from '../'; import { getConfigValueSync } from '../../src/GenericHelpers'
/** /**
* Resolves the data type for the used database type * Resolves the data type for the used database type
@ -11,7 +11,7 @@ import {
* @returns {string} * @returns {string}
*/ */
export function resolveDataType(dataType: string) { export function resolveDataType(dataType: string) {
const dbType = GenericHelpers.getConfigValueSync('database.type') as DatabaseType; const dbType = getConfigValueSync('database.type') as DatabaseType;
const typeMap: { [key in DatabaseType]: { [key: string]: string } } = { const typeMap: { [key in DatabaseType]: { [key: string]: string } } = {
sqlite: { sqlite: {