-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7b55d4a
commit 5806354
Showing
5 changed files
with
65 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters