Settings.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /* eslint-disable no-console */
  2. const fs = require('fs')
  3. const Path = require('path')
  4. const { merge } = require('./merge')
  5. const CWD = process.cwd()
  6. const ENTRY_POINT_DIR = Path.dirname(process.argv[1])
  7. const NODE_ENV = (process.env.NODE_ENV || 'development').toLowerCase()
  8. const SHARELATEX_CONFIG = process.env.SHARELATEX_CONFIG
  9. let settings
  10. let settingsExist = false
  11. const defaultsPath =
  12. pathIfExists(Path.join(CWD, 'config/settings.defaults.js')) ||
  13. pathIfExists(Path.join(ENTRY_POINT_DIR, 'config/settings.defaults.js'))
  14. if (defaultsPath) {
  15. console.log(`Using default settings from ${defaultsPath}`)
  16. settings = require(defaultsPath)
  17. settingsExist = true
  18. } else {
  19. settings = {}
  20. }
  21. const overridesPath =
  22. pathIfExists(SHARELATEX_CONFIG) ||
  23. pathIfExists(Path.join(CWD, `config/settings.${NODE_ENV}.js`))
  24. if (overridesPath) {
  25. console.log(`Using settings from ${overridesPath}`)
  26. settings = merge(require(overridesPath), settings)
  27. settingsExist = true
  28. }
  29. if (!settingsExist) {
  30. console.warn("No settings or defaults found. I'm flying blind.")
  31. }
  32. module.exports = settings
  33. function pathIfExists(path) {
  34. if (path && fs.existsSync(path)) {
  35. return path
  36. }
  37. return null
  38. }