Skip to content
This repository was archived by the owner on Sep 25, 2020. It is now read-only.

Commit c885b91

Browse files
committed
Merge pull request #2 from uber/seed-config
support a seed option
2 parents 35d430c + 0a778a2 commit c885b91

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

README.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ playdoh-server/config := (dirname: String, opts?: {
5959
argv?: Array<String>,
6060
dc?: String,
6161
blackList?: Array<String>,
62-
env?: Object<String, String>
62+
env?: Object<String, String>,
63+
seed?: Object<String, Any>
6364
}) => {
6465
get: (keypath?: String) => Any,
6566
set: (keypath: String, value: Any) => void,
@@ -101,6 +102,7 @@ Below are the sources it reads in order of least precendence.
101102
- a object literal based on command line arguments. i.e. if
102103
you pass `--foo='bar' --bar.baz='bob'` you will get
103104
`{ "foo": "bar", "bar": { "baz": "bob" } }`
105+
- a seed object of manual overwrites for testing purposes.
104106

105107
The config loader also uses `config-chain` for the actual
106108
loading logic so you can read [their docs][config-chain]
@@ -182,6 +184,21 @@ If you prefer to not have this variable configured through
182184
the environment or want to call it something else then you
183185
can pass in `{ NODE_ENV: whatever }` as `opts.env`
184186

187+
#### `opts.seed`
188+
189+
`opts.seed` is optional, it can be set to an object
190+
191+
If it exists we will merge the seed object into the config
192+
data we have fetched. seed overwrites all the other sources
193+
of configuration.
194+
195+
The `seed` option is very useful for testing purposes, it allows
196+
you to overwrite the configuration that your application would
197+
load with test specific properties.
198+
199+
This is an alternative to the `NODE_ENV=test `pattern, we highly
200+
recommend that you do not have a `test.json` file at all.
201+
185202
#### `var value = config.get(keypath)`
186203

187204
`config.get(keypath)` will return the value at a keypath. The

index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ function fetchConfigSync(dirname, opts) {
149149
"transports" for loading configuration from disk
150150
*/
151151
var configTree = configChain(
152+
// the seed option overwrites everything
153+
opts.seed || null,
152154
// include all CLI arguments
153155
makeDeep(cliArgs),
154156
// load file from --config someFilePath

test/index.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,3 +296,28 @@ test('config.set(undefined) throws', function (assert) {
296296
assert.end();
297297

298298
});
299+
300+
test('config({ seed: seed })', function (assert) {
301+
var argv = [
302+
'--foo', 'bar',
303+
'--baz.lulz', 'some value',
304+
'--baz.foob', 'thingy'
305+
];
306+
307+
var config = fetchConfig(__dirname, {
308+
argv: argv,
309+
seed: {
310+
baz: {
311+
lulz: 42
312+
},
313+
bar: 'foo'
314+
}
315+
});
316+
317+
assert.equal(config.get('foo'), 'bar');
318+
assert.equal(config.get('baz.lulz'), 42);
319+
assert.equal(config.get('baz.foob'), 'thingy');
320+
assert.equal(config.get('bar'), 'foo');
321+
322+
assert.end();
323+
});

0 commit comments

Comments
 (0)