Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I dump the current nconf configuration to an object?

Is there any method in nconf to collect all the keys from all the stores into a single object?

Imagine I've got this little script:

assert = require('assert');
nconf = require('nconf');

nconf.argv().env().defaults({'C': 3});
assert.equal(nconf.get('A'), 1);
assert.equal(nconf.get('B'), 2);
assert.equal(nconf.get('C'), 3);
assert.deepEqual({'A':1, 'B':2, 'C':3}, nconf.X); // <-- What's X?

that I run with

A=1 node script.js -B 2

Is there an nconf.X that will pass the test? I'd even settle for listing all the configured keys.

like image 795
hurrymaplelad Avatar asked Nov 20 '12 07:11

hurrymaplelad


Video Answer


1 Answers

Yes to get the object you can do the following;

nconf.get();

it will fail the test as argv will with the exec string and env will have a lot more variables though.

You could whitelist the env call using the following

nconf.env({whitelist: 'A'});

Also the defaults adds a 'type' with value 'literal' to the resulting output.

For a test that passes you could use this;

var assert = require('assert'),
nconf = require('nconf');

nconf.argv().env({whitelist: ['A']}).defaults({'C': 3});
assert.equal(nconf.get('A'), 1);
assert.equal(nconf.get('B'), 2);
assert.equal(nconf.get('C'), 3);

var object = nconf.get();

delete object.type;
delete object['$0'];
delete object['_'];

assert.deepEqual({'A':1, 'B':2, 'C':3}, object);
like image 160
Jason Brumwell Avatar answered Oct 29 '22 00:10

Jason Brumwell