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
This commit is contained in:
parent
3607767f90
commit
e5db2ef6d7
4 changed files with 68 additions and 28 deletions
|
@ -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,8 +91,13 @@ Argv.prototype.loadArgv = function () {
|
|||
val = common.parseValues(val);
|
||||
}
|
||||
|
||||
if (self.separator) {
|
||||
self.set(common.key.apply(common, key.split(self.separator)), val);
|
||||
}
|
||||
else {
|
||||
self.set(key, val);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
this.showHelp = yargs.showHelp
|
||||
|
|
19
test/fixtures/scripts/nconf-hierarchical-load-merge-with-separator.js
vendored
Normal file
19
test/fixtures/scripts/nconf-hierarchical-load-merge-with-separator.js
vendored
Normal 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')
|
||||
}));
|
|
@ -111,6 +111,42 @@ vows.describe('nconf/hierarchy').addBatch({
|
|||
});
|
||||
}
|
||||
},
|
||||
"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'),
|
||||
|
|
|
@ -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);
|
Loading…
Reference in a new issue