Last active
August 29, 2015 14:08
-
-
Save gregberge/e991419fa0e7cd8553ad to your computer and use it in GitHub Desktop.
Simple YAML config loader for node.js.
This file contains hidden or 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
| 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