Skip to content

Commit

Permalink
Merge branch 'master' of git://github.com/coreyjewett/konphyg
Browse files Browse the repository at this point in the history
  • Loading branch information
pgte committed Oct 15, 2012
2 parents bdf643f + e9587a3 commit 8cb8642
Show file tree
Hide file tree
Showing 14 changed files with 200 additions and 9 deletions.
27 changes: 27 additions & 0 deletions README.md
Expand Up @@ -55,6 +55,33 @@ The resulting configuration for the development environment will be the merge of

This also works with attributes nested at any level.

# Explicitly Require Environment File(s)

Optional feature. At require time or per configuration fetch specify if an environment file is mandatory. If not found then an Error will be thrown. Normal behavior is to simply ignore the
non-existence of an environment specific file.

At require time:

// Initialize konphyg with the base config dir (require environment files)
var config = require('konphyg', true)(__dirname + '../config');

Or at configure time:

// Normal require
// ...

// Read the "redis" domain (environment file must exist)
var redisConfig = config('redis', true);

Also, over-riding require time mandate at configure time:

// Initialize konphyg with the base config dir (require environment files)
var config = require('konphyg', true)(__dirname + '../config');

// Read the "redis" domain (over-ride, environment file doesn't need to exist)
var redisConfig = config('redis', false);


## NODE_ENV defaults

If not present, the chosen environment is 'development'.
Expand Down
7 changes: 7 additions & 0 deletions assets/config/test3.json
@@ -0,0 +1,7 @@
{
"a": 10
, "b": {
"c": 12
, "d": 13
}
}
7 changes: 7 additions & 0 deletions assets/config/test4.json
@@ -0,0 +1,7 @@
{
"a": 10
, "b": {
"c": 12
, "d": 13
}
}
7 changes: 7 additions & 0 deletions assets/config/test5.json
@@ -0,0 +1,7 @@
{
"a": 10
, "b": {
"c": 12
, "d": 13
}
}
6 changes: 6 additions & 0 deletions assets/config/test6.development.json
@@ -0,0 +1,6 @@
{
"a": 100
, "b": {
"c": 120
}
}
7 changes: 7 additions & 0 deletions assets/config/test6.json
@@ -0,0 +1,7 @@
{
"a": 10
, "b": {
"c": 12
, "d": 13
}
}
7 changes: 7 additions & 0 deletions assets/config/test7.json
@@ -0,0 +1,7 @@
{
"a": 10
, "b": {
"c": 12
, "d": 13
}
}
7 changes: 7 additions & 0 deletions assets/config/test8.json
@@ -0,0 +1,7 @@
{
"a": 10
, "b": {
"c": 12
, "d": 13
}
}
18 changes: 12 additions & 6 deletions lib/config.js
Expand Up @@ -3,21 +3,27 @@ var path = require('path')
, merge = require('./merge');


module.exports = function(basePath) {
module.exports = function(basePath, alwaysRequireEnv) {
basePath = path.normalize(basePath);
domains = {};
return function(domain) {

var env = process.env.NODE_ENV || 'development';

return function(domain, requireEnv) {
var config;


// default to global expectation unless specifically overridden.
if (requireEnv !== false && alwaysRequireEnv === true) {
requireEnv = true;
}

if (domains.hasOwnProperty(domain)) {
return domains[domain];
}

var env = process.env.NODE_ENV || 'development';

config = merge(
load(path.join(basePath, domain + '.json'), domain, true)
, load(path.join(basePath, domain + '.' + env + '.json'), domain, false)
, load(path.join(basePath, domain + '.' + env + '.json'), domain + "." + env, !!(requireEnv === true))
);

domains[domain] = config;
Expand Down
2 changes: 1 addition & 1 deletion lib/load.js
Expand Up @@ -8,7 +8,7 @@ module.exports = function(filePath, domain, throwIfNotFound) {
} catch(err) {
if (err.code === 'EBADF' || err.code == 'ENOENT') {
if (throwIfNotFound) {
throw new Error('Could not find configuration file for ' + domain + ' domain');
throw new Error('Could not find configuration file for ' + domain + ' domain: ' + filePath);
}
return null;
}
Expand Down
3 changes: 3 additions & 0 deletions lib/merge.js
Expand Up @@ -41,6 +41,9 @@ function merge(a, b) {
mergeProperty(a, b, prop);
}

// expose merge routine for reuse
a._merge = function(b) { return merge(a, b); }

return a;
};

Expand Down
88 changes: 86 additions & 2 deletions tests/config_test.js
Expand Up @@ -15,9 +15,93 @@ test("non-existing base configuration file throws error", function(t) {
var config = konphyg('test2');
} catch(err) {
threw = true
t.equal(err.message, 'Could not find configuration file for test2 domain')
t.ok(!!(/^Could not find configuration file for test2 domain/.test(err.message)), "Error message: " + err.message);
}

t.ok(threw, "should throw when base file is not found");
t.end();
});
});

test("non-existing env configuration file doesn't throw error if requireEnv not specified", function(t) {
var didnt_throw = true;

try {
var config = konphyg('test3', false);
} catch(err) {
didnt_throw = false;
}

t.ok(didnt_throw, "should not throw, requireEnv not set.");
t.end();
});

test("non-existing env configuration file doesn't throw error if requireEnv !== true", function(t) {
var didnt_throw = true;

try {
var config = konphyg('test4', false);
} catch(err) {
didnt_throw = false;
}

t.ok(didnt_throw, "should not throw, requireEnv not set.");
t.end();
});

test("non-existing env configuration file throws error if requireEnv", function(t) {
var threw = false;

try {
var config = konphyg('test5', true);
} catch(err) {
threw = true
t.ok(!!(/^Could not find configuration file for test5.development domain/.test(err.message)), "Error message: " + err.message);
}

t.ok(threw, "should throw when env file is not found");
t.end();
});

test("non-existing env configuration file doesn't throw error if alwaysRequireEnv === false", function(t) {
var my_konphyg = require('../.')(__dirname + '/../assets/config', false);
var didnt_throw = true;

try {
var config = my_konphyg('test6', false);
} catch(err) {
didnt_throw = false;
}

t.ok(didnt_throw, "should not throw, alwaysRequireEnv not set.");
t.end();
});

test("non-existing env configuration file throws error if alwaysRequireEnv === true", function(t) {
var my_konphyg = require('../.')(__dirname + '/../assets/config', true);
var threw = false;

try {
var config = my_konphyg('test7');
} catch(err) {
threw = true
t.ok(!!(/^Could not find configuration file for test7.development domain/.test(err.message)), "Error message: " + err.message);
}

t.ok(threw, "should throw when env file is not found (alwaysRequireEnv)");
t.end();
});

test("non-existing env configuration file doesn't throw error if alwaysRequireEnv === true && requireEnv === false", function(t) {
var my_konphyg = require('../.')(__dirname + '/../assets/config', true);
var didnt_throw = true;

try {
var config = my_konphyg('test8', false);
} catch(err) {
didnt_throw = false;
}

t.ok(didnt_throw, "should not throw, requireEnv === false.");
t.end();
});

13 changes: 13 additions & 0 deletions tests/load_test.js
Expand Up @@ -12,4 +12,17 @@ test("load non-existing file", function(t) {
var config = load(__dirname + '/../assets/test_does_not_exist.json');
t.type(config, "null");
t.end();
});

test("load non-existing file and throw", function(t) {
var threw = false;
try {
load(__dirname + '/../assets/test_does_not_exist.json', 'testdomain', true);
} catch(err) {
threw = true
t.ok(!!(/^Could not find configuration file for testdomain domain/.test(err.message)));
}

t.ok(threw, "should throw when base file is not found");
t.end();
});
10 changes: 10 additions & 0 deletions tests/merge_test.js
Expand Up @@ -48,4 +48,14 @@ test("b object property is merged with a object property", function(t) {
t.type(result, "object");
t.similar(result, {a:{c:3,d:5,e:6},b:2});
t.end();
});

test("object has merging capability", function(t) {
var a = {a: 1, b: 2};
var b = {a: 3};
var result = merge(a, a);
result = result._merge(b);
t.type(result, "object");
t.similar(result, {a:3, b:2});
t.end();
});

0 comments on commit 8cb8642

Please sign in to comment.