WIP new Configuration class
This commit is contained in:
parent
dd2a6258d5
commit
cfaff61abf
104
core/config.js
104
core/config.js
|
@ -8,12 +8,114 @@ const DefaultConfig = require('./config_default');
|
|||
// deps
|
||||
const paths = require('path');
|
||||
const async = require('async');
|
||||
const _ = require('lodash');
|
||||
const assert = require('assert');
|
||||
|
||||
const _ = require('lodash');
|
||||
const reduceDeep = require('deepdash/getReduceDeep')(_);
|
||||
|
||||
exports.init = init;
|
||||
exports.getDefaultPath = getDefaultPath;
|
||||
|
||||
class Configuration {
|
||||
constructor(path, options) {
|
||||
this.current = {};
|
||||
}
|
||||
|
||||
static create(path, options, cb) {
|
||||
|
||||
}
|
||||
|
||||
get() {
|
||||
return this.current;
|
||||
}
|
||||
|
||||
_convertTo(value, type) {
|
||||
switch (type) {
|
||||
case 'bool' :
|
||||
case 'boolean' :
|
||||
value = 'true' === value.toLowerCase();
|
||||
break;
|
||||
|
||||
case 'number' :
|
||||
{
|
||||
const num = parseInt(value);
|
||||
if (!isNaN(num)) {
|
||||
value = num;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case 'object' :
|
||||
try {
|
||||
value = JSON.parse(value);
|
||||
} catch(e) { }
|
||||
break;
|
||||
|
||||
case 'date' :
|
||||
case 'time' :
|
||||
case 'datetime' :
|
||||
case 'timestamp' :
|
||||
{
|
||||
const m = moment(value);
|
||||
if (m.isValid()) {
|
||||
value = m;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case 'regex' :
|
||||
// :TODO: What flags to use, etc.?
|
||||
break;
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
_resolveEnvironmentVariable(spec) {
|
||||
const [prefix, varName, type, array] = spec.split(':');
|
||||
if (!varName) {
|
||||
return;
|
||||
}
|
||||
|
||||
let value = process.env[varName];
|
||||
if (!value) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ('array' === array) {
|
||||
value = value.split(',').map(v => this._convertTo(v, type));
|
||||
} else {
|
||||
value = this._convertTo(value, type);
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
_resolveCurrent() {
|
||||
reduceDeep(
|
||||
this.current,
|
||||
(acc, value, key, parent, ctx) => {
|
||||
// resolve self references; there may be a better way...
|
||||
if (_.isString(value) && '@' === value.charAt(0)) {
|
||||
if (value.startsWith('@reference:')) {
|
||||
value = value.slice(11);
|
||||
const ref = _.get(acc, value);
|
||||
if (ref) {
|
||||
_.set(acc, ctx.path, ref);
|
||||
}
|
||||
} else if (value.startsWith('@environment:')) {
|
||||
value = this._resolveEnvironmentVariable(value);
|
||||
if (!_.isUndefined(value)) {
|
||||
_.set(acc, ctx.path, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
return acc;
|
||||
}
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
let currentConfiguration = {};
|
||||
|
||||
function hasMessageConferenceAndArea(config) {
|
||||
|
|
|
@ -58,7 +58,8 @@
|
|||
"uuid-parse": "1.1.0",
|
||||
"ws": "^7.3.0",
|
||||
"xxhash": "^0.3.0",
|
||||
"yazl": "^2.5.1"
|
||||
"yazl": "^2.5.1",
|
||||
"deepdash" : "^5.1.1"
|
||||
},
|
||||
"devDependencies": {},
|
||||
"engines": {
|
||||
|
|
13
yarn.lock
13
yarn.lock
|
@ -372,6 +372,14 @@ deep-extend@^0.6.0:
|
|||
resolved "https://registry.yarnpkg.com/deep-extend/-/deep-extend-0.6.0.tgz#c4fa7c95404a17a9c3e8ca7e1537312b736330ac"
|
||||
integrity sha512-LOHxIOaPYdHlJRtCQfDIVZtfw/ufM8+rVj649RIHzcm/vGwQRXFt6OPqIFWsm2XEMrNIEtWR64sY1LEKD2vAOA==
|
||||
|
||||
deepdash@^5.1.1:
|
||||
version "5.1.1"
|
||||
resolved "https://registry.yarnpkg.com/deepdash/-/deepdash-5.1.1.tgz#dcf68b9e15085b5df18bdb4011723790e0c40430"
|
||||
integrity sha512-esz3pjQJaeYO4z74seqCMrOYUsAAdrhO3KJuEnGEaxTGbSy8VGOWn7jTU2J3nR5WDyNpS5/hse3m/hdM1/8ZWA==
|
||||
dependencies:
|
||||
lodash "^4.17.15"
|
||||
lodash-es "^4.17.15"
|
||||
|
||||
define-property@^0.2.5:
|
||||
version "0.2.5"
|
||||
resolved "https://registry.yarnpkg.com/define-property/-/define-property-0.2.5.tgz#c35b1ef918ec3c990f9a5bc57be04aacec5c8116"
|
||||
|
@ -978,6 +986,11 @@ later@1.2.0:
|
|||
resolved "https://registry.yarnpkg.com/later/-/later-1.2.0.tgz#f2cf6c4dd7956dd2f520adf0329836e9876bad0f"
|
||||
integrity sha1-8s9sTdeVbdL1IK3wMpg26YdrrQ8=
|
||||
|
||||
lodash-es@^4.17.15:
|
||||
version "4.17.15"
|
||||
resolved "https://registry.yarnpkg.com/lodash-es/-/lodash-es-4.17.15.tgz#21bd96839354412f23d7a10340e5eac6ee455d78"
|
||||
integrity sha512-rlrc3yU3+JNOpZ9zj5pQtxnx2THmvRykwL4Xlxoa8I9lHBlVbbyPhgyPMioxVZ4NqyxaVVtaJnzsyOidQIhyyQ==
|
||||
|
||||
lodash@^4.17.15:
|
||||
version "4.17.15"
|
||||
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.15.tgz#b447f6670a0455bbfeedd11392eff330ea097548"
|
||||
|
|
Loading…
Reference in New Issue