Last active
December 17, 2015 16:59
-
-
Save pstaender/5642971 to your computer and use it in GitHub Desktop.
Merging of environments in config.yml including a getter to access config values by string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require('js-yaml') | |
data = require('./config.yml') | |
_ = require('underscore') | |
defaultEnv = 'development' | |
env = process.env.NODE_ENV or | |
config = _.extend(data[defaultEnv], data[env] or {}) | |
config.byString = (o, s) -> | |
a = s.split('.') | |
while a.length | |
n = a.shift() | |
if n of o | |
o = o[n] | |
else | |
return | |
o | |
config.get = (key, value) -> | |
if key?.split('.')?.length > 1 then config.byString(config,key) else config[key] or null | |
module.exports = exports = config | |
# Example: | |
# config.get('db.mongodb.url') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment