1.1.0 • Published 9 years ago
configuratoror v1.1.0
configuratoror
Simple module to load inheritable configs from folder
From the folder path you specify configuratoror loads all config files in memory, parses them and returns a config loader that in turn returns a config for your env.
install
npm i configuratororusage
folder structure
etc/
common.js
production.js
development.js
whatever.jsconfig files
common.js:
module.exports = {
database: {
uri: 'http://localhost:1338',
username: 'admin'
}
}production.js:
module.exports = {
_extends: 'common',
database: {
name: 'production',
password: 'michaeljordan',
}
}load config
var options = {
folder: './etc', // path from pwd to folder where configs are stored
};
var env = 'production'; // it's up to you how to determine what environment you're running in, e.g. process.env.NODE_ENV
var config = require('configuratoror')(options)(env);
console.log(config);output
{
database: {
uri: 'http://localhost:1338',
username: 'admin'
name: 'production',
password: 'michaeljordan',
},
}details
- the config files must end in
.jsand are loaded using NodeJSrequire, so they need to usemodule.exportsto export the configuration object. - to determine which config files are present in
options.folder,fs.readdirSyncis used. This removes the need for callbacks, but introduces asyncstep. If you absolutely want the shortest boot time, don't use this module (although I doubt you'll notice the difference). - a config can extend another config by specifying
_extends: '<name of other config>'. Properties in the extending config overwrite properties in the extended config. There's no maximum to the amount of inheritance levels. - configs are deeply merged using lodash.merge.
- this module throws errors because your process should die when it fails to load its configuration. If you disagree (for your specific use case), don't use this module or
try-catchyour way out of it.