UNPKG

deep-package-manager

Version:
262 lines (211 loc) 5.32 kB
/** * Created by AlexanderC on 5/25/15. */ 'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.Instance = undefined; var _Config = require('./Config'); var _Parameters = require('./Parameters'); var _InvalidArgumentException = require('../Exception/InvalidArgumentException'); var _Autoload = require('./Metadata/Autoload'); var _ResourceCollection = require('./Metadata/ResourceCollection'); var _Compiler = require('../Compilation/Compiler'); var _PostDeployHook = require('./PostDeployHook'); var _PreDeployHook = require('./PreDeployHook'); var _InitHook = require('./InitHook'); var _FrontendEngine = require('./FrontendEngine'); var _path = require('path'); var _path2 = _interopRequireDefault(_path); var _PostRootFetchHook = require('./PostRootFetchHook'); var _PostInstallHook = require('./PostInstallHook'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } /** * Microservice instance */ class Instance { /** * @param {Config} config * @param {Parameters} parameters * @param {String} basePath */ constructor(config, parameters, basePath) { if (!(config instanceof _Config.Config)) { throw new _InvalidArgumentException.InvalidArgumentException(config, _Config.Config); } if (!(parameters instanceof _Parameters.Parameters)) { throw new _InvalidArgumentException.InvalidArgumentException(parameters, _Parameters.Parameters); } this._basePath = _path2.default.normalize(basePath); this._config = config.extract(); this._parameters = parameters.extract(); this._autoload = new _Autoload.Autoload(this._config.autoload, this._basePath); this._resources = null; this._overwriteRolePolicy = (type, policy) => {}; this._preDeployHook = new _PreDeployHook.PreDeployHook(this); this._postDeployHook = new _PostDeployHook.PostDeployHook(this); this._initHook = new _InitHook.InitHook(this); this._postRootFetchHook = new _PostRootFetchHook.PostRootFetchHook(this); this._postInstallHook = new _PostInstallHook.PostInstallHook(this); this._property = null; } /** * @param {Function} cb * * @returns {Instance|*} */ overwriteRolePolicyCb(cb) { this._overwriteRolePolicy = cb; return this; } /** * @param {String} type * @param {Policy|*} policy */ overwriteRolePolicy(type, policy) { this._overwriteRolePolicy(type, policy); } /** * @returns {Property|Instance|null} */ get property() { return this._property; } /** * @param {Property|Instance|null} property */ set property(property) { this._property = property; } /** * @returns {String} */ static get CONFIG_FILE() { return 'deepkg.json'; } /** * @returns {String} */ static get PARAMS_FILE() { return 'parameters.json'; } /** * @returns {String} */ static get RESOURCES_FILE() { return 'resources.json'; } /** * @param {String} basePath * @returns {Instance} */ static create(basePath) { basePath = _path2.default.normalize(basePath); let configFile = _path2.default.join(basePath, Instance.CONFIG_FILE); let parametersFile = _path2.default.join(basePath, Instance.PARAMS_FILE); return new Instance(_Config.Config.createFromJsonFile(configFile), _Parameters.Parameters.createFromJsonFile(parametersFile), basePath); } /** * @returns {FrontendEngine} */ get frontendEngine() { return _FrontendEngine.FrontendEngine.create(this); } /** * @returns {String} */ get identifier() { return this._config.identifier; } /** * @returns {String} */ get version() { return this._config.version; } /** * * @returns {String} */ get basePath() { return this._basePath; } /** * Compiles dependencies recursively * * @param {Boolean} lambdasOnly */ compile(lambdasOnly = false) { if (!lambdasOnly) { _Compiler.Compiler.compile(this); } this._config.lambdas = _Compiler.Compiler.buildLambdas(this); } /** * @returns {Boolean} */ get isRoot() { return !!this._config.propertyRoot; } /** * Retrieve microservice configuration * * @returns {Config} */ get config() { return this._config; } /** * @returns {Parameters} */ get parameters() { return this._parameters; } /** * @returns {Autoload} */ get autoload() { return this._autoload; } /** * @returns {Function} */ get initHook() { return this._initHook.getHook(); } /** * @returns {Function} */ get preDeployHook() { return this._preDeployHook.getHook(); } /** * @returns {Function} */ get postDeployHook() { return this._postDeployHook.getHook(); } /** * @returns {Function} */ get postInstallHook() { return this._postInstallHook.getHook(); } /** * @returns {Function} */ get postRootFetchHook() { return this._postRootFetchHook.getHook(); } /** * @returns {ResourceCollection} */ get resources() { if (this._resources === null) { this._resources = _ResourceCollection.ResourceCollection.create(this); } return this._resources; } } exports.Instance = Instance;