Merge pull request #6132 from overleaf/jpa-settings-linting

[settings] setup linting and fix all the violations

GitOrigin-RevId: 321ecf0e8749a0481d61ad720baffe06089dc5cf
This commit is contained in:
Jakob Ackermann 2021-12-16 09:19:28 +00:00 committed by Copybot
parent 4b308553be
commit 4de9c98d2f
4 changed files with 52 additions and 44 deletions

View file

@ -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

View file

@ -1 +1 @@
module.exports = require('./Settings');
module.exports = require('./Settings')

View file

@ -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 }

View file

@ -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"
}