lore
Version:
Convention-driven framework for building React-Redux applications
155 lines (113 loc) • 4.83 kB
JavaScript
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _lodash = require('lodash');
var _lodash2 = _interopRequireDefault(_lodash);
var _loader = require('../loader');
var _loader2 = _interopRequireDefault(_loader);
var _getVersionAndDependencyInfo = require('./getVersionAndDependencyInfo');
var _getVersionAndDependencyInfo2 = _interopRequireDefault(_getVersionAndDependencyInfo);
var _getLogger = require('./getLogger');
var _getLogger2 = _interopRequireDefault(_getLogger);
var _getEnvironment = require('./getEnvironment');
var _getEnvironment2 = _interopRequireDefault(_getEnvironment);
var _getHooks = require('./getHooks');
var _getHooks2 = _interopRequireDefault(_getHooks);
var _getInitializers = require('./getInitializers');
var _getInitializers2 = _interopRequireDefault(_getInitializers);
var _sortHooksByLoadOrder = require('./sortHooksByLoadOrder');
var _sortHooksByLoadOrder2 = _interopRequireDefault(_sortHooksByLoadOrder);
var _getConfig = require('./getConfig');
var _getConfig2 = _interopRequireDefault(_getConfig);
var _validateReactConfig = require('./validateReactConfig');
var _validateReactConfig2 = _interopRequireDefault(_validateReactConfig);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* The Lore class constructor. Exposes the following fields for use:
*
* lore.version
* lore.majorVersion
* lore.minorVersion
* lore.patchVersion
* lore.dependencies
* lore.config
* lore.loader
* lore.log
* lore.hooks
*
*/
/* eslint no-param-reassign: "warn" */
var Lore = function Lore() {
this.log = (0, _getLogger2.default)();
var versionInfo = (0, _getVersionAndDependencyInfo2.default)();
this.version = versionInfo.version;
this.majorVersion = versionInfo.majorVersion;
this.minorVersion = versionInfo.minorVersion;
this.patchVersion = versionInfo.patchVersion;
this.dependencies = versionInfo.dependencies;
this.utils = {};
};
_lodash2.default.extend(Lore.prototype, {
/**
* Builds the application, which involves setting the environment, composing the final
* configuration, loading the hooks, and executing any initializers.
*
* @param {Object} configOverride The configuration passed into lore.build(configOverride)
*/
build: function build(configOverride) {
configOverride = configOverride || {};
// Set the environment first, in case any parts of the build process
// change their behavior based on the environment
this.environment = (0, _getEnvironment2.default)({
environment: configOverride.environment
});
// The loader needs to be setup before the hooks as they will need to use
// it to set up the project files
this.loader = (0, _loader2.default)({
environment: this.environment
});
// Next we need to load the hooks before the config, as the hooks contain
// the defaults we need to build the final configuration for the app
this.hooks = (0, _getHooks2.default)(configOverride.hooks);
// Generate the final config from the combination of the overrides passed
// into the app, the default config (calculated from the hooks), and the
// user config for the project (loaded and compiled inside this function)
this.config = (0, _getConfig2.default)(configOverride, this.hooks, this.loader);
// Now that we have the final config, we can load the hooks, as their behavior
// is dependant on the final configuration for the application
(0, _sortHooksByLoadOrder2.default)(this.hooks, this.log).forEach(function (hook) {
// this.log.silly('Loading hook: ' + hook.id);
hook.load(this);
// this.log.verbose(hook.id, 'hook loaded successfully.');
}.bind(this));
// Get initializers and run them
this.initializers = (0, _getInitializers2.default)();
if (this.initializers.length > 0) {
this.initializers.forEach(function (initializer) {
initializer();
});
}
// this.log.verbose('All hooks were loaded successfully.');
},
/**
* Builds the application and mounts it to the DOM
*
* @param {Object} configOverride The configuration passed into lore.build(configOverride)
*/
summon: function summon(configOverride) {
this.build(configOverride);
// this.log.verbose('Mounting app...');
// Get the React config, so we can learn how to construct the Root component
// and mount the application
var react = this.config.react;
// Validate the React config has all required methods
(0, _validateReactConfig2.default)(react);
// Get the Root component we should mount to the DOM
var Root = react.getRootComponent(this);
// Mount the app!
react.mount(Root, this);
}
});
exports.default = Lore;
module.exports = exports['default'];