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

Commit a219f6a

Browse files
committed
support dcValue feature
1 parent 5651814 commit a219f6a

File tree

4 files changed

+55
-13
lines changed

4 files changed

+55
-13
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,13 @@ A zero configuration configuration loader
3131

3232
```js
3333
// server.js
34+
var fs = require('fs');
3435
var fetchConfig = require('playdoh-server/config')
3536

36-
var NODE_ENV = process.env.NODE_ENV
3737
var config = fetchConfig(__dirname, {
38-
dc: NODE_ENV === 'production' ?
39-
'/etc/playdoh/datacenter' : null
38+
dcValue: fs.existsSync('/etc/zero-config/datacenter') ?
39+
fs.readFileSync('/etc/zero-config/datacenter', 'utf8') :
40+
null
4041
})
4142

4243
var port = config.get("port")

errors.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ var MissingDatacenter = TypedError({
1818

1919
var DatacenterRequired = TypedError({
2020
type: 'datacenter.option.required',
21-
message: 'expected `opts.dc` to be passed to fetchConfig.\n' +
22-
'must call `fetchConfig(__dirname, { dc: "..." }).\n' +
21+
message: 'expected `opts.dcValue` to be passed to fetchConfig.\n' +
22+
'must call `fetchConfig(__dirname, { dcValue: "..." }).\n' +
2323
'instead I got opts: {strOpts}.\n' +
24-
'`opts.dc` is not optional when NODE_ENV is "production".\n' +
24+
'`opts.dcValue` is not optional when NODE_ENV is "production".\n' +
2525
'SUGGESTED FIX: update the `fetchConfig()` callsite.\n'
2626
});
2727

read-datacenter.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,21 @@ function readDatacenter(opts) {
99
var env = opts.env || process.env;
1010
var NODE_ENV = env.NODE_ENV;
1111

12-
if (NODE_ENV === 'production' && !opts.dc) {
12+
// specifying a datacenter is optional in dev but required
13+
// in production.
14+
if (NODE_ENV === 'production' && !opts.dc && !opts.dcValue) {
1315
throw errors.DatacenterRequired({
1416
strOpts: JSON.stringify(opts)
1517
});
1618
}
1719

1820
var result;
1921

20-
// specifying a datacenter is optional in dev but required
21-
// in production.
22-
if (opts.dc) {
22+
if (opts.dcValue) {
23+
result = Result.Ok({
24+
'datacenter': opts.dcValue.replace(/\s/g, '')
25+
});
26+
} else if (opts.dc) {
2327
var fileResult = readFileOrError(opts.dc);
2428
if (Result.isErr(fileResult)) {
2529
var err = Result.Err(fileResult);

test/index.js

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,43 @@ test('config loads from datacenter file', withFixtures(__dirname, {
137137
assert.end();
138138
}));
139139

140+
test('config loads from dcValue', withFixtures(__dirname, {
141+
'config': {
142+
'common.json': JSON.stringify({
143+
a: 'a',
144+
b: {
145+
c: 'c',
146+
d: 'd'
147+
}
148+
}),
149+
'production.json': JSON.stringify({
150+
b: {
151+
c: 'c2'
152+
}
153+
}),
154+
'production.peak1.json': JSON.stringify({
155+
a: 'a3'
156+
})
157+
}
158+
}, function (assert) {
159+
var env = {
160+
'NODE_ENV': 'production'
161+
};
162+
163+
var config = fetchConfig(__dirname, {
164+
env: env,
165+
dcValue: 'peak1'
166+
});
167+
168+
assert.equal(config.get('datacenter'), 'peak1');
169+
assert.equal(config.get('a'), 'a3');
170+
assert.equal(config.get('b.c'), 'c2');
171+
assert.equal(config.get('b.d'), 'd');
172+
assert.deepEqual(config.get('b'), { c: 'c2', d: 'd' });
173+
174+
assert.end();
175+
}));
176+
140177
test('config reads a datacenter file', withFixtures(__dirname, {
141178
datacenter: 'peak1'
142179
}, function (assert) {
@@ -201,7 +238,7 @@ test('will load from --config', withFixtures(__dirname, {
201238
assert.end();
202239
}));
203240

204-
test('no opts.dc in production', function (assert) {
241+
test('no opts.dcValue in production', function (assert) {
205242
var err = catchFn(function () {
206243
fetchConfig(__dirname, {
207244
env: { NODE_ENV: 'production' }
@@ -210,8 +247,8 @@ test('no opts.dc in production', function (assert) {
210247

211248
assert.ok(err);
212249
assert.equal(err.type, 'datacenter.option.required');
213-
assert.ok(/expected `opts.dc`/.test(err.message));
214-
assert.ok(/`opts.dc` is not optional/.test(err.message));
250+
assert.ok(/expected `opts.dcValue`/.test(err.message));
251+
assert.ok(/`opts.dcValue` is not optional/.test(err.message));
215252

216253
assert.end();
217254
});

0 commit comments

Comments
 (0)