From 4de9c98d2fc6e2a00bb9f66bb7af66d16195c59a Mon Sep 17 00:00:00 2001 From: Jakob Ackermann Date: Thu, 16 Dec 2021 09:19:28 +0000 Subject: [PATCH] Merge pull request #6132 from overleaf/jpa-settings-linting [settings] setup linting and fix all the violations GitOrigin-RevId: 321ecf0e8749a0481d61ad720baffe06089dc5cf --- libraries/settings/Settings.js | 76 ++++++++++++++++++--------------- libraries/settings/index.js | 2 +- libraries/settings/merge.js | 10 ++--- libraries/settings/package.json | 8 ++-- 4 files changed, 52 insertions(+), 44 deletions(-) diff --git a/libraries/settings/Settings.js b/libraries/settings/Settings.js index 6672803955..b806b775be 100644 --- a/libraries/settings/Settings.js +++ b/libraries/settings/Settings.js @@ -1,50 +1,58 @@ -let defaults, possibleConfigFiles, settingsExist; -const fs = require("fs"); -const path = require("path"); -const env = (process.env.NODE_ENV || "development").toLowerCase(); -const { merge } = require('./merge'); +/* eslint-disable no-console */ +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"); +const defaultSettingsPath = path.join( + __dirname, + '../../../config/settings.defaults' +) if (fs.existsSync(`${defaultSettingsPath}.js`)) { - console.log(`Using default settings from ${defaultSettingsPath}.js`); - defaults = require(`${defaultSettingsPath}.js`); - settingsExist = true; + 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`); + // 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; + defaults = {} + settingsExist = false } if (process.env.SHARELATEX_CONFIG) { - possibleConfigFiles = [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`) - ]; + possibleConfigFiles = [ + path.join(process.cwd(), `config/settings.${env}.js`), + path.join(__dirname, `../../../config/settings.${env}.js`), + // TODO: remove these in the next major version + path.join(process.cwd(), `config/settings.${env}.coffee`), + path.join(__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; - } +for (const 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."); + console.warn("No settings or defaults found. I'm flying blind.") } -module.exports = defaults; +module.exports = defaults diff --git a/libraries/settings/index.js b/libraries/settings/index.js index 1a9043cdaf..a2cc75a9f0 100755 --- a/libraries/settings/index.js +++ b/libraries/settings/index.js @@ -1 +1 @@ -module.exports = require('./Settings'); +module.exports = require('./Settings') diff --git a/libraries/settings/merge.js b/libraries/settings/merge.js index c1b24d2bae..2e11a1656e 100644 --- a/libraries/settings/merge.js +++ b/libraries/settings/merge.js @@ -1,12 +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] || {}); + if (typeof value === 'object' && !(value instanceof Array)) { + defaults[key] = merge(value, defaults[key] || {}) } else { - defaults[key] = value; + defaults[key] = value } } - return defaults; + return defaults } -module.exports = { merge }; +module.exports = { merge } diff --git a/libraries/settings/package.json b/libraries/settings/package.json index d7b0fa12bf..4c7a71bda0 100644 --- a/libraries/settings/package.json +++ b/libraries/settings/package.json @@ -4,10 +4,10 @@ "version": "2.1.1", "repository": "overleaf/settings-module", "scripts": { - "lint": "echo noop", - "lint:fix": "echo noop", - "format:fix": "echo noop", - "format": "echo noop", + "lint": "../../node_modules/.bin/eslint --max-warnings 0 --format unix .", + "lint:fix": "../../node_modules/.bin/eslint --fix .", + "format": "../../node_modules/.bin/prettier --list-different $PWD/'**/*.js'", + "format:fix": "../../node_modules/.bin/prettier --write $PWD/'**/*.js'", "test": "echo noop", "test:ci": "echo noop" }