1 | # confit
|
2 |
|
3 | Simple, environment-based configuration. `confit` loads a default JSON
|
4 | configuration file, additionally loading environment-specific files, if applicable.
|
5 | It will also process the loaded files using any configured
|
6 | [shortstop](https://github.com/paypal/shortstop) protocol handlers.
|
7 | (See **Options** below.)
|
8 |
|
9 | [![Build Status](https://travis-ci.org/totherik/confit.png)](https://travis-ci.org/totherik/confit)
|
10 |
|
11 | ## Usage
|
12 | ```javascript
|
13 | var confit = require('confit');
|
14 | ```
|
15 |
|
16 | ### confit(options, callback);
|
17 | * `options` (*String* | *Object*) - the base directory in which config files live
|
18 | or a configuration object.
|
19 | * `callback` (*Function*) - the callback to be called with the error or config object.
|
20 | Signature `function (err, config) { /* ... */}`
|
21 |
|
22 | ```javascript
|
23 | 'use strict';
|
24 |
|
25 | var path = require('path');
|
26 | var confit = require('confit');
|
27 |
|
28 | var basedir = path.join(__dirname, 'config');
|
29 | confit(basedir, function (err, config) {
|
30 | config.get; // Function
|
31 | config.set; // Function
|
32 | config.use; // Function
|
33 |
|
34 | config.get('env:env'); // 'development'
|
35 | });
|
36 | ```
|
37 |
|
38 | ## Options
|
39 | * `basedir` (*String*) - the base directory in which config files can be found.
|
40 | * `protocols` (*Object*) - An object containing a mapping of
|
41 | [shortstop](https://github.com/paypal/shortstop) protocols to handler implementations.
|
42 | This protocols will be used to process the config data prior to registration.
|
43 | * `defaults` (*String*) - the name of the file containing all default values.
|
44 | Defaults to `config.json`.
|
45 |
|
46 | ```javascript
|
47 | 'use strict';
|
48 |
|
49 | var path = require('path');
|
50 | var confit = require('confit');
|
51 | var handlers = require('shortstop-handlers');
|
52 |
|
53 |
|
54 | var options = {
|
55 | basedir: path.join(__dirname, 'config');
|
56 | protocols: {
|
57 | file: handlers.file,
|
58 | glob: handlers.glob
|
59 | }
|
60 | };
|
61 |
|
62 | confit(options, function (err, config) {
|
63 | config.get; // Function
|
64 | config.set; // Function
|
65 | config.use; // Function
|
66 |
|
67 | config.get('env:env'); // 'development'
|
68 | });
|
69 | ```
|
70 |
|
71 |
|
72 | ## API
|
73 | * `get(key)` - Retrieve the value for a given key.
|
74 | * `set(key, value)` - Set a value for the given key.
|
75 | * `use(obj)` - merge provided object into config.
|
76 |
|
77 | ```javascript
|
78 | config.set('foo', 'bar');
|
79 | config.get('foo'); // 'bar'
|
80 |
|
81 | config.use({ foo: 'baz' });
|
82 | config.get('foo'); // 'baz'
|
83 | ```
|
84 |
|
85 | ## Default Behavior
|
86 | By default, `confit` loads `process.env` and `argv` values upon initialization. Additionally,
|
87 | it creates convenience environment properties prefixed with `env:` based on the
|
88 | current `NODE_ENV` setting, defaulting to `development`. It also normalizes
|
89 | `NODE_ENV` settings to the long form, so `dev` becomes `development`, `prod`
|
90 | becomes `production`, etc.
|
91 | ```javascript
|
92 | // NODE_ENV='dev'
|
93 | config.get('NODE_ENV'); // 'dev'
|
94 | config.get('env:env'); // 'development'
|
95 | config.get('env:development'); // true
|
96 | config.get('env:test'); // false
|
97 | config.get('env:staging'); // false
|
98 | config.get('env:production'); // false
|
99 | ```
|
100 |
|
101 | ```javascript
|
102 | // NODE_ENV='custom'
|
103 | config.get('NODE_ENV'); // 'custom'
|
104 | config.get('env:env'); // 'custom'
|
105 | config.get('env:development'); // false
|
106 | config.get('env:test'); // false
|
107 | config.get('env:staging'); // false
|
108 | config.get('env:production'); // false
|
109 | config.get('env:custom'); // true
|
110 | ```
|