diff --git a/index.js b/index.js index 772a321..4651138 100644 --- a/index.js +++ b/index.js @@ -23,15 +23,54 @@ var addInstructionsToBrowserify = require('./lib/bundler'); * 2. Configures Models from the `model-config.json` file in the application * root directory. * + * 3. Configures the LoopBack Application object from the `config.json` file + * in the application root directory. These properties can be accessed + * using `app.get('propname')`. + * * If the argument is an object, then it looks for `models`, `dataSources`, - * and `appRootDir` properties of the object. + * 'config', `modelsRootDir`, `dsRootDir`, `appConfigRootDir` and `appRootDir` + * properties of the object. + * * If the object has no `appRootDir` property then it sets the current working * directory as the application root directory. + * + * The execution environment, {env}, is established from, in order, + * - `options.env` + * - `process.env.NODE_ENV`, + * - the literal `development`. + * * Then it: * - * 1. Creates DataSources from the `options.dataSources` object. + * 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) * - * 2. Configures Models from the `options.models` object. + * in the directory designated by 'options.dsRootDir', if present, or the + * application root directory. It merges the data source definitions from + * the files found. + * + * 2. Creates Models from the `options.models` object, if provided; + * otherwise, it searches for the files + * - `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) + * + * in the directory designated by 'options.modelsRootDir', if present, or + * the application root directory. It merges the model definitions from the + * files found. + * + * 3. Configures the Application object from the `options.config` object, + * if provided; + * otherwise, it searches for the files + * - `config.json`, + * - `config.local.js` or `config.local.json` (only one), + * - `config.{env}.js` or `config.{env}.json` (only one) + * + * in the directory designated by 'options.appConfigRootDir', if present, or + * the application root directory. It merges the properties from the files + * found. * * In both cases, the function loads JavaScript files in the * `/boot` subdirectory of the application root directory with `require()`. @@ -55,6 +94,8 @@ var addInstructionsToBrowserify = require('./lib/bundler'); * @property {String} [appRootDir] Directory to use when loading JSON and * JavaScript files. * Defaults to the current directory (`process.cwd()`). + * @property {String} [appConfigRootDir] Directory to use when loading + * `config.json`. Defaults to `appRootDir`. * @property {Object} [models] Object containing `Model` configurations. * @property {Object} [dataSources] Object containing `DataSource` definitions. * @property {String} [modelsRootDir] Directory to use when loading diff --git a/lib/compiler.js b/lib/compiler.js index bca4a85..d5d870a 100644 --- a/lib/compiler.js +++ b/lib/compiler.js @@ -28,7 +28,9 @@ module.exports = function compile(options) { var appRootDir = options.appRootDir = options.appRootDir || process.cwd(); var env = options.env || process.env.NODE_ENV || 'development'; - var appConfig = options.config || ConfigLoader.loadAppConfig(appRootDir, env); + var appConfigRootDir = options.appConfigRootDir || appRootDir; + var appConfig = options.config || + ConfigLoader.loadAppConfig(appConfigRootDir, env); assertIsValidConfig('app', appConfig); var modelsRootDir = options.modelsRootDir || appRootDir; diff --git a/test/compiler.test.js b/test/compiler.test.js index ee712d1..aa6182f 100644 --- a/test/compiler.test.js +++ b/test/compiler.test.js @@ -179,6 +179,23 @@ describe('compiler', function() { expect(appConfig).to.have.property('fromJs', true); }); + it('supports `appConfigRootDir` option', function() { + appdir.createConfigFilesSync({port:3000}); + + var customDir = path.resolve(appdir.PATH, 'custom'); + fs.mkdirsSync(customDir); + fs.renameSync( + path.resolve(appdir.PATH, 'config.json'), + path.resolve(customDir, 'config.json')); + + var instructions = boot.compile({ + appRootDir: appdir.PATH, + appConfigRootDir: path.resolve(appdir.PATH, 'custom') + }); + + expect(instructions.config).to.have.property('port'); + }); + it('supports `dsRootDir` option', function() { appdir.createConfigFilesSync();