Browse files

Merge branch 'add/env'

  • Loading branch information...
2 parents 248f8e2 + 584d873 commit 5f6f64703dd4218b7ac5329a80f87537c42f1011 @tj committed Jun 28, 2012
Showing with 51 additions and 1 deletion.
  1. +13 −0 Readme.md
  2. +1 −0 lib/eson.js
  3. +13 −0 lib/plugins/env.js
  4. +1 −1 test/bools.js
  5. +23 −0 test/env.js
View
13 Readme.md
@@ -109,6 +109,19 @@ yields:
Convert "yes", "no", "enabled", "disabled" into booleans.
+### eson.env([prefix])
+
+ Allow environment variables to define config values. If you have the following:
+
+```js
+{
+ "upload path": "/data/uploads"
+}
+```
+
+ You could then export `UPLOAD_PATH=/tmp` to change this value. Optionally when
+ a `prefix` is given such as "MYAPP_" then you must prefix such as `MYAPP_UPLOAD_PATH=/tmp`.
+
### eson.replace(str, val)
The replace plugin allows you to replace arbitrary substrings, useful
View
1 lib/eson.js
@@ -22,6 +22,7 @@ exports.version = '0.0.1';
*/
exports.ms = require('./plugins/ms');
+exports.env = require('./plugins/env');
exports.glob = require('./plugins/glob');
exports.bools = require('./plugins/bools');
exports.replace = require('./plugins/replace');
View
13 lib/plugins/env.js
@@ -0,0 +1,13 @@
+
+/**
+ * Allow environment variables to take precedence over `val`,
+ * with optional `prefix`.
+ */
+
+module.exports = function(prefix){
+ prefix = prefix || '';
+ return function(key, val){
+ var name = prefix + key.toUpperCase().split(' ').join('_');
+ return process.env[name] || val;
+ }
+};
View
2 test/bools.js
@@ -2,7 +2,7 @@
var Parser = require('../')
, bools = Parser.bools;
-describe('ms', function(){
+describe('bools', function(){
it('should parse string bool representations', function(){
bools('', 'yes').should.be.true;
bools('', 'enabled').should.be.true;
View
23 test/env.js
@@ -0,0 +1,23 @@
+
+var Parser = require('../')
+ , env = Parser.env;
+
+describe('env(prefix)', function(){
+ it('should allow setting environment variables', function(){
+ var fn = env();
+ fn('foo', 'bar').should.equal('bar');
+ process.env.FOO = 'baz';
+ fn('foo', 'bar').should.equal('baz');
+ process.env.DEV_UI = 'yes';
+ fn('dev ui', 'no').should.equal('yes');
+ })
+
+ it('should accept a prefix', function(){
+ var fn = env('NB_');
+ fn('dev ui', 'no').should.equal('no');
+ process.env.DEV_UI = 'yes';
+ fn('dev ui', 'no').should.equal('no');
+ process.env.NB_DEV_UI = 'yes';
+ fn('dev ui', 'no').should.equal('yes');
+ })
+})

0 comments on commit 5f6f647

Please sign in to comment.