Permalink
Browse files

pull in utils from config-chain

  • Loading branch information...
1 parent 7b55d4a commit 5806354c06e5f53bb2aba6bef1d143daf56f2650 @dominictarr committed Apr 8, 2013
Showing with 65 additions and 9 deletions.
  1. +1 −1 index.js
  2. +40 −0 lib/utils.js
  3. +7 −7 package.json
  4. +16 −0 test/ini.js
  5. +1 −1 { → test}/test.js
View
@@ -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'
View
@@ -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
+}
View
@@ -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",
@@ -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"
}
}
View
@@ -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})
+
View
@@ -4,7 +4,7 @@ var assert = require('assert')
process.env[n+'_envOption'] = 42
-var config = require('./')(n, {
+var config = require('../')(n, {
option: true
})

0 comments on commit 5806354

Please sign in to comment.