This commit is contained in:
chrosey
2017-09-13 07:52:34 +02:00
parent a1f16c37f4
commit 2340b0226b
24621 changed files with 2912161 additions and 149 deletions
+10
View File
@@ -0,0 +1,10 @@
var cc = require('..')
var assert = require('assert')
//throw on invalid json
assert.throws(function () {
cc(__dirname + '/broken.json')
})
+21
View File
@@ -0,0 +1,21 @@
{
"name": "config-chain",
"version": "0.3.0",
"description": "HANDLE CONFIGURATION ONCE AND FOR ALL",
"homepage": "http://github.com/dominictarr/config-chain",
"repository": {
"type": "git",
"url": "https://github.com/dominictarr/config-chain.git"
}
//missing , and then this comment. this json is intensionally invalid
"dependencies": {
"proto-list": "1",
"ini": "~1.0.2"
},
"bundleDependencies": ["ini"],
"REM": "REMEMBER TO REMOVE BUNDLING WHEN/IF ISAACS MERGES ini#7",
"author": "Dominic Tarr <dominic.tarr@gmail.com> (http://dominictarr.com)",
"scripts": {
"test": "node test/find-file.js && node test/ini.js && node test/env.js"
}
}
+100
View File
@@ -0,0 +1,100 @@
var test = require('tap').test
var CC = require('../index.js').ConfigChain
var env = { foo_blaz : 'blzaa', foo_env : 'myenv' }
var jsonObj = { blaz: 'json', json: true }
var iniObj = { 'x.y.z': 'xyz', blaz: 'ini' }
var fs = require('fs')
var ini = require('ini')
fs.writeFileSync('/tmp/config-chain-class.json', JSON.stringify(jsonObj))
fs.writeFileSync('/tmp/config-chain-class.ini', ini.stringify(iniObj))
var http = require('http')
var reqs = 0
http.createServer(function (q, s) {
if (++reqs === 2) this.close()
if (q.url === '/json') {
// make sure that the requests come back from the server
// out of order. they should still be ordered properly
// in the resulting config object set.
setTimeout(function () {
s.setHeader('content-type', 'application/json')
s.end(JSON.stringify({
blaz: 'http',
http: true,
json: true
}))
}, 200)
} else {
s.setHeader('content-type', 'application/ini')
s.end(ini.stringify({
blaz: 'http',
http: true,
ini: true,
json: false
}))
}
}).listen(1337)
test('basic class test', function (t) {
var cc = new CC()
var expectlist =
[ { blaz: 'json', json: true },
{ 'x.y.z': 'xyz', blaz: 'ini' },
{ blaz: 'blzaa', env: 'myenv' },
{ blaz: 'http', http: true, json: true },
{ blaz: 'http', http: true, ini: true, json: false } ]
cc.addFile('/tmp/config-chain-class.json')
.addFile('/tmp/config-chain-class.ini')
.addEnv('foo_', env)
.addUrl('http://localhost:1337/json')
.addUrl('http://localhost:1337/ini')
.on('load', function () {
t.same(cc.list, expectlist)
t.same(cc.snapshot, { blaz: 'json',
json: true,
'x.y.z': 'xyz',
env: 'myenv',
http: true,
ini: true })
cc.del('blaz', '/tmp/config-chain-class.json')
t.same(cc.snapshot, { blaz: 'ini',
json: true,
'x.y.z': 'xyz',
env: 'myenv',
http: true,
ini: true })
cc.del('blaz')
t.same(cc.snapshot, { json: true,
'x.y.z': 'xyz',
env: 'myenv',
http: true,
ini: true })
cc.shift()
t.same(cc.snapshot, { 'x.y.z': 'xyz',
env: 'myenv',
http: true,
json: true,
ini: true })
cc.shift()
t.same(cc.snapshot, { env: 'myenv',
http: true,
json: true,
ini: true })
cc.shift()
t.same(cc.snapshot, { http: true,
json: true,
ini: true })
cc.shift()
t.same(cc.snapshot, { http: true,
ini: true,
json: false })
cc.shift()
t.same(cc.snapshot, {})
t.end()
})
})
+10
View File
@@ -0,0 +1,10 @@
var cc = require('..')
var assert = require('assert')
assert.deepEqual({
hello: true
}, cc.env('test_', {
'test_hello': true,
'ignore_this': 4,
'ignore_test_this_too': []
}))
+13
View File
@@ -0,0 +1,13 @@
var fs = require('fs')
, assert = require('assert')
, objx = {
rand: Math.random()
}
fs.writeFileSync('/tmp/random-test-config.json', JSON.stringify(objx))
var cc = require('../')
var path = cc.find('tmp/random-test-config.json')
assert.equal(path, '/tmp/random-test-config.json')
+15
View File
@@ -0,0 +1,15 @@
var cc = require("../");
var chain = cc()
, name = "forFun";
chain
.add({
__sample:"for fun only"
}, name)
.on("load", function() {
//It throw exception here
console.log(chain.get("__sample", name));
//But if I drop the name param, it run normally and return as expected: "for fun only"
//console.log(chain.get("__sample"));
});
+5
View File
@@ -0,0 +1,5 @@
var cc = require('..')
//should not throw
cc(__dirname, 'non_existing_file')
+18
View File
@@ -0,0 +1,18 @@
var cc =require('..')
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})
+59
View File
@@ -0,0 +1,59 @@
var CC = require('../index.js').ConfigChain
var test = require('tap').test
var f1 = '/tmp/f1.ini'
var f2 = '/tmp/f2.json'
var ini = require('ini')
var f1data = {foo: {bar: 'baz'}, bloo: 'jaus'}
var f2data = {oof: {rab: 'zab'}, oolb: 'suaj'}
var fs = require('fs')
fs.writeFileSync(f1, ini.stringify(f1data), 'utf8')
fs.writeFileSync(f2, JSON.stringify(f2data), 'utf8')
test('test saving and loading ini files', function (t) {
new CC()
.add({grelb:'blerg'}, 'opt')
.addFile(f1, 'ini', 'inifile')
.addFile(f2, 'json', 'jsonfile')
.on('load', function (cc) {
t.same(cc.snapshot, { grelb: 'blerg',
bloo: 'jaus',
foo: { bar: 'baz' },
oof: { rab: 'zab' },
oolb: 'suaj' })
t.same(cc.list, [ { grelb: 'blerg' },
{ bloo: 'jaus', foo: { bar: 'baz' } },
{ oof: { rab: 'zab' }, oolb: 'suaj' } ])
cc.set('grelb', 'brelg', 'opt')
.set('foo', 'zoo', 'inifile')
.set('oof', 'ooz', 'jsonfile')
.save('inifile')
.save('jsonfile')
.on('save', function () {
t.equal(fs.readFileSync(f1, 'utf8'),
"bloo=jaus\nfoo=zoo\n")
t.equal(fs.readFileSync(f2, 'utf8'),
"{\"oof\":\"ooz\",\"oolb\":\"suaj\"}")
t.same(cc.snapshot, { grelb: 'brelg',
bloo: 'jaus',
foo: 'zoo',
oof: 'ooz',
oolb: 'suaj' })
t.same(cc.list, [ { grelb: 'brelg' },
{ bloo: 'jaus', foo: 'zoo' },
{ oof: 'ooz', oolb: 'suaj' } ])
t.pass('ok')
t.end()
})
})
})