overleaf/libraries/settings/Settings.js

51 lines
1.7 KiB
JavaScript
Raw Normal View History

let defaults, possibleConfigFiles, settingsExist;
const fs = require("fs");
const path = require("path");
const env = (process.env.NODE_ENV || "development").toLowerCase();
const { merge } = require('./merge');
const defaultSettingsPath = path.normalize(__dirname + "/../../../config/settings.defaults");
if (fs.existsSync(`${defaultSettingsPath}.js`)) {
console.log(`Using default settings from ${defaultSettingsPath}.js`);
defaults = require(`${defaultSettingsPath}.js`);
settingsExist = true;
} else if (fs.existsSync(`${defaultSettingsPath}.coffee`)) {
2021-05-26 10:06:24 -04:00
// TODO: remove this in the next major version
throw new Error(`CoffeeScript settings file ${defaultSettingsPath}.coffee is no longer supported, please convert to JavaScript`);
} else {
defaults = {};
settingsExist = false;
}
2021-05-26 10:06:24 -04:00
if (process.env.SHARELATEX_CONFIG) {
possibleConfigFiles = [process.env.SHARELATEX_CONFIG];
} else {
possibleConfigFiles = [
process.cwd() + `/config/settings.${env}.js`,
path.normalize(__dirname + `/../../../config/settings.${env}.js`),
2021-05-26 10:06:24 -04:00
// TODO: remove these in the next major version
process.cwd() + `/config/settings.${env}.coffee`,
path.normalize(__dirname + `/../../../config/settings.${env}.coffee`)
];
}
2014-02-17 09:51:21 -05:00
2021-05-26 10:06:24 -04:00
for (let file of possibleConfigFiles) {
if (fs.existsSync(file)) {
2021-05-26 10:06:24 -04:00
// TODO: remove this in the next major version
if (file.endsWith('.coffee')) {
2021-05-26 10:06:24 -04:00
throw new Error(`CoffeeScript settings file ${file} is no longer supported, please convert to JavaScript`);
}
console.log("Using settings from " + file);
module.exports = merge(require(file), defaults);
settingsExist = true;
break;
}
}
if (!settingsExist) {
console.warn("No settings or defaults found. I'm flying blind.");
}
module.exports = defaults;