Linted all config files.

This commit is contained in:
2018-02-07 13:49:11 +01:00
parent 444295d5d1
commit fe1ad2b1ad
4 changed files with 97 additions and 100 deletions

View File

@@ -4,40 +4,40 @@ const Field = require('./field.js');
let instance = null; let instance = null;
class Config { class Config {
constructor() { constructor() {
this.location = Config.determineLocation(); this.location = Config.determineLocation();
this.fields = require(`${this.location}`); this.fields = require(`${this.location}`);
} }
static getInstance() { static getInstance() {
if (instance == null) { if (instance == null) {
instance = new Config(); instance = new Config();
} }
return instance; return instance;
} }
static determineLocation() { static determineLocation() {
return path.join(__dirname, '..', '..', process.env.SEASONED_CONFIG); return path.join(__dirname, '..', '..', process.env.SEASONED_CONFIG);
} }
get(section, option) { get(section, option) {
if (this.fields[section] === undefined || this.fields[section][option] === undefined) { if (this.fields[section] === undefined || this.fields[section][option] === undefined) {
throw new Error(`Filed "${section} => ${option}" does not exist.`); throw new Error(`Filed "${section} => ${option}" does not exist.`);
} }
const field = new Field(this.fields[section][option]) const field = new Field(this.fields[section][option]);
if (field.value === '') { if (field.value === '') {
const envField = process.env[[section.toUpperCase(), option.toUpperCase()].join('_')] const envField = process.env[[section.toUpperCase(), option.toUpperCase()].join('_')];
if (envField !== undefined && envField.length !== 0) { return envField } if (envField !== undefined && envField.length !== 0) { return envField; }
} }
if (field.value === undefined) { if (field.value === undefined) {
throw new Error(`${section} => ${option} is empty.`); throw new Error(`${section} => ${option} is empty.`);
} }
return field.value; return field.value;
} }
} }
module.exports = Config; module.exports = Config;

View File

@@ -1,16 +1,15 @@
class EnvironmentVariables { class EnvironmentVariables {
constructor(variables) {
this.variables = variables || process.env;
}
constructor(variables) { get(variable) {
this.variables = variables || process.env; return this.variables[variable];
} }
get(variable) { has(variable) {
return this.variables[variable]; return this.get(variable) !== undefined;
} }
has(variable) {
return this.get(variable) !== undefined;
}
} }
module.exports = EnvironmentVariables; module.exports = EnvironmentVariables;

View File

@@ -2,49 +2,48 @@ const Filters = require('./filters.js');
const EnvironmentVariables = require('./environmentVariables.js'); const EnvironmentVariables = require('./environmentVariables.js');
class Field { class Field {
constructor(rawValue, environmentVariables) {
this.rawValue = rawValue;
this.filters = new Filters(rawValue);
this.valueWithoutFilters = this.filters.removeFiltersFromValue();
this.environmentVariables = new EnvironmentVariables(environmentVariables);
}
constructor(rawValue, environmentVariables) { get value() {
this.rawValue = rawValue; if (this.filters.isEmpty()) {
this.filters = new Filters(rawValue); return this.valueWithoutFilters;
this.valueWithoutFilters = this.filters.removeFiltersFromValue(); }
this.environmentVariables = new EnvironmentVariables(environmentVariables);
}
get value() { if (this.filters.has('base64') && !this.filters.has('env')) {
if (this.filters.isEmpty()) { return Field.base64Decode(this.valueWithoutFilters);
return this.valueWithoutFilters; }
}
if (this.filters.has('base64') && !this.filters.has('env')) { if (this.environmentVariables.has(this.valueWithoutFilters) &&
return Field.base64Decode(this.valueWithoutFilters); this.environmentVariables.get(this.valueWithoutFilters) === '') {
} return undefined;
}
if (this.environmentVariables.has(this.valueWithoutFilters) && if (!this.filters.has('base64') && this.filters.has('env')) {
this.environmentVariables.get(this.valueWithoutFilters) === '') { if (this.environmentVariables.has(this.valueWithoutFilters)) {
return undefined; return this.environmentVariables.get(this.valueWithoutFilters);
} }
return undefined;
}
if (!this.filters.has('base64') && this.filters.has('env')) { if (this.filters.has('env') && this.filters.has('base64')) {
if (this.environmentVariables.has(this.valueWithoutFilters)) { if (this.environmentVariables.has(this.valueWithoutFilters)) {
return this.environmentVariables.get(this.valueWithoutFilters); const encodedEnvironmentVariable = this.environmentVariables.get(this.valueWithoutFilters);
} return Field.base64Decode(encodedEnvironmentVariable);
return undefined; }
} return undefined;
}
if (this.filters.has('env') && this.filters.has('base64')) { return this.valueWithoutFilters;
if (this.environmentVariables.has(this.valueWithoutFilters)) { }
const encodedEnvironmentVariable = this.environmentVariables.get(this.valueWithoutFilters);
return Field.base64Decode(encodedEnvironmentVariable);
}
return undefined;
}
return this.valueWithoutFilters; static base64Decode(string) {
} return new Buffer(string, 'base64').toString('utf-8');
}
static base64Decode(string) {
return new Buffer(string, 'base64').toString('utf-8');
}
} }
module.exports = Field; module.exports = Field;

View File

@@ -1,35 +1,34 @@
class Filters { class Filters {
constructor(value) {
this.value = value;
this.delimiter = '|';
}
constructor(value) { get filters() {
this.value = value; return this.value.split(this.delimiter).slice(0, -1);
this.delimiter = '|'; }
}
get filters() { isEmpty() {
return this.value.split(this.delimiter).slice(0, -1); return !this.hasValidType() || this.value.length === 0;
} }
isEmpty() { has(filter) {
return !this.hasValidType() || this.value.length === 0; return this.filters.includes(filter);
} }
has(filter) { hasValidType() {
return this.filters.includes(filter); return (typeof this.value === 'string');
} }
hasValidType() { removeFiltersFromValue() {
return (typeof this.value === 'string'); if (this.hasValidType() === false) {
} return this.value;
}
removeFiltersFromValue() { let filtersCombined = this.filters.join(this.delimiter);
if (this.hasValidType() === false) { filtersCombined += this.filters.length >= 1 ? this.delimiter : '';
return this.value; return this.value.replace(filtersCombined, '');
} }
let filtersCombined = this.filters.join(this.delimiter);
filtersCombined += this.filters.length >= 1 ? this.delimiter : '';
return this.value.replace(filtersCombined, '');
}
} }
module.exports = Filters; module.exports = Filters;