Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implements #23, report which config files were loaded #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ Given your application name (`appname`), rc will look in all the obvious places
All configuration sources that were found will be flattened into one object,
so that sources **earlier** in this list override later ones.

## Which files were loaded?

The returned configuration object will have an non-enumerable array property `_rcfiles` that reports which files were loaded.

```javascript
var conf = require('rc')
console.log('Loaded', conf._rcfiles)
```

## Configuration File Formats

Expand Down
34 changes: 23 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,46 @@ var home = win
: process.env.HOME

module.exports = function (name, defaults, argv) {
var files = []

if('string' !== typeof name)
throw new Error('rc(name): name *must* be string')
if(!argv)
argv = require('minimist')(process.argv.slice(2))
defaults = (
'string' === typeof defaults
? cc.json(defaults) : defaults
? cc.json(files, defaults) : defaults
) || {}

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

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

return deepExtend.apply(null, [
var conf = 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 ? {} : cc.json(files, join(etc, name, 'config')),
win ? {} : cc.json(files, join(etc, name + 'rc')),
home ? cc.json(files, join(home, '.config', name, 'config')) : {},
home ? cc.json(files, join(home, '.config', name)) : {},
home ? cc.json(files, join(home, '.' + name, 'config')) : {},
home ? cc.json(files, join(home, '.' + name + 'rc')) : {},
cc.json(files, local),
local ? {config: local} : null,
env.config ? cc.json(env.config) : null,
argv.config ? cc.json(argv.config) : null,
env.config ? cc.json(files, env.config) : null,
argv.config ? cc.json(files, argv.config) : null,
env,
argv
])

// reverse the file list to be more in line with the readme
// i.e. so early entries in the list would have overridden later ones
files.reverse()

Object.defineProperty(conf, '_rcfiles', {
value: files
})

return conf
}

if(!module.parent) {
Expand Down
7 changes: 5 additions & 2 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ var parse = exports.parse = function (content, file) {
}

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

//path.join breaks if it's a not a string, so just skip this.
for(var i in args)
Expand All @@ -31,6 +32,9 @@ var json = exports.json = function () {
} catch (err) {
return
}

files.push(file)

return parse(content)
}

Expand Down Expand Up @@ -94,4 +98,3 @@ var find = exports.find = function () {
}
return find(process.cwd(), rel)
}

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "index.js",
"browserify": "browser.js",
"scripts": {
"test": "set -e; node test/test.js; node test/ini.js; node test/nested-env-vars.js"
"test": "set -e; node test/test.js; node test/ini.js; node test/nested-env-vars.js; node test/files.js"
},
"repository": {
"type": "git",
Expand Down
24 changes: 24 additions & 0 deletions test/files.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
var fs = require('fs')
var path = require('path')
var n = 'rc'+Math.random()
var assert = require('assert')
var jsonrc = path.resolve('.' + n + 'rc')

fs.writeFileSync(jsonrc, [
'{',
'"option": false,',
'"otherOption": 24',
'}'
].join('\n'));

var config = require('../')(n)

fs.unlinkSync(jsonrc);

console.log('\n\n------ report loaded configuration files ------\n', config)

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

// should not be enumerable
assert.equal(Object.keys(config).indexOf('_rcfiles'), -1)