rc
The non-configurable configuration loader for lazy people.
Usage
The only option is to pass rc the name of your app, and your default configuration.
var conf = appname //defaults go here. port: 2468 //defaults which are objects will be merged, not replaced views: engine: 'jade' ;
rc
will return your configuration options merged with the defaults you specify.
If you pass in a predefined defaults object, it will be mutated:
var conf = {};appname conf;
Standards
Given your application name (appname
), rc will look in all the obvious places for configuration.
- command line arguments (parsed by minimist)
- environment variables prefixed with
${appname}_
- or use "__" to indicate nested properties
(e.g.appname_foo__bar__baz
=>foo.bar.baz
)
- or use "__" to indicate nested properties
- if you passed an option
--config file
then from that file - a local
.${appname}rc
or the first found looking in./ ../ ../../ ../../../
etc. $HOME/.${appname}rc
$HOME/.${appname}/config
$HOME/.config/${appname}
$HOME/.config/${appname}/config
/etc/${appname}rc
/etc/${appname}/config
- the defaults object you passed in.
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.
var conf = console
Configuration File Formats
Configuration files (e.g. .appnamerc
) may be in either json or ini format. The example configurations below are equivalent:
ini
Formatted as ; You can include comments in `ini` format if you want.
dependsOn=0.10.0
; `rc` has built-in support for ini sections, see?
[commands]
www = ./commands/www
console = ./commands/repl
; You can even do nested sections
[generators.options]
engine = ejs
[generators.modules]
new = generate-new
engine = generate-backend
json
Formatted as
Comments are stripped from JSON config via strip-json-comments.
Since ini, and env variables do not have a standard for types, your application needs be prepared for strings.
Advanced Usage
argv
Pass in your own You may pass in your own argv
as the third argument to rc
. This is in case you want to use your own command-line opts parser.
appname defaults customArgvParser;
Note on Performance
rc
is running fs.statSync
-- so make sure you don't use it in a hot code path (e.g. a request handler)
License
BSD / MIT / Apache2