Skip to content

Commit d85c68e

Browse files
committed
Merge pull request #2 from timruffles/master
add `disableFallbacks()` to disable development defaults
2 parents 7e9c645 + a9885da commit d85c68e

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,16 @@ var config = getenv.multi({
9898

9999
```
100100

101+
### env.disableFallbacks()
102+
103+
Disallows fallbacks in environments where you don't want to rely on brittle development defaults (e.g production, integration testing). For example, to disable fallbacks if we indicate production via `NODE_ENV`:
104+
105+
```javascript
106+
if (process.env.NODE_ENV === 'production') {
107+
getenv.disableFallbacks();
108+
}
109+
```
110+
101111
## Changelog
102112

103113
### v0.3.0
@@ -115,6 +125,7 @@ var config = getenv.multi({
115125
- Christoph Tavan <[email protected]>
116126
- Jonas Dohse <[email protected]>
117127
- Jan Lehnardt (@janl): `getenv.multi()` support.
128+
- Tim Ruffles <[email protected]>: `disableFallbacks()`
118129

119130
## License
120131

lib/getenv.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
var util = require("util");
22

3+
var fallbacksDisabled = false;
4+
35
function _value(varName, fallback) {
46
var value = process.env[varName];
57
if (value === undefined) {
68
if (fallback === undefined) {
79
throw new Error('GetEnv.Nonexistent: ' + varName + ' does not exist ' +
810
'and no fallback value provided.');
911
}
12+
if (fallbacksDisabled) {
13+
throw new Error('GetEnv.DisabledFallbacks: ' + varName + ' relying on fallback ' +
14+
'when fallbacks have been disabled');
15+
}
1016
return '' + fallback;
1117
}
1218
return value;
@@ -98,4 +104,8 @@ getenv.multi = function multi(spec) {
98104
return result;
99105
};
100106

107+
getenv.disableFallbacks = function() {
108+
fallbacksDisabled = true;
109+
};
110+
101111
module.exports = getenv;

test/fallbacks.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
var assert = require('assert');
2+
3+
var getenv = require('../lib/getenv');
4+
5+
var tests = {};
6+
7+
// order dependent test
8+
tests['getenv.disableFallbacks() makes relying on fallbacks an error'] = function() {
9+
getenv.disableFallbacks();
10+
assert.throws(function() {
11+
getenv.string("url", "http://localhost");
12+
});
13+
};
14+
15+
16+
Object.keys(tests).forEach(function(key) {
17+
console.log('Test: %s', key);
18+
tests[key]();
19+
});

0 commit comments

Comments
 (0)