1 var ProtoList = require('proto-list')
2 , path = require('path')
5 , EE = require('events').EventEmitter
7 , http = require('http')
9 var exports = module.exports = function () {
10 var args = [].slice.call(arguments)
11 , conf = new ConfigChain()
16 ( 'string' === typeof a
24 //recursively find a file...
26 var find = exports.find = function () {
27 var rel = path.join.apply(null, [].slice.call(arguments))
29 function find(start, rel) {
30 var file = path.join(start, rel)
35 if(path.dirname(start) !== start) // root
36 return find(path.dirname(start), rel)
39 return find(__dirname, rel)
42 var parse = exports.parse = function (content, file, type) {
43 content = '' + content
44 // if we don't know what it is, try json and fall back to ini
45 // if we know what it is, then it must be that.
47 try { return JSON.parse(content) }
48 catch (er) { return ini.parse(content) }
49 } else if (type === 'json') {
51 try { return JSON.parse(content) }
52 catch (er) { this.emit('error', er) }
54 return JSON.parse(content)
57 return ini.parse(content)
61 var json = exports.json = function () {
62 var args = [].slice.call(arguments).filter(function (arg) { return arg != null })
63 var file = path.join.apply(null, args)
66 content = fs.readFileSync(file,'utf-8')
70 return parse(content, file, 'json')
73 var env = exports.env = function (prefix, env) {
74 env = env || process.env
78 if(k.indexOf(prefix) === 0)
79 obj[k.substring(l)] = env[k]
85 exports.ConfigChain = ConfigChain
86 function ConfigChain () {
88 ProtoList.apply(this, arguments)
94 // multi-inheritance-ish
96 constructor: { value: ConfigChain }
98 Object.keys(EE.prototype).forEach(function (k) {
99 extras[k] = Object.getOwnPropertyDescriptor(EE.prototype, k)
101 ConfigChain.prototype = Object.create(ProtoList.prototype, extras)
103 ConfigChain.prototype.del = function (key, where) {
104 // if not specified where, then delete from the whole chain, scorched
107 var target = this.sources[where]
108 target = target && target.data
110 return this.emit('error', new Error('not found '+where))
114 for (var i = 0, l = this.list.length; i < l; i ++) {
115 delete this.list[i][key]
121 ConfigChain.prototype.set = function (key, value, where) {
125 target = this.sources[where]
126 target = target && target.data
128 return this.emit('error', new Error('not found '+where))
131 target = this.list[0]
133 return this.emit('error', new Error('cannot set, no confs!'))
140 ConfigChain.prototype.get = function (key, where) {
142 where = this.sources[where]
143 if (where) where = where.data
144 if (where && Object.hasOwnProperty.call(where, key)) return where[key]
147 return this.list[0][key]
150 ConfigChain.prototype.save = function (where, type, cb) {
151 if (typeof type === 'function') cb = type, type = null
152 var target = this.sources[where]
153 if (!target || !(target.path || target.source) || !target.data) {
154 // TODO: maybe save() to a url target could be a PUT or something?
155 // would be easy to swap out with a reddis type thing, too
156 return this.emit('error', new Error('bad save target: '+where))
160 var pref = target.prefix || ''
161 Object.keys(target.data).forEach(function (k) {
162 target.source[pref + k] = target.data[k]
167 var type = type || target.type
168 var data = target.data
169 if (target.type === 'json') {
170 data = JSON.stringify(data)
172 data = ini.stringify(data)
176 fs.writeFile(target.path, data, 'utf8', function (er) {
179 if (cb) return cb(er)
180 else return this.emit('error', er)
182 if (this._saving === 0) {
190 ConfigChain.prototype.addFile = function (file, type, name) {
192 var marker = {__source__:name}
193 this.sources[name] = { path: file, type: type }
196 fs.readFile(file, 'utf8', function (er, data) {
197 if (er) this.emit('error', er)
198 this.addString(data, file, type, marker)
203 ConfigChain.prototype.addEnv = function (prefix, env, name) {
205 var data = exports.env(prefix, env)
206 this.sources[name] = { data: data, source: env, prefix: prefix }
207 return this.add(data, name)
210 ConfigChain.prototype.addUrl = function (req, type, name) {
212 var href = url.format(req)
214 var marker = {__source__:name}
215 this.sources[name] = { href: href, type: type }
217 http.request(req, function (res) {
219 var ct = res.headers['content-type']
221 type = ct.indexOf('json') !== -1 ? 'json'
222 : ct.indexOf('ini') !== -1 ? 'ini'
223 : href.match(/\.json$/) ? 'json'
224 : href.match(/\.ini$/) ? 'ini'
229 res.on('data', c.push.bind(c))
230 .on('end', function () {
231 this.addString(Buffer.concat(c), href, type, marker)
233 .on('error', this.emit.bind(this, 'error'))
236 .on('error', this.emit.bind(this, 'error'))
242 ConfigChain.prototype.addString = function (data, file, type, marker) {
243 data = this.parse(data, file, type)
244 this.add(data, marker)
248 ConfigChain.prototype.add = function (data, marker) {
249 if (marker && typeof marker === 'object') {
250 var i = this.list.indexOf(marker)
252 return this.emit('error', new Error('bad marker'))
254 this.splice(i, 1, data)
255 marker = marker.__source__
256 this.sources[marker] = this.sources[marker] || {}
257 this.sources[marker].data = data
258 // we were waiting for this. maybe emit 'load'
261 if (typeof marker === 'string') {
262 this.sources[marker] = this.sources[marker] || {}
263 this.sources[marker].data = data
265 // trigger the load event if nothing was already going to do so.
268 process.nextTick(this._resolve.bind(this))
273 ConfigChain.prototype.parse = exports.parse
275 ConfigChain.prototype._await = function () {
279 ConfigChain.prototype._resolve = function () {
281 if (this._awaiting === 0) this.emit('load', this)