Skip to content

Instantly share code, notes, and snippets.

@grncdr
Created May 30, 2012 18:56
Show Gist options
  • Save grncdr/2838283 to your computer and use it in GitHub Desktop.
Save grncdr/2838283 to your computer and use it in GitHub Desktop.
simple re-loadable configuration using JS modules
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