Advertisement
Guest User

Untitled

a guest
May 21st, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. const path = require('path');
  2. const nconf = require('nconf');
  3.  
  4. const getConfigDir = () => {
  5. if (process.env.CONFIG_DIR) {
  6. return process.env.CONFIG_DIR;
  7. }
  8. return path.join(process.cwd(), 'config');
  9. };
  10.  
  11. const loadConfigFiles = () => {
  12. const configDir = getConfigDir();
  13. console.log("configDir = ", configDir);
  14.  
  15. // Load from default config file
  16. nconf.file({
  17. file: path.join(configDir, 'config.default.json')
  18. });
  19.  
  20. if (process.env.NODE_ENV) {
  21. // Load/override defaults with environment specific config files (preprod / prod etc)
  22. nconf.file({
  23. file: path.join(configDir, `config.${process.env.NODE_ENV}.json`)
  24. });
  25. if (process.env.NODE_SUB_ENV) {
  26. // Load/override with sub environment specific config files - (dev / qa / stage etc)
  27. nconf.file({
  28. file: path.join(configDir, `config.${process.env.NODE_ENV}.${process.env.NODE_SUB_ENV}.json`)
  29. });
  30. }
  31. }
  32. }
  33.  
  34. const init = () => {
  35. // Preffered config hierarchy
  36.  
  37. // 1st priority for command line arguments
  38. nconf.argv();
  39.  
  40. // 2nd priority for environment variables
  41. nconf.env();
  42.  
  43. // 3rd priority for config files
  44. loadConfigFiles();
  45.  
  46. /*
  47. * last priotiy hardcoded default values
  48. * Note: this should be at the bottom of your configuration file
  49. */
  50. // nconf.defaults({
  51. // "someKey": "someValue"
  52. // });
  53. };
  54.  
  55. module.exports = {
  56. init
  57. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement