Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const path = require('path');
- const nconf = require('nconf');
- const getConfigDir = () => {
- if (process.env.CONFIG_DIR) {
- return process.env.CONFIG_DIR;
- }
- return path.join(process.cwd(), 'config');
- };
- const loadConfigFiles = () => {
- const configDir = getConfigDir();
- console.log("configDir = ", configDir);
- // Load from default config file
- nconf.file({
- file: path.join(configDir, 'config.default.json')
- });
- if (process.env.NODE_ENV) {
- // Load/override defaults with environment specific config files (preprod / prod etc)
- nconf.file({
- file: path.join(configDir, `config.${process.env.NODE_ENV}.json`)
- });
- if (process.env.NODE_SUB_ENV) {
- // Load/override with sub environment specific config files - (dev / qa / stage etc)
- nconf.file({
- file: path.join(configDir, `config.${process.env.NODE_ENV}.${process.env.NODE_SUB_ENV}.json`)
- });
- }
- }
- }
- const init = () => {
- // Preffered config hierarchy
- // 1st priority for command line arguments
- nconf.argv();
- // 2nd priority for environment variables
- nconf.env();
- // 3rd priority for config files
- loadConfigFiles();
- /*
- * last priotiy hardcoded default values
- * Note: this should be at the bottom of your configuration file
- */
- // nconf.defaults({
- // "someKey": "someValue"
- // });
- };
- module.exports = {
- init
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement