Settings.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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 = process.argv[1]
  7. ? Path.dirname(process.argv[1])
  8. : undefined
  9. const NODE_ENV = (process.env.NODE_ENV || 'development').toLowerCase()
  10. const SHARELATEX_CONFIG = process.env.SHARELATEX_CONFIG
  11. let settings
  12. let settingsExist = false
  13. const defaultsPath =
  14. pathIfExists(Path.join(CWD, 'config/settings.defaults.cjs')) ||
  15. pathIfExists(Path.join(CWD, 'config/settings.defaults.js')) ||
  16. pathIfExists(Path.join(ENTRY_POINT_DIR, 'config/settings.defaults.cjs')) ||
  17. pathIfExists(Path.join(ENTRY_POINT_DIR, 'config/settings.defaults.js'))
  18. if (defaultsPath) {
  19. console.log(`Using default settings from ${defaultsPath}`)
  20. settings = require(defaultsPath)
  21. settingsExist = true
  22. } else {
  23. settings = {}
  24. }
  25. const overridesPath =
  26. pathIfExists(SHARELATEX_CONFIG) ||
  27. pathIfExists(Path.join(CWD, `config/settings.${NODE_ENV}.cjs`)) ||
  28. pathIfExists(Path.join(CWD, `config/settings.${NODE_ENV}.js`))
  29. if (overridesPath) {
  30. console.log(`Using settings from ${overridesPath}`)
  31. settings = merge(require(overridesPath), settings)
  32. settingsExist = true
  33. }
  34. if (!settingsExist) {
  35. console.warn("No settings or defaults found. I'm flying blind.")
  36. }
  37. module.exports = settings
  38. function pathIfExists(path) {
  39. if (path && fs.existsSync(path)) {
  40. return path
  41. }
  42. return null
  43. }