Merge pull request #43 from johnsoftek/master

Custom rootDir for app config

Close #43
This commit is contained in:
Miroslav Bajtoš 2014-10-09 16:36:31 +02:00
commit 0d4b5bb7c4
3 changed files with 64 additions and 4 deletions

View File

@ -23,15 +23,54 @@ var addInstructionsToBrowserify = require('./lib/bundler');
* 2. Configures Models from the `model-config.json` file in the application * 2. Configures Models from the `model-config.json` file in the application
* root directory. * 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`, * 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 * If the object has no `appRootDir` property then it sets the current working
* directory as the application root directory. * 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: * 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 * In both cases, the function loads JavaScript files in the
* `/boot` subdirectory of the application root directory with `require()`. * `/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 * @property {String} [appRootDir] Directory to use when loading JSON and
* JavaScript files. * JavaScript files.
* Defaults to the current directory (`process.cwd()`). * 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} [models] Object containing `Model` configurations.
* @property {Object} [dataSources] Object containing `DataSource` definitions. * @property {Object} [dataSources] Object containing `DataSource` definitions.
* @property {String} [modelsRootDir] Directory to use when loading * @property {String} [modelsRootDir] Directory to use when loading

View File

@ -28,7 +28,9 @@ module.exports = function compile(options) {
var appRootDir = options.appRootDir = options.appRootDir || process.cwd(); var appRootDir = options.appRootDir = options.appRootDir || process.cwd();
var env = options.env || process.env.NODE_ENV || 'development'; 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); assertIsValidConfig('app', appConfig);
var modelsRootDir = options.modelsRootDir || appRootDir; var modelsRootDir = options.modelsRootDir || appRootDir;

View File

@ -179,6 +179,23 @@ describe('compiler', function() {
expect(appConfig).to.have.property('fromJs', true); 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() { it('supports `dsRootDir` option', function() {
appdir.createConfigFilesSync(); appdir.createConfigFilesSync();