Merge pull request #143 from strongloop/dashby3000-master

Fix & Merge https://github.com/strongloop/loopback-boot/pull/141
This commit is contained in:
Raymond Feng 2015-08-03 07:45:48 -07:00
commit e052da83db
2 changed files with 54 additions and 27 deletions

View File

@ -32,7 +32,7 @@ ConfigLoader.loadDataSources = function(rootDir, env) {
*/
ConfigLoader.loadModels = function(rootDir, env) {
/*jshint unused:false */
return tryReadJsonConfig(rootDir, 'model-config') || {};
return loadNamed(rootDir, env, 'model-config', mergeModelConfig);
};
/**
@ -140,11 +140,16 @@ function mergeConfigurations(configObjects, mergeFn) {
}
function mergeDataSourceConfig(target, config, fileName) {
for (var ds in target) {
var err = mergeObjects(target[ds], config[ds]);
if (err) {
throw new Error('Cannot apply ' + fileName + ' to `' + ds + '`: ' + err);
}
var err = mergeObjects(target, config);
if (err) {
throw new Error('Cannot apply ' + fileName + ': ' + err);
}
}
function mergeModelConfig(target, config, fileName) {
var err = mergeObjects(target, config);
if (err) {
throw new Error('Cannot apply ' + fileName + ': ' + err);
}
}
@ -182,11 +187,9 @@ function mergePhaseConfig(target, config, phase) {
}
function mergeComponentConfig(target, config, fileName) {
for (var c in target) {
var err = mergeObjects(target[c], config[c]);
if (err) {
throw new Error('Cannot apply ' + fileName + ' to `' + c + '`: ' + err);
}
var err = mergeObjects(target, config);
if (err) {
throw new Error('Cannot apply ' + fileName + ': ' + err);
}
}
@ -250,19 +253,3 @@ function hasCompatibleType(origValue, newValue) {
// we don't need to explicitly check array types
return typeof newValue !== 'object';
}
/**
* Try to read a config file with .json extension
* @param {string} cwd Dirname of the file
* @param {string} fileName Name of the file without extension
* @returns {Object|undefined} Content of the file, undefined if not found.
*/
function tryReadJsonConfig(cwd, fileName) {
try {
return require(path.join(cwd, fileName + '.json'));
} catch (e) {
if (e.code !== 'MODULE_NOT_FOUND') {
throw e;
}
}
}

View File

@ -417,6 +417,46 @@ describe('compiler', function() {
expect(db.nested).to.eql(arrayValue);
});
it('allows env specific model-config json', function() {
appdir.createConfigFilesSync();
appdir.writeConfigFileSync('model-config.local.json', {
foo: { dataSource: 'db' }
});
var instructions = boot.compile(appdir.PATH);
expect(instructions.models).to.have.length(1);
expect(instructions.models[0]).to.have.property('name', 'foo');
});
it('allows env specific model-config json to be merged', function() {
appdir.createConfigFilesSync(null, null,
{foo: {dataSource: 'mongo', public: false}});
appdir.writeConfigFileSync('model-config.local.json', {
foo: {dataSource: 'db'}
});
var instructions = boot.compile(appdir.PATH);
expect(instructions.models).to.have.length(1);
expect(instructions.models[0]).to.have.property('name', 'foo');
expect(instructions.models[0].config).to.eql({
dataSource: 'db',
public: false
});
});
it('allows env specific model-config js', function() {
appdir.createConfigFilesSync();
appdir.writeFileSync('model-config.local.js',
'module.exports = { foo: { dataSource: \'db\' } };');
var instructions = boot.compile(appdir.PATH);
expect(instructions.models).to.have.length(1);
expect(instructions.models[0]).to.have.property('name', 'foo');
});
it('refuses to merge Array properties of different length', function() {
appdir.createConfigFilesSync({
nest: {