diff --git a/README.md b/README.md index 3386d42..6209977 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/index.js b/index.js index 3b3e6b4..219a626 100755 --- a/index.js +++ b/index.js @@ -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) { diff --git a/lib/utils.js b/lib/utils.js index 5ce509d..6020ed0 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -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) @@ -31,6 +32,9 @@ var json = exports.json = function () { } catch (err) { return } + + files.push(file) + return parse(content) } @@ -94,4 +98,3 @@ var find = exports.find = function () { } return find(process.cwd(), rel) } - diff --git a/package.json b/package.json index fed237e..fe93a58 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/files.js b/test/files.js new file mode 100644 index 0000000..772d819 --- /dev/null +++ b/test/files.js @@ -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)