Skip to content

Instantly share code, notes, and snippets.

@gregberge
Last active August 29, 2015 14:08
Show Gist options
  • Select an option

  • Save gregberge/e991419fa0e7cd8553ad to your computer and use it in GitHub Desktop.

Select an option

Save gregberge/e991419fa0e7cd8553ad to your computer and use it in GitHub Desktop.
Simple YAML config loader for node.js.
var minimist = require('minimist');
var path = require('path');
var yaml = require('js-yaml');
var fs = require('fs');
var defaultsDeep = require('merge-defaults');
/**
* Define config path.
*/
var configPath = path.join(__dirname, '..', 'config');
/**
* Create and expose configuration.
*/
var defaultsConfig = readConfig('default');
var envConfig = readConfig(process.env.APP_ENV || process.env.NODE_ENV || 'development');
var args = minimist(process.argv.slice(2));
module.exports = defaultsDeep({}, defaultsConfig, envConfig, args);
/**
* Read config file.
*
* @param {string} name
* @returns {object}
*/
function readConfig(name) {
var filepath = path.join(configPath, name + '.yml');
if (! fs.existsSync(filepath)) return {};
return yaml.safeLoad(fs.readFileSync(filepath, 'utf-8'));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment