Changeset View
Changeset View
Standalone View
Standalone View
lib/app/config-manager.js
const dotProp = require("dot-prop"); | const dotProp = require("dot-prop"); | ||||
const merge = require("merge"); | const merge = require("merge"); | ||||
module.exports = class ConfigManager { | module.exports = class ConfigManager { | ||||
constructor() { | constructor() { | ||||
this.DEFAULT_CONFIG = {}; | this.DEFAULT_CONFIG = {}; | ||||
this.CUSTOM_CONFIG = {}; | this.CUSTOM_CONFIG = {}; | ||||
this.isLocked = false; | this.isLocked = false; | ||||
} | } | ||||
setDefault(key, value) { | setDefault(key, value) { | ||||
this._setGivenConfig(this.DEFAULT_CONFIG, key, value); | this._setGivenConfig(this.DEFAULT_CONFIG, key, value); | ||||
} | } | ||||
getDefaultConfig(key) { | getDefaultConfig(key) { | ||||
return dotProp.get(this.DEFAULT_CONFIG, key); | return dotProp.get(this.DEFAULT_CONFIG, key); | ||||
} | } | ||||
set(key, value) { | set(key, value) { | ||||
this._setGivenConfig(this.CUSTOM_CONFIG, key, value); | this._setGivenConfig(this.CUSTOM_CONFIG, key, value); | ||||
} | } | ||||
_setGivenConfig(config, key, value) { | _setGivenConfig(config, key, value) { | ||||
this._warnIfLocked(); | this._warnIfLocked(); | ||||
dotProp.set(config, key, value); | dotProp.set(config, key, value); | ||||
} | } | ||||
_warnIfLocked() { | _warnIfLocked() { | ||||
if (this.isLocked) { | if (this.isLocked) { | ||||
console.warn( | console.warn( | ||||
"Warning: " + | "Warning: " + | ||||
"you shouldn't change config after ConfigManager was locked" | "you shouldn't change config after ConfigManager was locked" | ||||
); | ); | ||||
} | } | ||||
} | } | ||||
setRoot(params) { | setRoot(params) { | ||||
this._warnIfLocked(); | this._warnIfLocked(); | ||||
merge.recursive(this.CUSTOM_CONFIG, params); | merge.recursive(this.CUSTOM_CONFIG, params); | ||||
} | } | ||||
get(key) { | get(key) { | ||||
return dotProp.get( | return dotProp.get( | ||||
merge.recursive(true, this.DEFAULT_CONFIG, this.CUSTOM_CONFIG), | merge.recursive(true, this.DEFAULT_CONFIG, this.CUSTOM_CONFIG), | ||||
key | key | ||||
); | ); | ||||
} | } | ||||
lock() { | lock() { | ||||
this.isLocked = true; | this.isLocked = true; | ||||
} | } | ||||
}; | }; |