Argv store separator (#291)

* argv store now accept a separator argument to create nested values

* remove stub file that shouldnt have been commited

* write a test to ensure separator is working well and use delete rather than undefined assign
master
Adrien Becchis 2017-11-05 02:30:14 +01:00 committed by Matt Hamann
parent 3607767f90
commit e5db2ef6d7
4 changed files with 68 additions and 28 deletions

View File

@ -35,7 +35,12 @@ var Argv = exports.Argv = function (options, usage) {
} else {
this.transform = false;
}
if (typeof options.separator === 'string' || options.separator instanceof RegExp) {
this.separator = options.separator;
delete options.separator;
} else {
this.separator = '';
}
};
// Inherit from the Memory store
@ -86,7 +91,12 @@ Argv.prototype.loadArgv = function () {
val = common.parseValues(val);
}
self.set(key, val);
if (self.separator) {
self.set(common.key.apply(common, key.split(self.separator)), val);
}
else {
self.set(key, val);
}
}
});

View File

@ -0,0 +1,19 @@
/*
* nconf-hierarchical-load-merge.js: Test fixture for loading and merging nested objects across stores.
*
* (C) 2012, Charlie Robbins and the Contributors.
* (C) 2012, Michael Hart
*
*/
var path = require('path'),
nconf = require('../../../lib/nconf');
nconf.argv({separator: '--'})
.env('__')
.file(path.join(__dirname, '..', 'merge', 'file1.json'));
process.stdout.write(JSON.stringify({
apples: nconf.get('apples'),
candy: nconf.get('candy')
}));

View File

@ -111,7 +111,43 @@ vows.describe('nconf/hierarchy').addBatch({
});
}
},
"configured with .file(), .defaults() should deep merge objects": {
"configured with .argv() and separator, .file() and invoked with nested command line options": {
topic: function () {
var script = path.join(__dirname, 'fixtures', 'scripts', 'nconf-hierarchical-load-merge-with-separator.js'),
argv = ['--candy--something', 'foo', '--candy--something5--second', 'bar'],
that = this,
data = '',
child;
process.env.candy__bonbon = 'sweet';
child = spawn('node', [script].concat(argv));
delete process.env.candy__bonbon;
child.stdout.on('data', function (d) {
data += d;
});
child.on('close', function() {
that.callback(null, data);
});
},
"should merge nested objects ": function (err, data) {
console.log(data)
assert.deepEqual(JSON.parse(data), {
apples: true,
candy: {
bonbon: 'sweet',
something: 'foo',
something1: true,
something2: true,
something5: {
first: 1,
second: 'bar'
}
}
});
}
},
"configured with .file(), .defaults() should deep merge objects": {
topic: function () {
var script = path.join(__dirname, 'fixtures', 'scripts', 'nconf-hierarchical-defaults-merge.js'),
that = this,

View File

@ -1,25 +0,0 @@
/*
* file-store-test.js: Tests for the nconf File store.
*
* (C) 2011, Charlie Robbins and the Contributors.
*
*/
var fs = require('fs'),
path = require('path'),
vows = require('vows'),
assert = require('assert'),
nconf = require('../lib/nconf'),
yargs = require('yargs')
vows.describe('nconf/argv').addBatch({
"When using the nconf": {
"with a custom yargs": {
topic: function () {
fs.readFile(path.join(__dirname, '..', 'package.json'), this.callback);
},
},
"with the default yars": {
}
}
}).export(module);