Skip to content

Commit

Permalink
Copy missing properties from default environment
Browse files Browse the repository at this point in the history
  • Loading branch information
cleishm committed Dec 18, 2011
1 parent 1a96bc7 commit bb00ce3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
29 changes: 25 additions & 4 deletions lib/envious.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,20 @@ function envious () {
// throw an error for an invalid env instead.
this.apply = function(options) {
if (!options) options = {}

if (this.default_env && !this[this.default_env]) {
// default not found
throw new Error('envious: no configuration found for default environment `' + this.default_env + '`');
}

var envName = process.env.NODE_ENV;
if (this[envName]) {
// environment matched
return this[envName];
var env = deepDefaults({}, this[envName]);
if (this.default_env) {
env = deepDefaults(env, this[this.default_env]);
}
return env;
} else {
// no environment matched
if (envName && options.strict) {
Expand All @@ -33,9 +43,6 @@ function envious () {
// env is undefined/empty
if (!this.default_env) {
throw new Error('envious: no default environment found');
} else if (this.default_env && !this[this.default_env]) {
// default not found
throw new Error('envious: no configuration found for default environment `' + this.default_env + '`');
} else if (this.default_env && this[this.default_env]) {
// return default
return this[this.default_env];
Expand All @@ -45,4 +52,18 @@ function envious () {
}
}

function deepDefaults(obj) {
Array.prototype.slice.call(arguments, 1).forEach(function(source) {
for (var prop in source) {
if (typeof obj[prop] === 'undefined') {
obj[prop] = source[prop];
} else if (typeof obj[prop] === 'object'
&& typeof source[prop] === 'object') {
obj[prop] = deepDefaults(obj[prop], source[prop]);
}
}
});
return obj;
}

module.exports = new envious();
14 changes: 14 additions & 0 deletions test/normal.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,20 @@ envious.development =
{
"mongodb_hostname": "dev.local",
"site_url": "http://127.0.0.1/",
"session_secret": "ssssshhhhh",
"smtp": {
"server": "localhost",
"email": "noreply@sterlingcooper.com"
}
}

envious.production =
{
"mongodb_hostname": "masterdb.sterlingcooper.com",
"site_url": "http://zombo.com/",
"smtp": {
"server": "smtp.sterlingcooper.com",
}
}

envious.default_env = "development";
Expand All @@ -31,4 +39,10 @@ assert.equal(env.mongodb_hostname, 'masterdb.sterlingcooper.com');
console.log('test -- variable site_url should not match that of the development set');
assert.notEqual(env.site_url, 'http://127.0.0.1/');

console.log('test -- variable session_secret should be that of the default set');
assert.equal(env.session_secret, 'ssssshhhhh');

console.log('test -- variable smtp.email should be that of the default set');
assert.equal(env.smtp.email, 'noreply@sterlingcooper.com');

console.log('== all tests passed successfully');

0 comments on commit bb00ce3

Please sign in to comment.