3 USE THIS MODULE TO LOAD ALL YOUR CONFIGURATIONS
7 //npm install config-chain
9 var cc = require('config-chain')
10 , opts = require('optimist').argv //ALWAYS USE OPTIMIST FOR COMMAND LINE OPTIONS.
11 , env = opts.env || process.env.YOUR_APP_ENV || 'dev' //SET YOUR ENV LIKE THIS.
13 // EACH ARG TO CONFIGURATOR IS LOADED INTO CONFIGURATION CHAIN
14 // EARLIER ITEMS OVERIDE LATER ITEMS
15 // PUTS COMMAND LINE OPTS FIRST, AND DEFAULTS LAST!
17 //strings are interpereted as filenames.
18 //will be loaded synchronously
22 //OVERRIDE SETTINGS WITH COMMAND LINE OPTS
25 //ENV VARS IF PREFIXED WITH 'myApp_'
27 cc.env('myApp_'), //myApp_foo = 'like this'
30 path.join(__dirname, 'config.' + env + '.json'),
32 //IF `env` is PRODUCTION
34 ? path.join(__dirname, 'special.json') //load a special file
35 : null //NULL IS IGNORED!
37 //SUBDIR FOR ENV CONFIG
38 path.join(__dirname, 'config', env, 'config.json'),
40 //SEARCH PARENT DIRECTORIES FROM CURRENT DIR FOR FILE
41 cc.find('config.json'),
49 var host = conf.get('host')
53 var host = conf.store.host
57 FINALLY, EASY FLEXIBLE CONFIGURATIONS!
59 ##see also: [proto-list](https://github.com/isaacs/proto-list/)
63 YOU WANT A "CLASS" SO THAT YOU CAN DO CRAYCRAY JQUERY CRAPS?
65 EXTEND WITH YOUR OWN FUNCTIONALTY!?
67 ## CONFIGCHAIN LIVES TO SERVE ONLY YOU!
70 var cc = require('config-chain')
72 // all the stuff you did before
76 cc.find('config.json'),
79 // CONFIGS AS A SERVICE, aka "CaaS", aka EVERY DEVOPS DREAM OMG!
80 .addUrl('http://configurator:1234/my-configs')
82 .addFile('/path/to/file.json')
84 // OBJECTS ARE OK TOO, they're SYNC but they still ORDER RIGHT
85 // BECAUSE PROMISES ARE USED BUT NO, NOT *THOSE* PROMISES, JUST
86 // ACTUAL PROMISES LIKE YOU MAKE TO YOUR MOM, KEPT OUT OF LOVE
87 .add({ another: 'object' })
89 // DIE A THOUSAND DEATHS IF THIS EVER HAPPENS!!
90 .on('error', function (er) {
91 // IF ONLY THERE WAS SOMETHIGN HARDER THAN THROW
92 // MY SORROW COULD BE ADEQUATELY EXPRESSED. /o\
96 // THROW A PARTY IN YOUR FACE WHEN ITS ALL LOADED!!
97 .on('load', function (config) {
98 console.awesome('HOLY SHIT!')
106 MAKE A CHAIN AND ADD ALL THE ARGS.
108 If the arg is a STRING, then it shall be a JSON FILENAME.
116 Join the args INTO A JSON FILENAME!
120 ## cc.find(relativePath)
122 SEEK the RELATIVE PATH by climbing the TREE OF DIRECTORIES.
124 RETURN THE FOUND PATH!
128 ## cc.parse(content, file, type)
130 Parse the content string, and guess the type from either the
131 specified type or the filename.
133 RETURN THE RESULTING OBJECT!
137 ## cc.env(prefix, env=process.env)
139 Get all the keys on the provided env object (or process.env) which are
140 prefixed by the specified prefix, and put the values on a new object.
142 RETURN THE RESULTING OBJECT!
148 The ConfigChain class for CRAY CRAY JQUERY STYLE METHOD CHAINING!
150 One of these is returned by the main exported function, as well.
152 It inherits (prototypically) from
153 [ProtoList](https://github.com/isaacs/proto-list/), and also inherits
155 [EventEmitter](http://nodejs.org/api/events.html#events_class_events_eventemitter)
157 It has all the methods from both, and except where noted, they are
160 ### LET IT BE KNOWN THAT chain IS AN INSTANCE OF ConfigChain.
164 A list of all the places where it got stuff. The keys are the names
165 passed to addFile or addUrl etc, and the value is an object with some
166 info about the data source.
168 ## chain.addFile(filename, type, [name=filename])
170 Filename is the name of the file. Name is an arbitrary string to be
171 used later if you desire. Type is either 'ini' or 'json', and will
172 try to guess intelligently if omitted.
174 Loaded files can be saved later.
176 ## chain.addUrl(url, type, [name=url])
178 Same as the filename thing, but with a url.
180 Can't be saved later.
182 ## chain.addEnv(prefix, env, [name='env'])
184 Add all the keys from the env object that start with the prefix.
186 ## chain.addString(data, file, type, [name])
188 Parse the string and add it to the set. (Mainly used internally.)
190 ## chain.add(object, [name])
192 Add the object to the set.
194 ## chain.root {Object}
196 The root from which all the other config objects in the set descend
199 Put your defaults here.
201 ## chain.set(key, value, name)
203 Set the key to the value on the named config object. If name is
204 unset, then set it on the first config object in the set. (That is,
205 the one with the highest priority, which was added first.)
207 ## chain.get(key, [name])
209 Get the key from the named config object explicitly, or from the
210 resolved configs if not specified.
212 ## chain.save(name, type)
214 Write the named config object back to its origin.
216 Currently only supported for env and file config types.
218 For files, encode the data according to the type.
220 ## chain.on('save', function () {})
222 When one or more files are saved, emits `save` event when they're all
225 ## chain.on('load', function (chain) {})
227 When the config chain has loaded all the specified files and urls and
228 such, the 'load' event fires.