rconf v1.1.1
rconf
An easy-to-use, powerful configuration package.
Features
- Loading values from files
- Passing values as command-line arguments
- Automatically generating
--help
text - A configuration file to define the configuration of your configuration.
Usage
In the root of your project, create a folder named config
, and create a file named _opts.json
inside it. This file will contain the names, descriptions, and default values of your config options. The file should be formatted like this:
{
/* Name: [ Description, Flag, Type, Default value */
"address": ["The address to deliver the cake to", "-a", "string", "123 Cakewalk Lane"],
"slices": ["How many slices of cake are needed", "-s", "number", 3]
}
Now you can load the config in your code like so:
var config = require('rconf');
console.log('Delivering ' + config.slices + ' slices of cake to ' + config.address);
And now you can run it in the console
$ node cakedelivery.js
Delivering 3 slices of cake to 123 Cakewalk Lane
$ node cakedelivery.js --slices 7436
Delivering 7436 slices of cake to 123 Cakewalk Lane
$ node cakedelivery.js -a "456 Pastry Drive"
Delivering 3 slices of cake to 456 Pastry Drive
Rconf will also generate a help message
$ node cakedelivery.js --help
--address -a ['123 Cakewalk Lane'] The address to deliver the cake to
--slices -s [3] How many slices of cake are needed
Configuration files
Rconf can also load config values from files. By default, it tries to load values from config/local.json
and config/$NODE_ENV.json
. To test this, create the config/local.json
file and format it like so:
{
"address": "789 Frosting Road",
"slices": 5
}
These values will now override the default values set in _opts.json
, but will be overridden by command-line arguments.
Rconf also loads a file for the current NODE_ENV. For instance, if the NODE_ENV is set to "production"
, config/production.json
will be loaded. The values in production.json
override local.json
and _opts.json
, but are still overridden by command-line arguments.
Hidden values
When you run your script with the --help
option, it will display the preset values for each option. However, some values may need to be kept secret (API keys, database passwords, etc.). To hide these values, add a fifth element to the end of the option's array in _opts.json
, set to true.
"address": ["The address to deliver the cake to", "-a", "string", "123 Cakewalk Lane"],
"slices": ["How many slices of cake are needed", "-s", "number", 3]
"recipe": ["The secret recipe used to make the cake", "-r", "string", "", true] // Note the extra value
Now, any values set in the configuration files for recipe
will be redacted in the help text, like so:
$ node cakedelivery.js --help
--address -a ['123 Cakewalk Lane'] The address to deliver the cake to
--slices -s [3] How many slices of cake are needed
--recipe -r [hidden] The secret recipe used to make the cake