Skip to content

Commit

Permalink
Changes config key to point to latest parsed config file instead of…
Browse files Browse the repository at this point in the history
… only .namerc if it exists

Also adds `configs` key that contains all config files loaded
Adds documentation and test for `configs` key
  • Loading branch information
dbkaplun authored and Dan Kaplun committed Mar 19, 2015
1 parent 1f2ccd5 commit 0971fc3
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 20 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ var conf = {};
require('rc')(appname, conf);
```

If `rc` finds any config files for your app, the returned config object will have
a `configs` array containing their paths:

```javascript
var appCfg = require('rc')(appname, conf);
appCfg.configs[0] // /etc/appnamerc
appCfg.configs[1] // /home/dominictarr/.config/appname
appCfg.config // same as appCfg.configs[appCfg.configs.length - 1]
```

## Standards

Expand Down
46 changes: 26 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,36 @@ module.exports = function (name, defaults, argv, parse) {

parse = parse || cc.parse

function file () {
var content = cc.file.apply(null, arguments)
return content ? parse(content) : null
}
var env = cc.env(name + '_')

var local = cc.find('.'+name+'rc')
var configs = [defaults]
var configFiles = []
function addConfigFile (file) {
var fileConfig = cc.file(file)
if (fileConfig) {
configs.push(parse(fileConfig))
configFiles.push(file)
}
}

var env = cc.env(name + '_')
// which files do we look at?
if (!win)
[join(etc, name, 'config'),
join(etc, name + 'rc')].forEach(addConfigFile)
if (home)
[join(home, '.config', name, 'config'),
join(home, '.config', name),
join(home, '.' + name, 'config'),
join(home, '.' + name + 'rc')].forEach(addConfigFile)
addConfigFile(cc.find('.'+name+'rc'))
if (env.config) addConfigFile(env.config)
if (argv.config) addConfigFile(argv.config)

return deepExtend.apply(null, [
defaults,
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 ? file(env.config) : null,
argv.config ? file(argv.config) : null,
return deepExtend.apply(null, configs.concat([
env,
argv
])
argv,
configFiles.length ? {configs: configFiles, config: configFiles[configFiles.length - 1]} : null,
]))
}

if(!module.parent) {
Expand Down
3 changes: 3 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,7 @@ console.log(commentedJSON)

assert.equal(commentedJSON.option, false)
assert.equal(commentedJSON.envOption, 42)

assert.equal(commentedJSON.config, jsonrc)
assert.equal(commentedJSON.configs.length, 1)
assert.equal(commentedJSON.configs[0], jsonrc)

0 comments on commit 0971fc3

Please sign in to comment.