Skip to content

Commit

Permalink
Ensure main body of readconfig is only run once.
Browse files Browse the repository at this point in the history
  • Loading branch information
wwalser committed Sep 28, 2016
1 parent c840030 commit bbcd9e6
Showing 1 changed file with 36 additions and 32 deletions.
68 changes: 36 additions & 32 deletions lib/helpers/config-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ var _ = require('lodash');

var api = {
config: undefined,
rawConfig: undefined,
error: undefined,
init: function () {
return Bluebird.resolve()
Expand Down Expand Up @@ -37,7 +38,7 @@ var api = {
}
})
.then(function (config) {
api.config = config;
api.rawConfig = config;
})
.then(function () {
// Always return the full config api
Expand Down Expand Up @@ -100,43 +101,46 @@ var api = {
},

readConfig: function () {
var env = helpers.generic.getEnvironment();

if (api.config === undefined) {
throw new Error(
'Error reading "' +
api.relativeConfigFile() +
'". Error: ' + api.error
);
}
if (!api.config) {
var env = helpers.generic.getEnvironment();

if (api.rawConfig === undefined) {
throw new Error(
'Error reading "' +
api.relativeConfigFile() +
'". Error: ' + api.error
);
}

if (typeof api.config !== 'object') {
throw new Error(
'Config must be an object or a promise for and object: ' +
api.relativeConfigFile()
);
}
if (typeof api.rawConfig !== 'object') {
throw new Error(
'Config must be an object or a promise for and object: ' +
api.relativeConfigFile()
);
}

if (args.url) {
console.log('Parsed url ' + api.filteredUrl(args.url));
} else {
console.log('Loaded configuration file "' + api.relativeConfigFile() + '".');
}
if (args.url) {
console.log('Parsed url ' + api.filteredUrl(args.url));
} else {
console.log('Loaded configuration file "' + api.relativeConfigFile() + '".');
}

// in case url is present - we overwrite the configuration
if (api.config.url) {
api.config = _.merge(api.config, api.parseDbUrl(api.config.url));
}
// in case url is present - we overwrite the configuration
if (api.rawConfig.url) {
api.rawConfig = _.merge(api.rawConfig, api.parseDbUrl(api.rawConfig.url));
}

if (api.config[env]) {
console.log('Using environment "' + env + '".');
if (api.rawConfig[env]) {
console.log('Using environment "' + env + '".');

// The Sequelize library needs a function passed in to its logging option
if (api.config.logging && !_.isFunction(api.config.logging)) {
api.config.logging = console.log;
}
// The Sequelize library needs a function passed in to its logging option
if (api.rawConfig.logging && !_.isFunction(api.rawConfig.logging)) {
api.rawConfig.logging = console.log;
}

api.config = api.config[env];
api.rawConfig = api.racConfig[env];
}
api.config = api.rawConfig;
}
return api.config;
},
Expand Down

0 comments on commit bbcd9e6

Please sign in to comment.