diff --git a/README.md b/README.md index 10a63fb..5fa3348 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/lib/nconf/provider.js b/lib/nconf/provider.js index 1ce89ed..c228894 100644 --- a/lib/nconf/provider.js +++ b/lib/nconf/provider.js @@ -29,14 +29,34 @@ 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); }; }); // -// Define wrapper functions for using +// 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 // ['defaults', 'overrides'].forEach(function (type) { diff --git a/package.json b/package.json index 652b89a..569ad34 100644 --- a/package.json +++ b/package.json @@ -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. ", "contributors": [ { "name": "Charlie Robbins", "email": "charlie@nodejitsu.com" } diff --git a/test/hierarchy-test.js b/test/hierarchy-test.js index 1ef8b7c..e286974 100644 --- a/test/hierarchy-test.js +++ b/test/hierarchy-test.js @@ -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; },