Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'master' of git://github.com/coreyjewett/konphyg

  • Loading branch information...
commit 8cb8642ec6980f31787d2a02c4ec46bd8bf2096f 2 parents bdf643f + e9587a3
Pedro Teixeira authored
27 README.md
View
@@ -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'.
7 assets/config/test3.json
View
@@ -0,0 +1,7 @@
+{
+ "a": 10
+ , "b": {
+ "c": 12
+ , "d": 13
+ }
+}
7 assets/config/test4.json
View
@@ -0,0 +1,7 @@
+{
+ "a": 10
+ , "b": {
+ "c": 12
+ , "d": 13
+ }
+}
7 assets/config/test5.json
View
@@ -0,0 +1,7 @@
+{
+ "a": 10
+ , "b": {
+ "c": 12
+ , "d": 13
+ }
+}
6 assets/config/test6.development.json
View
@@ -0,0 +1,6 @@
+{
+ "a": 100
+ , "b": {
+ "c": 120
+ }
+}
7 assets/config/test6.json
View
@@ -0,0 +1,7 @@
+{
+ "a": 10
+ , "b": {
+ "c": 12
+ , "d": 13
+ }
+}
7 assets/config/test7.json
View
@@ -0,0 +1,7 @@
+{
+ "a": 10
+ , "b": {
+ "c": 12
+ , "d": 13
+ }
+}
7 assets/config/test8.json
View
@@ -0,0 +1,7 @@
+{
+ "a": 10
+ , "b": {
+ "c": 12
+ , "d": 13
+ }
+}
18 lib/config.js
View
@@ -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;
2  lib/load.js
View
@@ -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;
}
3  lib/merge.js
View
@@ -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;
};
88 tests/config_test.js
View
@@ -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 tests/load_test.js
View
@@ -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 tests/merge_test.js
View
@@ -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();
});
Please sign in to comment.
Something went wrong with that request. Please try again.