Permalink
Browse files

minor version bump

  • Loading branch information...
1 parent dfc1101 commit 805386a7411c9ed2507153e23e8c559673cf6284 @pgte committed Oct 15, 2012
Showing with 36 additions and 12 deletions.
  1. +26 −9 lib/config.js
  2. +1 −1 package.json
  3. +9 −2 tests/config_test.js
View
@@ -1,15 +1,15 @@
-var path = require('path')
+var merge = require('./merge')
, load = require('./load')
- , merge = require('./merge');
-
+ , path = require('path')
+ , fs = require('fs')
module.exports = function(basePath, alwaysRequireEnv) {
basePath = path.normalize(basePath);
- domains = {};
-
+ var domains = {};
var env = process.env.NODE_ENV || 'development';
-
- return function(domain, requireEnv) {
+ var files = fs.readdirSync(basePath)
+
+ var get = function (domain, requireEnv) {
var config;
// default to global expectation unless specifically overridden.
@@ -20,7 +20,7 @@ module.exports = function(basePath, alwaysRequireEnv) {
if (domains.hasOwnProperty(domain)) {
return domains[domain];
}
-
+
config = merge(
load(path.join(basePath, domain + '.json'), domain, true)
, load(path.join(basePath, domain + '.' + env + '.json'), domain + "." + env, !!(requireEnv === true))
@@ -29,5 +29,22 @@ module.exports = function(basePath, alwaysRequireEnv) {
domains[domain] = config;
return config;
- };
+ }
+
+ get.all = function () {
+ files.forEach(function (file) {
+ if(file.match(/^\./)) return;
+ if(!file.match(/\.json$/i)) return;
+ var domain = file.match(/^(.*?)\./i)[1];
+ if(domains.hasOwnProperty(domain)) return;
+ domains[domain] = merge(
+ load(path.join(basePath, domain + '.json'), domain, true)
+ , load(path.join(basePath, domain + '.' + env + '.json'), domain, false)
+ );
+ });
+
+ return domains;
+ }
+
+ return get
};
View
@@ -1,7 +1,7 @@
{ "name" : "konphyg"
, "description" : "Cascading configuration files made easy in Node.js."
, "tags" : ["configuration", "environments"]
-, "version" : "1.1.0"
+, "version" : "1.2.0"
, "author" : "Pedro Teixeira <pedro.teixeira@gmail.com>"
, "repository" :
{ "type" : "git"
View
@@ -8,6 +8,13 @@ test("load existing configuration with one file", function(t) {
t.end();
});
+test("load all configs", function(t) {
+ var config = konphyg.all();
+ var expected = {test1:{a:100,b:{c:120,d:13}},test9:{e:10,f:[1,2,3,4]}}
+ t.similar(config, expected, "wrong file contents");
+ t.end();
+});
+
test("non-existing base configuration file throws error", function(t) {
var threw = false;
@@ -52,10 +59,10 @@ test("non-existing env configuration file throws error if requireEnv", function(
var threw = false;
try {
- var config = konphyg('test5', true);
+ var config = konphyg('testdoesnotexist', 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(!!(/^Could not find configuration file for testdoesnotexist domain/.test(err.message)), "Error message: " + err.message);
}
t.ok(threw, "should throw when env file is not found");

0 comments on commit 805386a

Please sign in to comment.