Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Expose #merge #2

Merged
merged 6 commits into from

2 participants

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
27 README.md
@@ -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'.
View
7 assets/config/test3.json
@@ -0,0 +1,7 @@
+{
+ "a": 10
+ , "b": {
+ "c": 12
+ , "d": 13
+ }
+}
View
7 assets/config/test4.json
@@ -0,0 +1,7 @@
+{
+ "a": 10
+ , "b": {
+ "c": 12
+ , "d": 13
+ }
+}
View
7 assets/config/test5.json
@@ -0,0 +1,7 @@
+{
+ "a": 10
+ , "b": {
+ "c": 12
+ , "d": 13
+ }
+}
View
6 assets/config/test6.development.json
@@ -0,0 +1,6 @@
+{
+ "a": 100
+ , "b": {
+ "c": 120
+ }
+}
View
7 assets/config/test6.json
@@ -0,0 +1,7 @@
+{
+ "a": 10
+ , "b": {
+ "c": 12
+ , "d": 13
+ }
+}
View
7 assets/config/test7.json
@@ -0,0 +1,7 @@
+{
+ "a": 10
+ , "b": {
+ "c": 12
+ , "d": 13
+ }
+}
View
7 assets/config/test8.json
@@ -0,0 +1,7 @@
+{
+ "a": 10
+ , "b": {
+ "c": 12
+ , "d": 13
+ }
+}
View
18 lib/config.js
@@ -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;
View
2  lib/load.js
@@ -8,7 +8,7 @@ module.exports = function(filePath, domain, throwIfNotFound) {
} catch(err) {
if (err.code === 'EBADF') {
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;
}
View
3  lib/merge.js
@@ -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;
};
View
2  package.json
@@ -1,7 +1,7 @@
{ "name" : "konphyg"
, "description" : "Cascading configuration files made easy in Node.js."
, "tags" : ["configuration", "environments"]
-, "version" : "1.0.3"
+, "version" : "1.0.5"
, "author" : "Pedro Teixeira <pedro.teixeira@gmail.com>"
, "repository" :
{ "type" : "git"
View
88 tests/config_test.js
@@ -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();
+});
+
View
13 tests/load_test.js
@@ -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();
});
View
10 tests/merge_test.js
@@ -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();
});
Something went wrong with that request. Please try again.