carcass-config
Version:
(Node.js) A config file loader and manager, in Carcass style.
80 lines (74 loc) • 2.44 kB
text/coffeescript
path = require('path')
carcass = require('carcass')
configurable = require('configurable')
configProto = require('./config')
parsers = require('../').parsers
_ = require('lodash')
###*
* Mixin this so your object / instance becomes a config manager. It will have
* the ability to load a stack of config files, parse them with a set of
* parsers, and store the result in the object / instance.
*
* `npm info configurable`
###
module.exports = _.merge(configProto, {
###*
* ENV is used in a config file name.
*
* {Function}
###
env: carcass.helpers.accessor('_env', ->
return process.env.NODE_ENV ? 'development'
)
###*
* We assume all the config files are in a same directory.
*
* {Function}
###
configDir: carcass.helpers.accessor('_configDir')
###*
* Initialize config.
*
* Note that the parsers and the file stacks are just some nice defaults. You
* can safely override this.
###
initConfig: (filename = 'config.json') ->
configurable(@)
# Default parsers.
# Stack config file, plus some variations.
dir =
return @ if not dir?
ext = path.extname(filename)
name = path.basename(filename, ext)
# Default, i.e.`config.default.json`.
# The config, i.e.`config.json`.
# Environment, `config.development.json` etc.
return @
###*
* In addition to loading the configs, it also extends settings.
###
reload: (done = ->) ->
###*
* Helper; get an instance of a class and set some defaults.
###
getConsumer: (name, args...) ->
if _.isFunction(name)
# A provided class.
ins = new name(args...)
ins.configManager?(@)
else if ? and _.isFunction([name])
# Otherwise an internal class.
ins = new [name](args...)
ins.configManager?(@)
# TODO: otherwise throw.
return ins
})