2019-05-07 13:12:54 +00:00
|
|
|
// Copyright IBM Corp. 2014,2019. All Rights Reserved.
|
2016-05-06 04:52:36 +00:00
|
|
|
// Node module: loopback-boot
|
|
|
|
// This file is licensed under the MIT License.
|
|
|
|
// License text available at https://opensource.org/licenses/MIT
|
|
|
|
|
2017-05-08 21:48:48 +00:00
|
|
|
'use strict';
|
|
|
|
|
2016-08-04 16:05:24 +00:00
|
|
|
// Strong globalize
|
2019-11-17 19:19:25 +00:00
|
|
|
const g = require('./lib/globalize');
|
2016-08-04 16:05:24 +00:00
|
|
|
|
2019-11-17 19:19:25 +00:00
|
|
|
const PluginBase = require('./lib/plugin-base');
|
|
|
|
const Bootstrapper = require('./lib/bootstrapper');
|
|
|
|
const addInstructionsToBrowserify = require('./lib/bundler');
|
2014-05-23 14:39:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize an application from an options object or
|
|
|
|
* a set of JSON and JavaScript files.
|
|
|
|
*
|
2014-06-06 17:11:46 +00:00
|
|
|
* > **NOTE**: This module is primarily intended for use with LoopBack 2.0.
|
|
|
|
* It _does_ work with LoopBack 1.x applications, but
|
|
|
|
* none of the LoopBack 1.x examples or generated code (scaffolding) use it.
|
2014-06-16 13:15:36 +00:00
|
|
|
*
|
2014-05-23 14:39:34 +00:00
|
|
|
* This function takes an optional argument that is either a string
|
|
|
|
* or an object.
|
|
|
|
*
|
|
|
|
* If the argument is a string, then it sets the application root directory
|
|
|
|
* based on the string value. Then it:
|
|
|
|
*
|
|
|
|
* 1. Creates DataSources from the `datasources.json` file in the application
|
|
|
|
* root directory.
|
|
|
|
*
|
2014-07-15 09:05:42 +00:00
|
|
|
* 2. Configures Models from the `model-config.json` file in the application
|
2014-05-23 14:39:34 +00:00
|
|
|
* root directory.
|
|
|
|
*
|
2014-09-29 14:46:13 +00:00
|
|
|
* 3. Configures the LoopBack Application object from the `config.json` file
|
|
|
|
* in the application root directory. These properties can be accessed
|
2014-10-09 14:34:39 +00:00
|
|
|
* using `app.get('propname')`.
|
2014-09-29 14:46:13 +00:00
|
|
|
*
|
2014-06-09 12:43:44 +00:00
|
|
|
* If the argument is an object, then it looks for `models`, `dataSources`,
|
2014-10-09 14:34:39 +00:00
|
|
|
* 'config', `modelsRootDir`, `dsRootDir`, `appConfigRootDir` and `appRootDir`
|
2014-09-29 14:46:13 +00:00
|
|
|
* properties of the object.
|
2014-10-09 14:34:39 +00:00
|
|
|
*
|
2014-05-23 14:39:34 +00:00
|
|
|
* If the object has no `appRootDir` property then it sets the current working
|
|
|
|
* directory as the application root directory.
|
2014-10-09 14:34:39 +00:00
|
|
|
*
|
|
|
|
* The execution environment, {env}, is established from, in order,
|
|
|
|
* - `options.env`
|
|
|
|
* - `process.env.NODE_ENV`,
|
|
|
|
* - the literal `development`.
|
2014-09-29 14:46:13 +00:00
|
|
|
*
|
2014-05-23 14:39:34 +00:00
|
|
|
* Then it:
|
|
|
|
*
|
2014-10-09 14:34:39 +00:00
|
|
|
* 1. Creates DataSources from the `options.dataSources` object, if provided;
|
|
|
|
* otherwise, it searches for the files
|
|
|
|
* - `datasources.json`,
|
|
|
|
* - `datasources.local.js` or `datasources.local.json` (only one),
|
|
|
|
* - `datasources.{env}.js` or `datasources.{env}.json` (only one)
|
|
|
|
*
|
2014-09-29 14:46:13 +00:00
|
|
|
* in the directory designated by 'options.dsRootDir', if present, or the
|
|
|
|
* application root directory. It merges the data source definitions from
|
|
|
|
* the files found.
|
|
|
|
*
|
2014-10-09 14:34:39 +00:00
|
|
|
* 2. Creates Models from the `options.models` object, if provided;
|
2014-09-29 14:46:13 +00:00
|
|
|
* otherwise, it searches for the files
|
2014-10-09 14:34:39 +00:00
|
|
|
* - `model-config.json`,
|
|
|
|
* - `model-config.local.js` or `model-config.local.json` (only one),
|
|
|
|
* - `model-config.{env}.js` or `model-config.{env}.json` (only one)
|
|
|
|
*
|
2014-09-29 14:46:13 +00:00
|
|
|
* in the directory designated by 'options.modelsRootDir', if present, or
|
|
|
|
* the application root directory. It merges the model definitions from the
|
|
|
|
* files found.
|
2014-05-23 14:39:34 +00:00
|
|
|
*
|
2014-10-09 14:34:39 +00:00
|
|
|
* 3. Configures the Application object from the `options.config` object,
|
2014-09-29 14:46:13 +00:00
|
|
|
* if provided;
|
|
|
|
* otherwise, it searches for the files
|
2014-10-09 14:34:39 +00:00
|
|
|
* - `config.json`,
|
|
|
|
* - `config.local.js` or `config.local.json` (only one),
|
|
|
|
* - `config.{env}.js` or `config.{env}.json` (only one)
|
|
|
|
*
|
2014-09-29 14:46:13 +00:00
|
|
|
* in the directory designated by 'options.appConfigRootDir', if present, or
|
|
|
|
* the application root directory. It merges the properties from the files
|
|
|
|
* found.
|
2014-05-23 14:39:34 +00:00
|
|
|
*
|
2014-06-09 12:43:44 +00:00
|
|
|
* In both cases, the function loads JavaScript files in the
|
|
|
|
* `/boot` subdirectory of the application root directory with `require()`.
|
|
|
|
*
|
|
|
|
* **NOTE:** The version 2.0 of loopback-boot changed the way how models
|
2014-07-15 09:05:42 +00:00
|
|
|
* are created. The `model-config.json` file contains only configuration
|
|
|
|
* options like dataSource and extra relations. To define a model,
|
|
|
|
* create a per-model JSON file in `models/` directory.
|
2014-05-23 14:39:34 +00:00
|
|
|
*
|
2014-06-16 13:15:36 +00:00
|
|
|
* **NOTE:** Mixing `bootLoopBackApp(app, bootConfig)` and
|
|
|
|
* `app.model(name, modelConfig)` in multiple
|
2014-06-06 17:11:46 +00:00
|
|
|
* files may result in models being undefined due to race conditions.
|
|
|
|
* To avoid this when using `bootLoopBackApp()` make sure all models are passed
|
2014-05-23 14:39:34 +00:00
|
|
|
* as part of the `models` definition.
|
|
|
|
*
|
|
|
|
* Throws an error if the config object is not valid or if boot fails.
|
|
|
|
*
|
|
|
|
* @param app LoopBack application created by `loopback()`.
|
|
|
|
* @options {String|Object} options Boot options; If String, this is
|
|
|
|
* the application root directory; if object, has below properties.
|
2014-06-09 12:43:44 +00:00
|
|
|
* @property {String} [appRootDir] Directory to use when loading JSON and
|
|
|
|
* JavaScript files.
|
2014-05-23 14:39:34 +00:00
|
|
|
* Defaults to the current directory (`process.cwd()`).
|
2014-10-09 14:34:39 +00:00
|
|
|
* @property {String} [appConfigRootDir] Directory to use when loading
|
|
|
|
* `config.json`. Defaults to `appRootDir`.
|
2014-06-09 12:43:44 +00:00
|
|
|
* @property {Object} [models] Object containing `Model` configurations.
|
2015-05-05 08:56:51 +00:00
|
|
|
* @property {Array} [modelDefinitions] List of model definitions to use.
|
|
|
|
* When `options.modelDefinitions` is provided, loopback-boot does not
|
|
|
|
* search filesystem and use only the models provided in this argument.
|
2014-06-09 12:43:44 +00:00
|
|
|
* @property {Object} [dataSources] Object containing `DataSource` definitions.
|
|
|
|
* @property {String} [modelsRootDir] Directory to use when loading
|
2014-07-15 09:05:42 +00:00
|
|
|
* `model-config.json`. Defaults to `appRootDir`.
|
2014-06-09 12:43:44 +00:00
|
|
|
* @property {String} [dsRootDir] Directory to use when loading
|
2014-05-27 12:46:20 +00:00
|
|
|
* `datasources.json`. Defaults to `appRootDir`.
|
2016-02-03 12:44:09 +00:00
|
|
|
* @property {String} [middlewareRootDir] Directory to use when loading
|
|
|
|
* `middleware.json`. Defaults to `appRootDir`.
|
|
|
|
* @property {String} [componentRootDir] Directory to use when loading
|
|
|
|
* `component-config.json`. Defaults to `appRootDir`.
|
2014-06-09 12:43:44 +00:00
|
|
|
* @property {String} [env] Environment type, defaults to `process.env.NODE_ENV`
|
2014-06-02 16:47:46 +00:00
|
|
|
* or `development`. Common values are `development`, `staging` and
|
|
|
|
* `production`; however the applications are free to use any names.
|
2014-06-13 11:14:43 +00:00
|
|
|
* @property {Array.<String>} [modelSources] List of directories where to look
|
|
|
|
* for files containing model definitions.
|
2014-11-12 15:56:01 +00:00
|
|
|
* @property {Object} [middleware] Middleware configuration to use instead
|
|
|
|
* of `{appRootDir}/middleware.json`
|
2015-01-06 13:00:26 +00:00
|
|
|
* @property {Object} [components] Component configuration to use instead
|
|
|
|
* of `{appRootDir}/component-config.json`
|
2015-04-14 09:02:09 +00:00
|
|
|
* @property {Array.<String>} [mixinDirs] List of directories where to look
|
2015-06-30 16:07:22 +00:00
|
|
|
* for files containing model mixin definitions. All files (mixins) found
|
|
|
|
* in these directory are loaded.
|
|
|
|
* @property {Array.<String>} [mixinSources] List of directories where to look
|
|
|
|
* for files containing model mixin definitions. Only mixins used by
|
|
|
|
* application models are loaded from these directories.
|
2014-08-04 08:33:03 +00:00
|
|
|
* @property {Array.<String>} [bootDirs] List of directories where to look
|
|
|
|
* for boot scripts.
|
2014-08-18 12:26:11 +00:00
|
|
|
* @property {Array.<String>} [bootScripts] List of script files to execute
|
|
|
|
* on boot.
|
2015-04-14 09:02:09 +00:00
|
|
|
* @property {String|Function|Boolean} [normalization] Mixin normalization
|
|
|
|
* format: false, 'none', 'classify', 'dasherize' - defaults to 'classify'.
|
2014-05-23 14:39:34 +00:00
|
|
|
* @end
|
2014-10-30 20:34:59 +00:00
|
|
|
* @param {Function} [callback] Callback function.
|
2014-05-23 14:39:34 +00:00
|
|
|
*
|
2014-10-30 20:34:59 +00:00
|
|
|
* @header boot(app, [options], [callback])
|
2014-05-23 14:39:34 +00:00
|
|
|
*/
|
|
|
|
|
2014-10-09 19:18:36 +00:00
|
|
|
exports = module.exports = function bootLoopBackApp(app, options, callback) {
|
2017-05-08 21:48:48 +00:00
|
|
|
if (typeof options === 'string') {
|
|
|
|
// The 2nd arg is appRootDir
|
|
|
|
options = {appRootDir: options};
|
|
|
|
}
|
2016-04-27 22:55:57 +00:00
|
|
|
if (typeof options === 'function' && callback === undefined) {
|
|
|
|
callback = options;
|
|
|
|
options = {};
|
|
|
|
}
|
|
|
|
options = options || {};
|
2014-06-02 16:47:46 +00:00
|
|
|
// backwards compatibility with loopback's app.boot
|
|
|
|
options.env = options.env || app.get('env');
|
2014-05-23 14:39:34 +00:00
|
|
|
|
2019-11-17 19:19:25 +00:00
|
|
|
const bootstrapper = new Bootstrapper(options);
|
2016-04-27 22:55:57 +00:00
|
|
|
|
2019-11-17 19:19:25 +00:00
|
|
|
const context = {
|
2016-04-27 22:55:57 +00:00
|
|
|
bootstrapper: bootstrapper,
|
|
|
|
app: app,
|
|
|
|
};
|
|
|
|
|
|
|
|
return bootstrapper.run(context, callback);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.compile = function(options, done) {
|
2019-11-17 19:19:25 +00:00
|
|
|
const bootstrapper = new Bootstrapper(options);
|
2016-04-27 22:55:57 +00:00
|
|
|
bootstrapper.phases = ['load', 'compile'];
|
2019-11-17 19:19:25 +00:00
|
|
|
const context = {};
|
2016-04-27 22:55:57 +00:00
|
|
|
return bootstrapper.run(context, done);
|
2014-05-23 14:39:34 +00:00
|
|
|
};
|
|
|
|
|
2014-06-03 12:08:34 +00:00
|
|
|
/**
|
|
|
|
* Compile boot instructions and add them to a browserify bundler.
|
|
|
|
* @param {Object|String} options as described in `bootLoopBackApp` above.
|
2015-03-13 01:03:35 +00:00
|
|
|
* @property {String} [appId] Application identifier used to load the correct
|
|
|
|
* boot configuration when building multiple applications using browserify.
|
|
|
|
* @end
|
2014-06-03 12:08:34 +00:00
|
|
|
* @param {Object} bundler A browserify bundler created by `browserify()`.
|
|
|
|
*
|
|
|
|
* @header boot.compileToBrowserify(options, bundler)
|
|
|
|
*/
|
2016-04-27 22:55:57 +00:00
|
|
|
exports.compileToBrowserify = function(options, bundler, done) {
|
|
|
|
return exports.compile(options, function(err, context) {
|
|
|
|
if (err) return done(err);
|
2017-05-08 21:48:48 +00:00
|
|
|
addInstructionsToBrowserify({instructions: context.instructions},
|
2016-04-27 22:55:57 +00:00
|
|
|
bundler);
|
|
|
|
done();
|
|
|
|
});
|
2014-06-03 12:08:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.addInstructionsToBrowserify = addInstructionsToBrowserify;
|
2016-04-27 22:55:57 +00:00
|
|
|
exports.Bootstrapper = Bootstrapper;
|
|
|
|
exports.PluginBase = PluginBase;
|
|
|
|
|
|
|
|
exports.execute = function(app, instructions, done) {
|
2019-11-17 19:19:25 +00:00
|
|
|
const bootstrapper = new Bootstrapper(
|
|
|
|
{phases: ['starting', 'start', 'started']},
|
2018-07-26 16:03:12 +00:00
|
|
|
);
|
2019-11-17 19:19:25 +00:00
|
|
|
const context = {
|
2016-04-27 22:55:57 +00:00
|
|
|
app: app,
|
|
|
|
instructions: instructions,
|
|
|
|
};
|
|
|
|
return bootstrapper.run(context, done);
|
|
|
|
};
|