carcass-config
Version:
(Node.js) A config file loader and manager, in Carcass style.
75 lines (69 loc) • 2.18 kB
text/coffeescript
# debug = require('debug')('carcass:config')
carcass = require('carcass')
highland = carcass.highland
_ = require('lodash')
###*
* Mixin this so your object / instance can become a config loader, which can
* have a stack of sources, a parser, and can load the sources with the
* parser and merge (a deep merge) the results together.
*
* {Object}
###
module.exports = {
###*
* A stack of sources.
*
* {Function}
###
source: carcass.helpers.stacker('_sources')
###*
* One parser, or an array of parsers.
*
* {Function}
###
parser: carcass.helpers.accessor('_parser')
###*
* Builder; returns a function which can be used to map a stream with a
* given parser.
*
* {Function|Object} parser can be either a function or an object, in
* which case the parser.parse() will be used.
*
* {Function} curried map().
*
*
###
_mapWith: (parser) ->
return highland.map((item) ->
return parser(item) if _.isFunction(parser)
return parser.parse(item) if _.isObject(parser) and parser.parse?
return item
)
###*
* Loads all the sources and parses with a given parser, and merges the
* results together.
*
* Bad results (false, null, undefined) are skipped.
*
* {Function|null} done the callback, if provided, will be called
* with the result, and if not provided, the stream will be returned.
*
* {this|stream} depends on whether a callback is provided.
*
*
###
_load: (done) ->
parser =
stream = highland()
# Either a single parser or an array of parsers.
if _.isArray(parser)
stream = .flatten().compact() for p in parser
else
stream = .flatten().compact()
# Deeply merge all the results.
stream = stream.reduce({}, _.merge)
# TODO: stream.errors()
return stream if not done?
stream.pull(done)
return @
}