confab
A tool for describing application configurations!
Contents
Convention and Configuration
Confab is configuration-first by nature, as the details of configuration may vary widely from one project to the next. Nevertheless, the built-in transformations reflect certain opinions.
Namely, configuration should be:
separate. Keeping configuration isolated from application logic eases deployment across multiple environments. Confab encourages developers to author complete configurations independent of the application.
predictable. Like any other exception, errors in configuration should be immediately fatal. All confab transformations will fail immediately if unexpected conditions are encountered, while the
required
transformation can assert the presence of certain configuration keys. Similarly, thedefaults
transformation--while unquestionably useful--should be approached with care.immutable. The running application should not be concerned with configuration changes: if a change must be applied it should be applied to a new process. The
freeze
transformation guarantees that a config will not change after initialization.simple. File-based configs (JSON, YAML, etc.) make it easy to nest data inside multiple levels of keys. This is convenient for grouping like data, but it is not immediately clear how these data would map to (e.g.) environment variables or command-line arguments. Sub-configurations can enhance separation between unrelated concerns, but they should be used with care.
And one non-opinion
- Command-line parsing, and what impact (if any) arguments should have on the configuration is left as a project-specific decision. No transformations are provided for command-line support--but you can write your own!
confab
(definition)
Build a configuration object from a list of transformations:
var confab = require('confab');
var config = confab([
confab.loadJSON([
'./config.' + process.env.NODE_ENV + '.json',
'./config.json'
]),
confab.defaults({
role: 'api',
port: 3200
})
]);
Later, access the configuration exactly as you would expect.
console.log(config.role); // 'api'
Arguments
Type | Name | Description |
---|---|---|
Array |
transforms | a list of transformation functions |
loadJSON
(definition)
Load a configuration from a list of candidate files.
Extends the configuration with the contents of the configuration file at the
first matched path
, falling back to subsequent path
s if the first cannot
be found. Throws a ReferenceError
if none of the specified paths are
matched.
var loadJSONTransform = confab.loadJSON([
'./config.json',
__dirname + '/config.json',
process.env.HOME + '/config.json'
]);
Arguments
Type | Name | Description |
---|---|---|
Array|String |
paths | one or more paths to search for an appropriate config file |
loadEnvironment
(definition)
Map environment variables (if set) to keys in the config. Unset variables are ignored.
var envTransform = confab.loadEnvironment({
'NODE_ENV': 'environment',
'PORT': 'port'
});
Arguments
Type | Name | Description |
---|---|---|
Object |
map | a list of environment variables to map to config keys |
Object |
options | a list of options. |
assign
(definition)
Merge an object with the existing config, replacing any existing keys with
the assign
-ed values.
var envTransform = confab.assign({
port: '1492'
});
Arguments
Type | Name | Description |
---|---|---|
Object |
obj | an object to merge into the config |
Object |
obj2... | additional objects to merge into the config |
required
(definition)
List required config fields and throw a ReferenceError
with a list of any
missing fields. If all fields are present, required
acts as a pass-through.
Arguments
Type | Name | Description |
---|---|---|
Array |
required | the names of required fields in the config |
defaults
(definition)
Fill in any missing values in config with specified defaults
var defaultsTransform = confab.defaults({
port: 3200,
logLevel: 'debug'
});
Arguments
Type | Name | Description |
---|---|---|
Object |
defaults | default values to fill into the config |
Boolean |
warn | (Optional) print warning to `stdout` when a default value is being used |
freeze
(definition)
Freeze the config (hint: run this transformation last!)