Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
  • 4 commits
  • 7 files changed
  • 0 commit comments
  • 2 contributors
View
80 README.md
@@ -14,11 +14,22 @@ These should be JSON files, terminated by ".json".
This example loads and parses the ../config/redis.json file:
- // Initialize konphyg with the base config dir
- var config = require('konphyg')(__dirname + '../config');
+```js
+// Initialize konphyg with the base config dir
+var config = require('konphyg')(__dirname + '../config');
- // Read the "redis" domain
- var redisConfig = config('redis');
+// Read the "redis" domain
+var redisConfig = config('redis');
+```
+
+Loading all configurations:
+```js
+// Initialize konphyg with the base config dir
+var konphyg = require('konphyg')(__dirname + '../config');
+
+// Read the "redis" domain
+var config = konphyg.all();
+```
## Environment-specific files and cascading
@@ -34,62 +45,41 @@ What happens here is that the settings in the base configuration are merged with
For instance let's say you have this `redis.json` file:
- {
- "host": "redis.acme.com"
- , "port": "6379"
- }
+```json
+{
+ "host": "redis.acme.com"
+ , "port": "6379"
+}
+```
and that you have this `redis.development.json` file:
- {
- "host": "127.0.0.1"
- }
+```json
+{
+ "host": "127.0.0.1"
+}
+```
The resulting configuration for the development environment will be the merge of the 2:
- {
- "host": "127.0.0.1"
- , "port": "6379"
- }
-
+```json
+{
+ "host": "127.0.0.1"
+ , "port": "6379"
+}
+```
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'.
If you want to launch a node process using the 'production' environment you should then do something like:
-
- $ NODE_ENV=production node app.js
+```bash
+NODE_ENV=production node app.js
+```
# Resources
View
4 assets/config/test3.development.json
@@ -0,0 +1,4 @@
+{
+ "e": 10
+ , "f": [1, 2, 3, 4]
+}
View
4 assets/config/test9.development.json
@@ -0,0 +1,4 @@
+{
+ "e": 10
+ , "f": [1, 2, 3, 4]
+}
View
4 assets/config/test9.json
@@ -0,0 +1,4 @@
+{
+ "e": 10
+ , "f": [1, 2, 3]
+}
View
35 lib/config.js
@@ -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
2  package.json
@@ -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
11 tests/config_test.js
@@ -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");

No commit comments for this range

Something went wrong with that request. Please try again.