config-chain
Advanced tools
Comparing version 1.0.1 to 1.1.0
118
index.js
@@ -90,2 +90,4 @@ | ||
this._awaiting = 0 | ||
this._saving = 0 | ||
this.sources = {} | ||
} | ||
@@ -102,4 +104,93 @@ | ||
ConfigChain.prototype.addFile = function (file, type) { | ||
var marker = {file:file} | ||
ConfigChain.prototype.del = function (key, where) { | ||
// if not specified where, then delete from the whole chain, scorched | ||
// earth style | ||
if (where) { | ||
var target = this.sources[where] | ||
target = target && target.data | ||
if (!target) { | ||
return this.emit('error', new Error('not found '+where)) | ||
} | ||
delete target[key] | ||
} else { | ||
for (var i = 0, l = this.list.length; i < l; i ++) { | ||
delete this.list[i][key] | ||
} | ||
} | ||
return this | ||
} | ||
ConfigChain.prototype.set = function (key, value, where) { | ||
var target | ||
if (where) { | ||
target = this.sources[where] | ||
target = target && target.data | ||
if (!target) { | ||
return this.emit('error', new Error('not found '+where)) | ||
} | ||
} else { | ||
target = this.list[0] | ||
if (!target) { | ||
return this.emit('error', new Error('cannot set, no confs!')) | ||
} | ||
} | ||
target[key] = value | ||
return this | ||
} | ||
ConfigChain.prototype.get = function (key, where) { | ||
if (where) { | ||
where = this.sources[where] | ||
if (where) where = where.data | ||
if (where && where.hasOwnProperty(key)) return where[key] | ||
return undefined | ||
} | ||
return this.list[0][key] | ||
} | ||
ConfigChain.prototype.save = function (where, type, cb) { | ||
if (typeof type === 'function') cb = type, type = null | ||
var target = this.sources[where] | ||
if (!target || !(target.path || target.source) || !target.data) { | ||
// TODO: maybe save() to a url target could be a PUT or something? | ||
// would be easy to swap out with a reddis type thing, too | ||
return this.emit('error', new Error('bad save target: '+where)) | ||
} | ||
if (target.source) { | ||
var pref = target.prefix || '' | ||
Object.keys(target.data).forEach(function (k) { | ||
target.source[pref + k] = target.data[k] | ||
}) | ||
return this | ||
} | ||
var type = type || target.type | ||
var data = target.data | ||
if (target.type === 'json') { | ||
data = JSON.stringify(data) | ||
} else { | ||
data = ini.stringify(data) | ||
} | ||
this._saving ++ | ||
fs.writeFile(target.path, data, 'utf8', function (er) { | ||
this._saving -- | ||
if (er) { | ||
if (cb) return cb(er) | ||
else return this.emit('error', er) | ||
} | ||
if (this._saving === 0) { | ||
if (cb) cb() | ||
this.emit('save') | ||
} | ||
}.bind(this)) | ||
return this | ||
} | ||
ConfigChain.prototype.addFile = function (file, type, name) { | ||
name = name || file | ||
var marker = {__source__:name} | ||
this.sources[name] = { path: file, type: type } | ||
this.push(marker) | ||
@@ -114,13 +205,18 @@ this._await() | ||
ConfigChain.prototype.addEnv = function (prefix, env) { | ||
ConfigChain.prototype.addEnv = function (prefix, env, name) { | ||
this._await() | ||
name = name || 'env' | ||
this.sources[name] = { data: env, source: env, prefix: prefix } | ||
this.push(exports.env(prefix, env)) | ||
process.nextTick(this._resolve.bind(this)) | ||
return this | ||
} | ||
ConfigChain.prototype.addUrl = function (req, type) { | ||
ConfigChain.prototype.addUrl = function (req, type, name) { | ||
this._await() | ||
var marker = {url:req} | ||
var href = url.format(req) | ||
name = name || href | ||
var marker = {__source__:name} | ||
this.sources[name] = { href: href, type: type } | ||
this.push(marker) | ||
var href = url.format(req) | ||
http.request(req, function (res) { | ||
@@ -135,2 +231,3 @@ var c = [] | ||
: null | ||
marker.type = type | ||
} | ||
@@ -158,3 +255,3 @@ | ||
ConfigChain.prototype.add = function (data, marker) { | ||
if (marker) { | ||
if (marker && typeof marker === 'object') { | ||
var i = this.list.indexOf(marker) | ||
@@ -165,5 +262,12 @@ if (i === -1) { | ||
this.splice(i, 1, data) | ||
marker = marker.__source__ | ||
this.sources[marker] = this.sources[marker] || {} | ||
this.sources[marker].data = data | ||
// we were waiting for this. maybe emit 'load' | ||
this._resolve() | ||
} else { | ||
if (typeof marker === 'string') { | ||
this.sources[marker] = this.sources[marker] || {} | ||
this.sources[marker].data = data | ||
} | ||
// trigger the load event if nothing was already going to do so. | ||
@@ -170,0 +274,0 @@ this._await() |
{ | ||
"name": "config-chain", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "HANDLE CONFIGURATION ONCE AND FOR ALL", | ||
@@ -5,0 +5,0 @@ "homepage": "http://github.com/dominictarr/config-chain", |
@@ -63,5 +63,18 @@ var test = require('tap').test | ||
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', | ||
blaz: 'ini', | ||
env: 'myenv', | ||
@@ -72,4 +85,3 @@ http: true, | ||
cc.shift() | ||
t.same(cc.snapshot, { blaz: 'blzaa', | ||
env: 'myenv', | ||
t.same(cc.snapshot, { env: 'myenv', | ||
http: true, | ||
@@ -79,9 +91,7 @@ json: true, | ||
cc.shift() | ||
t.same(cc.snapshot, { blaz: 'http', | ||
http: true, | ||
t.same(cc.snapshot, { http: true, | ||
json: true, | ||
ini: true }) | ||
cc.shift() | ||
t.same(cc.snapshot, { blaz: 'http', | ||
http: true, | ||
t.same(cc.snapshot, { http: true, | ||
ini: true, | ||
@@ -88,0 +98,0 @@ json: false }) |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
20963
13
450
229
5