3 var ini = require('ini')
4 var path = require('path')
5 var stripJsonComments = require('strip-json-comments')
7 var parse = exports.parse = function (content) {
9 //if it ends in .json or starts with { then it must be json.
10 //must be done this way, because ini accepts everything.
11 //can't just try and parse it and let it throw if it's not ini.
12 //everything is ini. even json with a syntax error.
14 if(/^\s*{/.test(content))
15 return JSON.parse(stripJsonComments(content))
16 return ini.parse(content)
20 var file = exports.file = function () {
21 var args = [].slice.call(arguments).filter(function (arg) { return arg != null })
23 //path.join breaks if it's a not a string, so just skip this.
25 if('string' !== typeof args[i])
28 var file = path.join.apply(null, args)
31 return fs.readFileSync(file,'utf-8')
37 var json = exports.json = function () {
38 var content = file.apply(null, arguments)
39 return content ? parse(content) : null
42 var env = exports.env = function (prefix, env) {
43 env = env || process.env
47 if(k.toLowerCase().indexOf(prefix.toLowerCase()) === 0) {
49 var keypath = k.substring(l).split('__')
51 // Trim empty strings from keypath array
53 while ((_emptyStringIndex=keypath.indexOf('')) > -1) {
54 keypath.splice(_emptyStringIndex, 1)
58 keypath.forEach(function _buildSubObj(_subkey,i){
60 // (check for _subkey first so we ignore empty strings)
61 // (check for cursor to avoid assignment to primitive objects)
62 if (!_subkey || typeof cursor !== 'object')
65 // If this is the last key, just stuff the value in there
66 // Assigns actual value from env variable to final key
67 // (unless it's just an empty string- in that case use the last valid key)
68 if (i === keypath.length-1)
69 cursor[_subkey] = env[k]
72 // Build sub-object if nothing already exists at the keypath
73 if (cursor[_subkey] === undefined)
76 // Increment cursor used to track the object at the current depth
77 cursor = cursor[_subkey]
88 var find = exports.find = function () {
89 var rel = path.join.apply(null, [].slice.call(arguments))
91 function find(start, rel) {
92 var file = path.join(start, rel)
97 if(path.dirname(start) !== start) // root
98 return find(path.dirname(start), rel)
101 return find(process.cwd(), rel)