deep-package-manager
Version:
DEEP Package Manager
160 lines (125 loc) • 3.42 kB
JavaScript
/**
* Created by AlexanderC on 6/17/15.
*/
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Parameters = undefined;
var _jsonfile = require('jsonfile');
var _jsonfile2 = _interopRequireDefault(_jsonfile);
var _joi = require('joi');
var _joi2 = _interopRequireDefault(_joi);
var _path = require('path');
var _path2 = _interopRequireDefault(_path);
var _parameters = require('./parameters.schema');
var _parameters2 = _interopRequireDefault(_parameters);
var _InvalidConfigException = require('./Exception/InvalidConfigException');
var _fs = require('fs');
var _fs2 = _interopRequireDefault(_fs);
var _Extractor = require('../Parameters/Extractor');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* User defined parameters
*/
class Parameters {
/**
* @param {Object} rawParameters
* @param {String} workingDir
*/
constructor(rawParameters = {}, workingDir = null) {
this._rawParameters = rawParameters;
this._workingDir = workingDir;
this._parsedObject = _joi2.default.validate(this._rawParameters, _parameters2.default);
this._filledObject = null;
}
/**
* @returns {Object}
*/
get rawParameters() {
return this._rawParameters;
}
/**
* 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
*
* @param {String} type
* @returns {Object}
*/
extract(type = null) {
if (!this.valid) {
throw new _InvalidConfigException.InvalidConfigException(this.error);
}
if (!this._filledObject) {
this._fillParametersObject();
}
return type ? this._filledObject[type] : this._filledObject;
}
/**
* @private
*/
_fillParametersObject() {
if (!this.valid) {
return;
}
let paramsObj = this._parsedObject.value;
if (paramsObj.hasOwnProperty(Parameters.GLOBALS)) {
paramsObj[Parameters.GLOBALS] = new _Extractor.Extractor(paramsObj[Parameters.GLOBALS]).extractOptimal(this._workingDir, Parameters.GLOBALS);
}
paramsObj[Parameters.FRONTEND] = new _Extractor.Extractor(paramsObj[Parameters.FRONTEND]).extractOptimal(this._workingDir, Parameters.FRONTEND);
paramsObj[Parameters.BACKEND] = new _Extractor.Extractor(paramsObj[Parameters.BACKEND]).extractOptimal(this._workingDir, Parameters.BACKEND);
this._filledObject = paramsObj;
// @todo: move this?
_Extractor.Extractor.dumpParameters(this._workingDir, paramsObj, true);
}
/**
* @returns {String}
*/
static get GLOBALS() {
return 'globals';
}
/**
* @returns {String}
*/
static get FRONTEND() {
return 'frontend';
}
/**
* @returns {String}
*/
static get BACKEND() {
return 'backend';
}
/**
* Read microservice configuration from json file
*
* @param {String} file
* @returns {Parameters}
*/
static createFromJsonFile(file) {
let rawParameters = {
frontend: {},
backend: {}
};
if (_fs2.default.existsSync(file)) {
rawParameters = _jsonfile2.default.readFileSync(file);
}
return new Parameters(rawParameters, _path2.default.dirname(file));
}
}
exports.Parameters = Parameters;