2021-12-16 04:19:28 -05:00
|
|
|
/* eslint-disable no-console */
|
|
|
|
const fs = require('fs')
|
2022-02-07 06:46:51 -05:00
|
|
|
const Path = require('path')
|
2021-12-16 04:19:28 -05:00
|
|
|
const { merge } = require('./merge')
|
2014-04-02 12:25:50 -04:00
|
|
|
|
2022-02-07 06:46:51 -05:00
|
|
|
const CWD = process.cwd()
|
2022-03-24 08:29:11 -04:00
|
|
|
const ENTRY_POINT_DIR = process.argv[1]
|
|
|
|
? Path.dirname(process.argv[1])
|
|
|
|
: undefined
|
2022-02-07 06:46:51 -05:00
|
|
|
const NODE_ENV = (process.env.NODE_ENV || 'development').toLowerCase()
|
|
|
|
const SHARELATEX_CONFIG = process.env.SHARELATEX_CONFIG
|
2014-04-02 12:25:50 -04:00
|
|
|
|
2022-02-07 06:46:51 -05:00
|
|
|
let settings
|
|
|
|
let settingsExist = false
|
2022-03-02 06:27:48 -05:00
|
|
|
const defaultsPath =
|
2022-12-13 07:37:49 -05:00
|
|
|
pathIfExists(Path.join(CWD, 'config/settings.defaults.cjs')) ||
|
2022-03-02 06:27:48 -05:00
|
|
|
pathIfExists(Path.join(CWD, 'config/settings.defaults.js')) ||
|
2022-12-13 07:37:49 -05:00
|
|
|
pathIfExists(Path.join(ENTRY_POINT_DIR, 'config/settings.defaults.cjs')) ||
|
2022-03-02 06:27:48 -05:00
|
|
|
pathIfExists(Path.join(ENTRY_POINT_DIR, 'config/settings.defaults.js'))
|
2022-02-07 06:46:51 -05:00
|
|
|
if (defaultsPath) {
|
|
|
|
console.log(`Using default settings from ${defaultsPath}`)
|
|
|
|
settings = require(defaultsPath)
|
2021-12-16 04:19:28 -05:00
|
|
|
settingsExist = true
|
2021-05-26 09:24:24 -04:00
|
|
|
} else {
|
2022-02-07 06:46:51 -05:00
|
|
|
settings = {}
|
2021-05-26 09:24:24 -04:00
|
|
|
}
|
2014-04-02 12:25:50 -04:00
|
|
|
|
2022-02-07 06:46:51 -05:00
|
|
|
const overridesPath =
|
|
|
|
pathIfExists(SHARELATEX_CONFIG) ||
|
2022-12-13 07:37:49 -05:00
|
|
|
pathIfExists(Path.join(CWD, `config/settings.${NODE_ENV}.cjs`)) ||
|
2022-02-07 06:46:51 -05:00
|
|
|
pathIfExists(Path.join(CWD, `config/settings.${NODE_ENV}.js`))
|
|
|
|
if (overridesPath) {
|
|
|
|
console.log(`Using settings from ${overridesPath}`)
|
|
|
|
settings = merge(require(overridesPath), settings)
|
|
|
|
settingsExist = true
|
2021-05-26 09:24:24 -04:00
|
|
|
}
|
2014-04-02 12:25:50 -04:00
|
|
|
|
2021-05-26 09:24:24 -04:00
|
|
|
if (!settingsExist) {
|
2021-12-16 04:19:28 -05:00
|
|
|
console.warn("No settings or defaults found. I'm flying blind.")
|
2021-05-26 09:24:24 -04:00
|
|
|
}
|
2014-04-02 12:25:50 -04:00
|
|
|
|
2022-02-07 06:46:51 -05:00
|
|
|
module.exports = settings
|
|
|
|
|
|
|
|
function pathIfExists(path) {
|
|
|
|
if (path && fs.existsSync(path)) {
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
return null
|
|
|
|
}
|