Skip to content

Commit

Permalink
implement injectable parsers
Browse files Browse the repository at this point in the history
  • Loading branch information
dominictarr committed Feb 2, 2015
1 parent eb2ef87 commit f289c02
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
27 changes: 17 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ var home = win
? process.env.USERPROFILE
: process.env.HOME

module.exports = function (name, defaults, argv) {
module.exports = function (name, defaults, argv, parse) {
if('string' !== typeof name)
throw new Error('rc(name): name *must* be string')
if(!argv)
Expand All @@ -18,22 +18,29 @@ module.exports = function (name, defaults, argv) {
? cc.json(defaults) : defaults
) || {}

parse = parse || cc.parse

function file () {
var content = cc.file.apply(null, arguments)
return content ? parse(content) : null
}

var local = cc.find('.'+name+'rc')

var env = cc.env(name + '_')

return deepExtend.apply(null, [
defaults,
win ? {} : cc.json(join(etc, name, 'config')),
win ? {} : cc.json(join(etc, name + 'rc')),
home ? cc.json(join(home, '.config', name, 'config')) : {},
home ? cc.json(join(home, '.config', name)) : {},
home ? cc.json(join(home, '.' + name, 'config')) : {},
home ? cc.json(join(home, '.' + name + 'rc')) : {},
cc.json(local),
win ? {} : file(join(etc, name, 'config')),
win ? {} : file(join(etc, name + 'rc')),
home ? file(join(home, '.config', name, 'config')) : {},
home ? file(join(home, '.config', name)) : {},
home ? file(join(home, '.' + name, 'config')) : {},
home ? file(join(home, '.' + name + 'rc')) : {},
file(local),
local ? {config: local} : null,
env.config ? cc.json(env.config) : null,
argv.config ? cc.json(argv.config) : null,
env.config ? file(env.config) : null,
argv.config ? file(argv.config) : null,
env,
argv
])
Expand Down
10 changes: 7 additions & 3 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var parse = exports.parse = function (content, file) {

}

var json = exports.json = function () {
var file = exports.file = function () {
var args = [].slice.call(arguments).filter(function (arg) { return arg != null })

//path.join breaks if it's a not a string, so just skip this.
Expand All @@ -27,11 +27,15 @@ var json = exports.json = function () {
var file = path.join.apply(null, args)
var content
try {
content = fs.readFileSync(file,'utf-8')
return fs.readFileSync(file,'utf-8')
} catch (err) {
return
}
return parse(content)
}

var json = exports.json = function () {
var content = file.apply(null, arguments)
return content ? parse(content) : null
}

var env = exports.env = function (prefix, env) {
Expand Down

0 comments on commit f289c02

Please sign in to comment.