Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

fix flatiron/nconf#28 #33

Closed
wants to merge 10 commits into from

3 participants

Jonathan Stewmon Joshua Holbrook Russ Frank
Jonathan Stewmon

Please cherry pick 81861d8.

Joshua Holbrook

Can you explain what this commit entails, exactly?

Jonathan Stewmon

It is a backward compatible change to the way file-based configuration can be hierarchically added to address #28.

Currently, the convenience methods for argv, env, file, defaults and overrides all work the same way: a store is added with a key corresponding to the type of store being added. For argv and env, that is sensible because there is only one source for those store types. However, it is possible (even likely) to have many file stores that should be added hierarchically (default.json, qa.json, etc.). So commit 81861d8 seeks to remove confusion by specifying that the preferred way to add file stores is by specifying a key and a path. The documentation and tests are updated to reflect this.

Russ Frank
rf commented

+1, there's no reason the file store should take an object.

Though, you should cut a new branch on your fork and cherry pick 81861d8, then reopen the pull request from that branch. Then the big fat auto-merge button can be used, making it more likely this will be pulled.

Joshua Holbrook

I can actually handle the cherry-pick just fine.

Also, lgtm. I'll take care of this tomorrow. :)

Joshua Holbrook

Cherry-picked! Thanks.

Joshua Holbrook jfhbrook closed this
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.
5 README.md
View
@@ -174,7 +174,10 @@ Loads a given object literal into the configuration hierarchy. Both `nconf.defau
Based on the Memory store, but provides additional methods `.save()` and `.load()` which allow you to read your configuration to and from file. As with the Memory store, all method calls are synchronous with the exception of `.save()` and `.load()` which take callback functions. It is important to note that setting keys in the File engine will not be persisted to disk until a call to `.save()` is made.
``` js
- nconf.file({ file: 'path/to/your/config.json' });
+ nconf.file('path/to/your/config.json');
+ // add multiple files, hierarchically. notice the unique key for each file
+ nconf.file('user', 'path/to/your/user.json');
+ nconf.file('global', 'path/to/your/global.json');
```
The file store is also extensible for multiple file formats, defaulting to `JSON`. To use a custom format, simply pass a format object to the `.use()` method. This object must have `.parse()` and `.stringify()` methods just like the native `JSON` object.
2  lib/nconf.js
View
@@ -7,6 +7,7 @@
var fs = require('fs'),
async = require('async'),
+ optimist = require('optimist'),
common = require('./nconf/common'),
Provider = require('./nconf/provider').Provider,
nconf = module.exports = new Provider();
@@ -37,3 +38,4 @@ nconf.loadFiles = common.loadFiles;
nconf.loadFilesSync = common.loadFilesSync;
nconf.formats = require('./nconf/formats');
nconf.Provider = Provider;
+nconf.showHelp = function() { this.opti = this.opti || optimist.options(nconf.stores.argv.options); this.opti.showHelp(); };
22 lib/nconf/provider.js
View
@@ -29,13 +29,33 @@ var Provider = exports.Provider = function (options) {
// Define wrapper functions for using basic stores
// in this instance
//
-['argv', 'env', 'file'].forEach(function (type) {
+['argv', 'env'].forEach(function (type) {
Provider.prototype[type] = function (options) {
return this.add(type, options);
};
});
//
+// Adds the file at `path` to the stores with `key`
+// If key is not given, `file` will be used as the key
+// For backward compatibility, an object can still be passed like {file: '/etc/foo.conf'}
+//
+Provider.prototype.file = function(key, path) {
+ var args = Array.prototype.slice.call(arguments, 0);
+ if(args.length == 1) {
+
+ if(typeof key === 'object'){
+ path = key.file;
+ }
+ else {
+ path = key;
+ key = 'file';
+ }
+ }
+ return this.add(key, {type: 'file', file: path});
+};
+
+//
// Define wrapper functions for using
// overrides and defaults
//
5 package.json
View
@@ -1,7 +1,7 @@
{
"name": "nconf",
"description": "Hierarchical node.js configuration with files, environment variables, command-line arguments, and atomic object merging.",
- "version": "0.5.1",
+ "version": "0.6.0",
"author": "Nodejitsu Inc. <info@nodejitsu.com>",
"contributors": [
{ "name": "Charlie Robbins", "email": "charlie@nodejitsu.com" }
@@ -18,7 +18,8 @@
"pkginfo": "0.2.x"
},
"devDependencies": {
- "vows": "0.6.x"
+ "vows": "0.6.x",
+ "eyes": "0.1.7"
},
"main": "./lib/nconf",
"scripts": { "test": "vows test/*-test.js test/**/*-test.js --spec" },
17 test/hierarchy-test.js
View
@@ -20,8 +20,21 @@ vows.describe('nconf/hierarchy').addBatch({
"When using nconf": {
"configured with two file stores": {
topic: function () {
- nconf.add('user', { type: 'file', file: userConfig })
- nconf.add('global', { type: 'file', file: globalConfig })
+ nconf.add('user', { type: 'file', file: userConfig });
+ nconf.add('global', { type: 'file', file: globalConfig });
+ nconf.load();
+ return nconf;
+ },
+ "should have the appropriate keys present": function () {
+ assert.equal(nconf.get('title'), 'My specific title');
+ assert.equal(nconf.get('color'), 'green');
+ assert.equal(nconf.get('movie'), 'Kill Bill');
+ }
+ },
+ "configured with two file stores using `file`": {
+ topic: function () {
+ nconf.file('user', userConfig);
+ nconf.file('global', globalConfig);
nconf.load();
return nconf;
},
3  test/provider-test.js
View
@@ -11,7 +11,8 @@ var assert = require('assert'),
spawn = require('child_process').spawn,
vows = require('vows'),
helpers = require('./helpers'),
- nconf = require('../lib/nconf');
+ nconf = require('../lib/nconf'),
+ eyes = require('eyes');
var fixturesDir = path.join(__dirname, 'fixtures'),
mergeFixtures = path.join(fixturesDir, 'merge'),
Something went wrong with that request. Please try again.