Skip to content

Commit

Permalink
pull in utils from config-chain
Browse files Browse the repository at this point in the history
  • Loading branch information
dominictarr committed Apr 8, 2013
1 parent 7b55d4a commit 5806354
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 9 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#! /usr/bin/env node
var argv = require('optimist').argv
var cc = require('config-chain')
var cc = require('./lib/utils')
var join = require('path').join
var deepExtend = require('deep-extend')
var etc = '/etc'
Expand Down
40 changes: 40 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
var fs = require('fs')
var ini = require('ini')
var path = require('path')

var parse = exports.parse = function (content, file) {

//if it ends in .json or starts with { then it must be json.
//must be done this way, because ini accepts everything.
//can't just try and parse it and let it throw if it's not ini.
//everything is ini. even json with a systax error.

if((file && /\.json$/.test(file)) || /^\s*{/.test(content))
return JSON.parse(content)
return ini.parse(content)

}

var json = exports.json = function () {
var args = [].slice.call(arguments).filter(function (arg) { return arg != null })
var file = path.join.apply(null, args)
var content
try {
content = fs.readFileSync(file,'utf-8')
} catch (err) {
return
}
return parse(content)
}

var env = exports.env = function (prefix, env) {
env = env || process.env
var obj = {}
var l = prefix.length
for(var k in env) {
if((i =k.indexOf(prefix)) === 0)
obj[k.substring(l)] = env[k]
}

return obj
}
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
"main": "index.js",
"browserify": "browser.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "set -e; node test/test.js; node test/ini.js"
},
"repository": {
"type": "git",
"url": "/~https://github.com/dominictarr/rc.git"
},
"repository": {"type": "git", "url": "/~https://github.com/dominictarr/rc.git"},
"keywords": [
"config",
"rc",
Expand All @@ -18,11 +21,8 @@
"author": "Dominic Tarr <dominic.tarr@gmail.com> (dominictarr.com)",
"license": "BSD",
"dependencies": {
"config-chain": "~0.3",
"optimist": "~0.3.4",
"deep-extend": "~0.2.5"
},
"scripts": {
"test": "node test.js"
"deep-extend": "~0.2.5",
"ini": "~1.1.0"
}
}
16 changes: 16 additions & 0 deletions test/ini.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
var cc =require('../lib/utils')
var INI = require('ini')
var assert = require('assert')

function test(obj) {

var _json, _ini
var json = cc.parse (_json = JSON.stringify(obj))
var ini = cc.parse (_ini = INI.stringify(obj))
console.log(_ini, _json)
assert.deepEqual(json, ini)
}


test({hello: true})

2 changes: 1 addition & 1 deletion test.js → test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var assert = require('assert')

process.env[n+'_envOption'] = 42

var config = require('./')(n, {
var config = require('../')(n, {
option: true
})

Expand Down

0 comments on commit 5806354

Please sign in to comment.