lint: src/node/utils/Settings.js

pull/4667/head
John McLear 2021-01-21 21:06:52 +00:00 committed by Richard Hansen
parent af8ea6b45f
commit 31f1e39565
1 changed files with 24 additions and 25 deletions

View File

@ -1,3 +1,4 @@
'use strict';
/** /**
* The Settings module reads the settings out of settings.json and provides * The Settings module reads the settings out of settings.json and provides
* this information to the other modules * this information to the other modules
@ -31,7 +32,6 @@ const fs = require('fs');
const os = require('os'); const os = require('os');
const path = require('path'); const path = require('path');
const argv = require('./Cli').argv; const argv = require('./Cli').argv;
const npm = require('npm/lib/npm.js');
const jsonminify = require('jsonminify'); const jsonminify = require('jsonminify');
const log4js = require('log4js'); const log4js = require('log4js');
const randomString = require('./randomstring'); const randomString = require('./randomstring');
@ -381,29 +381,30 @@ exports.commitRateLimiting = {
exports.importMaxFileSize = 50 * 1024 * 1024; exports.importMaxFileSize = 50 * 1024 * 1024;
// checks if abiword is avaiable // checks if abiword is avaiable
exports.abiwordAvailable = function () { exports.abiwordAvailable = () => {
if (exports.abiword != null) { if (exports.abiword != null) {
return os.type().indexOf('Windows') != -1 ? 'withoutPDF' : 'yes'; return os.type().indexOf('Windows') !== -1 ? 'withoutPDF' : 'yes';
} else { } else {
return 'no'; return 'no';
} }
}; };
exports.sofficeAvailable = function () { exports.sofficeAvailable = () => {
if (exports.soffice != null) { if (exports.soffice != null) {
return os.type().indexOf('Windows') != -1 ? 'withoutPDF' : 'yes'; return os.type().indexOf('Windows') !== -1 ? 'withoutPDF' : 'yes';
} else { } else {
return 'no'; return 'no';
} }
}; };
exports.exportAvailable = function () { exports.exportAvailable = () => {
const abiword = exports.abiwordAvailable(); const abiword = exports.abiwordAvailable();
const soffice = exports.sofficeAvailable(); const soffice = exports.sofficeAvailable();
if (abiword == 'no' && soffice == 'no') { if (abiword === 'no' && soffice === 'no') {
return 'no'; return 'no';
} else if ((abiword == 'withoutPDF' && soffice == 'no') || (abiword == 'no' && soffice == 'withoutPDF')) { } else if ((abiword === 'withoutPDF' && soffice === 'no') ||
(abiword === 'no' && soffice === 'withoutPDF')) {
return 'withoutPDF'; return 'withoutPDF';
} else { } else {
return 'yes'; return 'yes';
@ -411,7 +412,7 @@ exports.exportAvailable = function () {
}; };
// Provide git version if available // Provide git version if available
exports.getGitCommit = function () { exports.getGitCommit = () => {
let version = ''; let version = '';
try { try {
let rootPath = exports.root; let rootPath = exports.root;
@ -436,9 +437,7 @@ exports.getGitCommit = function () {
}; };
// Return etherpad version from package.json // Return etherpad version from package.json
exports.getEpVersion = function () { exports.getEpVersion = () => require('../../package.json').version;
return require('ep_etherpad-lite/package.json').version;
};
/** /**
* Receives a settingsObj and, if the property name is a valid configuration * Receives a settingsObj and, if the property name is a valid configuration
@ -447,7 +446,7 @@ exports.getEpVersion = function () {
* This code refactors a previous version that copied & pasted the same code for * This code refactors a previous version that copied & pasted the same code for
* both "settings.json" and "credentials.json". * both "settings.json" and "credentials.json".
*/ */
function storeSettings(settingsObj) { const storeSettings = (settingsObj) => {
for (const i in settingsObj) { for (const i in settingsObj) {
// test if the setting starts with a lowercase character // test if the setting starts with a lowercase character
if (i.charAt(0).search('[a-z]') !== 0) { if (i.charAt(0).search('[a-z]') !== 0) {
@ -456,7 +455,7 @@ function storeSettings(settingsObj) {
// we know this setting, so we overwrite it // we know this setting, so we overwrite it
// or it's a settings hash, specific to a plugin // or it's a settings hash, specific to a plugin
if (exports[i] !== undefined || i.indexOf('ep_') == 0) { if (exports[i] !== undefined || i.indexOf('ep_') === 0) {
if (_.isObject(settingsObj[i]) && !_.isArray(settingsObj[i])) { if (_.isObject(settingsObj[i]) && !_.isArray(settingsObj[i])) {
exports[i] = _.defaults(settingsObj[i], exports[i]); exports[i] = _.defaults(settingsObj[i], exports[i]);
} else { } else {
@ -467,7 +466,7 @@ function storeSettings(settingsObj) {
console.warn(`Unknown Setting: '${i}'. This setting doesn't exist or it was removed`); console.warn(`Unknown Setting: '${i}'. This setting doesn't exist or it was removed`);
} }
} }
} };
/* /*
* If stringValue is a numeric string, or its value is "true" or "false", coerce * If stringValue is a numeric string, or its value is "true" or "false", coerce
@ -481,7 +480,7 @@ function storeSettings(settingsObj) {
* short syntax "${ABIWORD}", and not "${ABIWORD:null}": the latter would result * short syntax "${ABIWORD}", and not "${ABIWORD:null}": the latter would result
* in the literal string "null", instead. * in the literal string "null", instead.
*/ */
function coerceValue(stringValue) { const coerceValue = (stringValue) => {
// cooked from https://stackoverflow.com/questions/175739/built-in-way-in-javascript-to-check-if-a-string-is-a-valid-number // cooked from https://stackoverflow.com/questions/175739/built-in-way-in-javascript-to-check-if-a-string-is-a-valid-number
const isNumeric = !isNaN(stringValue) && !isNaN(parseFloat(stringValue) && isFinite(stringValue)); const isNumeric = !isNaN(stringValue) && !isNaN(parseFloat(stringValue) && isFinite(stringValue));
@ -502,7 +501,7 @@ function coerceValue(stringValue) {
// otherwise, return this value as-is // otherwise, return this value as-is
return stringValue; return stringValue;
} };
/** /**
* Takes a javascript object containing Etherpad's configuration, and returns * Takes a javascript object containing Etherpad's configuration, and returns
@ -540,7 +539,7 @@ function coerceValue(stringValue) {
* *
* see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#The_replacer_parameter * see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#The_replacer_parameter
*/ */
function lookupEnvironmentVariables(obj) { const lookupEnvironmentVariables = (obj) => {
const stringifiedAndReplaced = JSON.stringify(obj, (key, value) => { const stringifiedAndReplaced = JSON.stringify(obj, (key, value) => {
/* /*
* the first invocation of replacer() is with an empty key. Just go on, or * the first invocation of replacer() is with an empty key. Just go on, or
@ -569,7 +568,7 @@ function lookupEnvironmentVariables(obj) {
// MUXATOR 2019-03-21: we could use named capture groups here once we migrate to nodejs v10 // MUXATOR 2019-03-21: we could use named capture groups here once we migrate to nodejs v10
const match = value.match(/^\$\{([^:]*)(:((.|\n)*))?\}$/); const match = value.match(/^\$\{([^:]*)(:((.|\n)*))?\}$/);
if (match === null) { if (match == null) {
// no match: use the value literally, without any substitution // no match: use the value literally, without any substitution
return value; return value;
@ -613,7 +612,7 @@ function lookupEnvironmentVariables(obj) {
const newSettings = JSON.parse(stringifiedAndReplaced); const newSettings = JSON.parse(stringifiedAndReplaced);
return newSettings; return newSettings;
} };
/** /**
* - reads the JSON configuration file settingsFilename from disk * - reads the JSON configuration file settingsFilename from disk
@ -623,7 +622,7 @@ function lookupEnvironmentVariables(obj) {
* *
* The isSettings variable only controls the error logging. * The isSettings variable only controls the error logging.
*/ */
function parseSettings(settingsFilename, isSettings) { const parseSettings = (settingsFilename, isSettings) => {
let settingsStr = ''; let settingsStr = '';
let settingsType, notFoundMessage, notFoundFunction; let settingsType, notFoundMessage, notFoundFunction;
@ -663,9 +662,9 @@ function parseSettings(settingsFilename, isSettings) {
process.exit(1); process.exit(1);
} }
} };
exports.reloadSettings = function reloadSettings() { exports.reloadSettings = () => {
// Discover where the settings file lives // Discover where the settings file lives
const settingsFilename = absolutePaths.makeAbsolute(argv.settings || 'settings.json'); const settingsFilename = absolutePaths.makeAbsolute(argv.settings || 'settings.json');
@ -695,7 +694,7 @@ exports.reloadSettings = function reloadSettings() {
const skinBasePath = path.join(exports.root, 'src', 'static', 'skins'); const skinBasePath = path.join(exports.root, 'src', 'static', 'skins');
const countPieces = exports.skinName.split(path.sep).length; const countPieces = exports.skinName.split(path.sep).length;
if (countPieces != 1) { if (countPieces !== 1) {
console.error(`skinName must be the name of a directory under "${skinBasePath}". This is not valid: "${exports.skinName}". Falling back to the default "colibris".`); console.error(`skinName must be the name of a directory under "${skinBasePath}". This is not valid: "${exports.skinName}". Falling back to the default "colibris".`);
exports.skinName = 'colibris'; exports.skinName = 'colibris';
@ -766,7 +765,7 @@ exports.reloadSettings = function reloadSettings() {
} }
if (exports.dbType === 'dirty') { if (exports.dbType === 'dirty') {
const dirtyWarning = 'DirtyDB is used. This is fine for testing but not recommended for production.'; const dirtyWarning = 'DirtyDB is used. This is not recommended for production.';
if (!exports.suppressErrorsInPadText) { if (!exports.suppressErrorsInPadText) {
exports.defaultPadText = `${exports.defaultPadText}\nWarning: ${dirtyWarning}${suppressDisableMsg}`; exports.defaultPadText = `${exports.defaultPadText}\nWarning: ${dirtyWarning}${suppressDisableMsg}`;
} }