1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 | 1× 10× 1× 1× 1× 1× 1× 1× 3× 36× 1× 1× 18× 18× 18× 18× 1× 18× 18× 18× 18× 18× 18× 18× 9× 9× 135× 9× 171× 171× 171× 171× 171× 171× 18× 18× 18× 18× 18× 18× 36× 18× 72× 72× 72× 72× 72× 72× 270× 270× 270× 270× 270× 270× 90× 270× 270× 270× 72× 18× 18× 1× 1× 1× 18× 18× 1× 18× 18× 9× 9× 1× 1× | '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 _fs = require('fs'); var _fs2 = _interopRequireDefault(_fs); var _path = require('path'); var _path2 = _interopRequireDefault(_path); var _utils = require('../utils'); var _utils2 = _interopRequireDefault(_utils); 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"); } } var ConfigManager = function () { /** * Create a new instance of the ConfigManager. * * @param api API reference object. */ /** * Api reference object. * * @type {null} */ function ConfigManager(api) { _classCallCheck(this, ConfigManager); this.api = null; this._watchedFiles = []; this.api = api; } /** * Start the config execution. */ /** * Files to watch for changes. * * @type {Array} * @private */ _createClass(ConfigManager, [{ key: 'execute', value: function execute(next) { // init the execution environment this._setupEnvironment(); // creates 'temp' folder if it does not exist this._createTempFolder(); // load manifest file, and core, project and modules configs this._loadConfigs(); // finish the config execution on the next tick process.nextTick(next); } /** * Setup the execution environment. * * This define what environment should be used. * * TODO: use the command line arguments to define the environment */ }, { key: '_setupEnvironment', value: function _setupEnvironment() { var self = this; // if (argv.NODE_ENV) { // self.api.env = argv.NODE_ENV // } else Eif (process.env.NODE_ENV) { self.api.env = process.env.NODE_ENV; } else { self.api.env = 'development'; } } /** * Unwatch all files. */ }, { key: 'unwatchAllFiles', value: function unwatchAllFiles() { var self = this; // iterate all watched files and say to the FS to stop watch the changes for (var i in self._watchedFiles) { _fs2.default.unwatchFile(self._watchedFiles[i]); } // reset the watch array self._watchedFiles = []; } /** * Start watching for changes on a file and set a function to be executed * on the file change. * * @param file File path * @param callback Callback function. */ }, { key: 'watchFileAndAct', value: function watchFileAndAct(file, callback) { var self = this; // normalise file path file = _path2.default.normalize(file); // check if file exists Iif (!_fs2.default.existsSync(file)) { throw new Error(file + ' does not exist, and cannot be watched'); } // the watch for files change only works on development mode Iif (self.api.config.general.developmentMode !== true && self._watchedFiles.indexOf(file) > 0) { return; } // push the new file to the array of watched files self._watchedFiles.push(file); // say to the FS to start watching for changes in this file with an interval of 1 seconds _fs2.default.watchFile(file, { interval: 1000 }, function (curr, prev) { if (curr.mtime > prev.mtime && self.api.config.general.developmentMode === true) { process.nextTick(function () { var cleanPath = file; // we need to replace the '/' by '\' if (process.platform === 'win32') { cleanPath = file.replace(/\//g, '\\'); } // remove file from require cache to force reload the file delete require.cache[require.resolve(cleanPath)]; // execute the callback function callback(file); }); } }); } /** * Reboot handler. * * This is executed when a config file is changed. * * @param file File path who as changed. * @private */ }, { key: '_rebootCallback', value: function _rebootCallback(file) { var self = this; self.api.log('\r\n\r\n*** rebooting due to config change (' + file + ') ***\r\n\r\n', 'info'); delete require.cache[require.resolve(file)]; self.api.commands.restart.call(self.api._self); } }, { key: '_loadConfigs', value: function _loadConfigs() { var self = this; // set config object on API self.api.config = {}; try { // read project manifest self.api.config = require(self.api.scope.rootPath + '/manifest.json'); } catch (e) { // when the project manifest doesn't exists the user is informed // and the engine instance is terminated self.api.log('Project `manifest.json` file does not exists.', 'emergency'); // finish process (we can not stop the Engine because it can not be run) process.exit(1); } // load the default config files from the Stellar core self.loadConfigDirectory(__dirname + '/../config'); // load all the configs from the modules self.api.config.modules.forEach(function (moduleName) { return self.loadConfigDirectory(self.api.scope.rootPath + '/modules/' + moduleName + '/config', true); }); // load the config files from the current universe if exists // the platform should be reloaded when the project configs changes self.loadConfigDirectory(self.api.scope.rootPath + '/config', true); } /** * Load a directory as a config repository. * * @param configPath * @param watch */ }, { key: 'loadConfigDirectory', value: function loadConfigDirectory(configPath) { var watch = arguments.length <= 1 || arguments[1] === undefined ? false : arguments[1]; var self = this; // get all files from the config folder var configFiles = _utils2.default.recursiveDirectoryGlob(configPath); var loadRetries = 0; var loadErrors = {}; for (var i = 0, limit = configFiles.length; i < limit; i++) { // get the next file to be loaded var file = configFiles[i]; try { // attempt configuration file load var localConfig = require(file); Eif (localConfig.default) { self.api.config = _utils2.default.hashMerge(self.api.config, localConfig.default, self.api); } if (localConfig[self.api.env]) { self.api.config = _utils2.default.hashMerge(self.api.config, localConfig[self.api.env], self.api); } // configuration file load success: clear retries and errors since progress // has been made loadRetries = 0; loadErrors = {}; // configuration file loaded: set watch if (watch !== false) { self.watchFileAndAct(file, self._rebootCallback.bind(self)); } } catch (error) { // error loading configuration, abort if all remaining configuration files // have been tried and failed indicating inability to progress loadErrors[file] = error.toString(); if (++loadRetries === limit - i) { throw new Error('Unable to load configurations, errors: ' + JSON.stringify(loadErrors)); } // adjust configuration files list: remove and push failed configuration to // the end of the list and continue with next file at same index configFiles.push(configFiles.splice(i--, 1)[0]); } } } /** * Creates the 'temp' folder if it does not exist. * * This folder is used to store the log files. * * @private */ }, { key: '_createTempFolder', value: function _createTempFolder() { var self = this; Iif (!_utils2.default.directoryExists(self.api.scope.rootPath + '/temp')) { _utils2.default.createFolder(self.api.scope.rootPath + '/temp'); } } }]); return ConfigManager; }(); /** * This initializer loads all app configs to the current running instance. */ var _class = function () { function _class() { _classCallCheck(this, _class); this.loadPriority = 0; } /** * Load priority. * * This initializer needs to be loaded first of all * others. * * @type {number} */ _createClass(_class, [{ key: 'load', /** * Load satellite function. * * @param api API object reference. * @param next Callback function. */ value: function load(api, next) { // put the config instance available on the API object api.configs = new ConfigManager(api); // start the config manager execution api.configs.execute(next); } /** * Start satellite function. * * @param api Api object reference. * @param next Callback function. */ }, { key: 'start', value: function start(api, next) { // print out the current environment api.log('environment: ' + api.env, 'notice'); // finish the satellite start next(); } }]); return _class; }(); exports.default = _class; //# sourceMappingURL=config.js.map |