overleaf/libraries/settings/Settings.js
Antoine Clausse 7f48c67512 Add prefer-node-protocol ESLint rule (#21532)
* Add `unicorn/prefer-node-protocol`

* Fix `unicorn/prefer-node-protocol` ESLint errors

* Run `npm run format:fix`

* Add sandboxed-module sourceTransformers in mocha setups

Fix `no such file or directory, open 'node:fs'` in `sandboxed-module`

* Remove `node:` in the SandboxedModule requires

* Fix new linting errors with `node:`

GitOrigin-RevId: 68f6e31e2191fcff4cb8058dd0a6914c14f59926
2024-11-11 09:04:51 +00:00

55 lines
1.6 KiB
JavaScript

/* eslint-disable no-console */
const fs = require('node:fs')
const Path = require('node:path')
const { merge } = require('./merge')
const CWD = process.cwd()
const ENTRY_POINT_DIR = process.argv[1]
? Path.dirname(process.argv[1])
: undefined
const NODE_ENV = (process.env.NODE_ENV || 'development').toLowerCase()
const SHARELATEX_CONFIG = process.env.SHARELATEX_CONFIG
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'
)
}
let settings
let settingsExist = false
const defaultsPath =
pathIfExists(Path.join(CWD, 'config/settings.defaults.cjs')) ||
pathIfExists(Path.join(CWD, 'config/settings.defaults.js')) ||
pathIfExists(Path.join(ENTRY_POINT_DIR, 'config/settings.defaults.cjs')) ||
pathIfExists(Path.join(ENTRY_POINT_DIR, 'config/settings.defaults.js'))
if (defaultsPath) {
console.log(`Using default settings from ${defaultsPath}`)
settings = require(defaultsPath)
settingsExist = true
} else {
settings = {}
}
const overridesPath =
pathIfExists(OVERLEAF_CONFIG) ||
pathIfExists(Path.join(CWD, `config/settings.${NODE_ENV}.cjs`)) ||
pathIfExists(Path.join(CWD, `config/settings.${NODE_ENV}.js`))
if (overridesPath) {
console.log(`Using settings from ${overridesPath}`)
settings = merge(require(overridesPath), settings)
settingsExist = true
}
if (!settingsExist) {
console.warn("No settings or defaults found. I'm flying blind.")
}
module.exports = settings
function pathIfExists(path) {
if (path && fs.existsSync(path)) {
return path
}
return null
}