'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; Eif ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { Eif (protoProps) defineProperties(Constructor.prototype, protoProps); Iif (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _path = require('path');
var _path2 = _interopRequireDefault(_path);
var _fs = require('fs');
var _fs2 = _interopRequireDefault(_fs);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { Iif (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
/**
* Config Class
*/
var Config = function () {
function Config() {
_classCallCheck(this, Config);
this._config = {};
}
/**
* Internal Config Storage
* @type {Object}
*/
_createClass(Config, [{
key: 'get',
/**
* Retrieve a config value via dot-notation
*
* @param {String} key dot-notation representation of config path
* @param {Mixed} defaultVal the default value to be used if no config param is set at this location
* @return {Mixed}
*/
value: function get(key, defaultVal) {
var i = void 0,
keys = key.split('.'),
curs = this._config;
for (i = 0; i < keys.length; i++) {
curs = curs[keys[i]];
}
return curs || defaultVal;
}
/**
* Set a config value via dot-notation
*
* @param {String} key dot-notation representatino of config path
* @param {Config} val this class
*/
}, {
key: 'set',
value: function set(key, val) {
var i = void 0,
keys = key.split('.'),
curs = this._config;
for (i = 0; i < keys.length - 1; i++) {
var _key = keys[i];
Eif (!curs[_key]) {
curs[_key] = {};
}
curs = curs[_key];
}
curs[keys[keys.length - 1]] = val;
return this;
}
/**
* Load configs from a given folder
* using their filenames as a key
*
* @param {String} __dir path to directory
* @return {Config}
*/
}, {
key: 'loadFromFolder',
value: function loadFromFolder(__dir) {
var _this = this;
var filenames = _fs2.default.readdirSync(__dir);
filenames.forEach(function (file) {
var name = void 0,
contents = void 0;
name = file.replace('.jsx', '').replace('.js', '');
if (name !== 'index') {
contents = require(_path2.default.join(__dir, file));
_this.set(name, contents.default || contents);
}
});
return this;
}
/**
* Load configs from a raw object
* @param {Object} obj configuration object
* @return {Config}
*/
}, {
key: 'loadFromObj',
value: function loadFromObj(obj) {
this._config = obj;
return this;
}
}]);
return Config;
}();
exports.default = new Config(); |