diff --git a/libraries/settings/.gitignore b/libraries/settings/.gitignore new file mode 100644 index 0000000000..aac65cda7a --- /dev/null +++ b/libraries/settings/.gitignore @@ -0,0 +1,2 @@ +.npmrc +Dockerfile diff --git a/libraries/settings/LICENSE b/libraries/settings/LICENSE new file mode 100644 index 0000000000..54a584e24f --- /dev/null +++ b/libraries/settings/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2014-2021 Overleaf + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/libraries/settings/README.md b/libraries/settings/README.md new file mode 100644 index 0000000000..ac5adfc14f --- /dev/null +++ b/libraries/settings/README.md @@ -0,0 +1,19 @@ +@overleaf/settings +=================== + +A small module to allow global config settings to be set for all services +within the Overleaf architecture. + +Settings file location +---------------------- + +You can specify a custom location for the settings file by setting the +`SHARELATEX_CONFIG` environment variable. E.g. + + $ export SHARELATEX_CONFIG=/home/james/config/settings.development.js + +Otherwise, the settings will be loaded from `config/settings.NODE_ENV.js`, +where `NODE_ENV` is another environment variable, or defaults to `development`. + +The config directory is first looked for in the current directory, and then relative +to the settings module directory. diff --git a/libraries/settings/Settings.js b/libraries/settings/Settings.js new file mode 100644 index 0000000000..6672803955 --- /dev/null +++ b/libraries/settings/Settings.js @@ -0,0 +1,50 @@ +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`)) { + // 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; +} + +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`), + // TODO: remove these in the next major version + process.cwd() + `/config/settings.${env}.coffee`, + path.normalize(__dirname + `/../../../config/settings.${env}.coffee`) + ]; +} + +for (let file of possibleConfigFiles) { + if (fs.existsSync(file)) { + // TODO: remove this in the next major version + if (file.endsWith('.coffee')) { + 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; diff --git a/libraries/settings/index.js b/libraries/settings/index.js new file mode 100755 index 0000000000..1a9043cdaf --- /dev/null +++ b/libraries/settings/index.js @@ -0,0 +1 @@ +module.exports = require('./Settings'); diff --git a/libraries/settings/merge.js b/libraries/settings/merge.js new file mode 100644 index 0000000000..c1b24d2bae --- /dev/null +++ b/libraries/settings/merge.js @@ -0,0 +1,12 @@ +function merge(settings, defaults) { + for (const [key, value] of Object.entries(settings)) { + if ((typeof(value) === "object") && !(value instanceof Array)) { + defaults[key] = merge(value, defaults[key] || {}); + } else { + defaults[key] = value; + } + } + return defaults; +} + +module.exports = { merge }; diff --git a/libraries/settings/package-lock.json b/libraries/settings/package-lock.json new file mode 100644 index 0000000000..de48101b3a --- /dev/null +++ b/libraries/settings/package-lock.json @@ -0,0 +1,5 @@ +{ + "name": "@overleaf/settings", + "version": "2.1.1", + "lockfileVersion": 1 +} diff --git a/libraries/settings/package.json b/libraries/settings/package.json new file mode 100644 index 0000000000..27fae668c3 --- /dev/null +++ b/libraries/settings/package.json @@ -0,0 +1,6 @@ +{ + "name": "@overleaf/settings", + "description": "A centralised settings system for Overleaf", + "version": "2.1.1", + "repository": "overleaf/settings-module" +}