UNPKG

deep-package-manager

Version:
91 lines (72 loc) 1.69 kB
/** * Created by AlexanderC on 5/25/15. */ 'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.Config = undefined; var _jsonfile = require('jsonfile'); var _jsonfile2 = _interopRequireDefault(_jsonfile); var _joi = require('joi'); var _joi2 = _interopRequireDefault(_joi); var _deepkg = require('./deepkg.schema'); var _deepkg2 = _interopRequireDefault(_deepkg); var _InvalidConfigException = require('./Exception/InvalidConfigException'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } /** * Microservice configuration loader */ class Config { /** * @param {Object} rawConfig */ constructor(rawConfig = {}) { this._rawConfig = rawConfig; this._parsedObject = _joi2.default.validate(this._rawConfig, _deepkg2.default); } /** * @returns {Object} */ get rawConfig() { return this._rawConfig; } /** * Validates raw object * * @returns {Boolean} */ get valid() { return !this.error; } /** * Retrieve parse error if available * * @returns {String} */ get error() { return this._parsedObject.error; } /** * Extracts parsed configuration * * @returns {Object} */ extract() { if (!this.valid) { throw new _InvalidConfigException.InvalidConfigException(this.error); } return this._parsedObject.value; } /** * Read microservice configuration from json file * * @param {String} file * @returns {Config} */ static createFromJsonFile(file) { let rawConfig = _jsonfile2.default.readFileSync(file); return new Config(rawConfig); } } exports.Config = Config;