Created
May 30, 2012 18:56
-
-
Save grncdr/2838283 to your computer and use it in GitHub Desktop.
simple re-loadable configuration using JS modules
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
inspect = require('util').inspect | |
module.exports = function (config_path, opts) { | |
var settings = { | |
toJSON: function() { return this.__proto__ }, | |
inspect: function() { return inspect(this.__proto__) }, | |
} | |
opts || (opts = {}) | |
if (!opts.error) { | |
opts.error = function(err){ | |
console.error("Error reloading %s: %s", config_path, err) | |
} | |
} | |
function reload() { | |
delete require.cache[require.resolve(config_path)] | |
try { settings.__proto__ = require(config_path) } | |
catch (err) { opts.error(err) } | |
} | |
if (opts.signal) { | |
process.on(opts.signal, reload) | |
} | |
reload() | |
return settings | |
}; // sad wink |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment