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