UNPKG

5.22 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _imaHelpers = require('ima-helpers');
8
9var _imaHelpers2 = _interopRequireDefault(_imaHelpers);
10
11var _namespace = require('./namespace');
12
13var _namespace2 = _interopRequireDefault(_namespace);
14
15var _ObjectContainer = require('./ObjectContainer');
16
17var _ObjectContainer2 = _interopRequireDefault(_ObjectContainer);
18
19var _Router = require('./router/Router');
20
21var _Router2 = _interopRequireDefault(_Router);
22
23function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
24
25_namespace2.default.namespace('ima');
26
27/**
28 * Environment name value in the production environment.
29 *
30 * @const
31 * @type {string}
32 */
33const PRODUCTION_ENVIRONMENT = 'prod';
34
35/**
36 * Application bootstrap used to initialize the environment and the application
37 * itself.
38 */
39class Bootstrap {
40 /**
41 * Initializes the bootstrap.
42 *
43 * @param {ObjectContainer} oc The application's object container to use
44 * for managing dependencies.
45 */
46 constructor(oc) {
47 /**
48 * The object container used to manage dependencies.
49 *
50 * @type {ObjectContainer}
51 */
52 this._oc = oc;
53
54 /**
55 * Application configuration.
56 *
57 * @type {Object<string, *>}
58 */
59 this._config = {};
60 }
61
62 /**
63 * Initializes the application by running the bootstrap sequence. The
64 * sequence initializes the components of the application in the following
65 * order:
66 * - application settings
67 * - constants, service providers and class dependencies configuration
68 * - services
69 * - UI components
70 * - routing
71 *
72 * @param {Object<string, *>} config The application environment
73 * configuration for the current environment.
74 */
75 run(config) {
76 this._config = config;
77
78 this._initSettings();
79 this._bindDependencies();
80 this._initServices();
81 this._initRoutes();
82 }
83
84 /**
85 * Initializes the application settings. The method loads the settings for
86 * all environments and then pics the settings for the current environment.
87 *
88 * The method also handles using the values in the production environment
89 * as default values for configuration items in other environments.
90 */
91 _initSettings() {
92 let currentApplicationSettings = {};
93
94 let plugins = this._config.plugins.concat([this._config]);
95
96 plugins.filter(plugin => typeof plugin.initSettings === 'function').forEach(plugin => {
97 let allPluginSettings = plugin.initSettings(_namespace2.default, this._oc, this._config.settings);
98 let environmentPluginSetting = this._getEnvironmentSetting(allPluginSettings);
99
100 _imaHelpers2.default.assignRecursively(currentApplicationSettings, environmentPluginSetting);
101 });
102
103 this._config.bind = Object.assign(this._config.bind || {}, currentApplicationSettings, this._config.settings);
104 }
105
106 /**
107 * Returns setting for current environment where base values are from production
108 * environment and other environments override base values.
109 *
110 * @return {Object<string, *>}
111 */
112 _getEnvironmentSetting(allSettings) {
113 let environment = this._config.settings.$Env;
114 let environmentSetting = allSettings[environment] || {};
115
116 if (environment !== PRODUCTION_ENVIRONMENT) {
117 let productionSettings = allSettings[PRODUCTION_ENVIRONMENT];
118 _imaHelpers2.default.assignRecursively(productionSettings, environmentSetting);
119 environmentSetting = productionSettings;
120 }
121
122 return environmentSetting;
123 }
124
125 /**
126 * Binds the constants, service providers and class dependencies to the
127 * object container.
128 */
129 _bindDependencies() {
130 this._oc.setBindingState(_ObjectContainer2.default.IMA_BINDING_STATE);
131 this._config.initBindIma(_namespace2.default, this._oc, this._config.bind);
132
133 this._oc.setBindingState(_ObjectContainer2.default.PLUGIN_BINDING_STATE);
134 this._config.plugins.filter(plugin => typeof plugin.initBind === 'function').forEach(plugin => {
135 plugin.initBind(_namespace2.default, this._oc, this._config.bind);
136 });
137
138 this._oc.setBindingState(_ObjectContainer2.default.APP_BINDING_STATE);
139 this._config.initBindApp(_namespace2.default, this._oc, this._config.bind);
140 }
141
142 /**
143 * Initializes the routes.
144 */
145 _initRoutes() {
146 let router = this._oc.get(_Router2.default);
147 this._config.initRoutes(_namespace2.default, this._oc, this._config.routes, router);
148 }
149
150 /**
151 * Initializes the basic application services.
152 */
153 _initServices() {
154 this._config.initServicesIma(_namespace2.default, this._oc, this._config.services);
155
156 this._config.plugins.filter(plugin => typeof plugin.initServices === 'function').forEach(plugin => {
157 plugin.initServices(_namespace2.default, this._oc, this._config.services);
158 });
159
160 this._config.initServicesApp(_namespace2.default, this._oc, this._config.services);
161 }
162}
163
164exports.default = Bootstrap;
165_namespace2.default.ima.Bootstrap = Bootstrap;
166
167typeof $IMA !== 'undefined' && $IMA !== null && $IMA.Loader && $IMA.Loader.register('ima/Bootstrap', [], function (_export, _context) {
168 'use strict';
169 return {
170 setters: [],
171 execute: function () {
172 _export('default', exports.default);
173 }
174 };
175});