From a489f871f8539214bcba94b671f28fd54c24d2bd Mon Sep 17 00:00:00 2001 From: Kristjan ESPERANTO <35647502+KristjanESPERANTO@users.noreply.github.com> Date: Wed, 18 Sep 2024 21:32:06 +0200 Subject: [PATCH] Review check_config - remove superfluous colors in Log.error - invert negative if - update ESLint env - use camel case variable name - optimize Log strings --- CHANGELOG.md | 7 ++++--- cspell.config.json | 1 + js/check_config.js | 46 ++++++++++++++++++++++------------------------ 3 files changed, 27 insertions(+), 27 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ec74476f76..22022536f8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,9 +12,9 @@ _This release is scheduled to be released on 2024-10-01._ ### Added - [core] Check config at every start of MagicMirror² (#3450) -- [core] Add spelling check (cspell): `npm run test:spelling` and handle spelling issues -- [core] removed `config.paths.vendor` (could not work because `vendor` is hardcoded in `index.html`), renamed `config.paths.modules` to `config.foreignModulesDir`, added variable `MM_CUSTOMCSS_FILE` which - if set - overrides `config.customCss`, added variable `MM_MODULES_DIR` which - if set - overrides `config.foreignModulesDir` -- [core] elements are now removed from index.html when loading script or stylesheet files fails +- [core] Add spelling check (cspell): `npm run test:spelling` and handle spelling issues (#3544) +- [core] removed `config.paths.vendor` (could not work because `vendor` is hardcoded in `index.html`), renamed `config.paths.modules` to `config.foreignModulesDir`, added variable `MM_CUSTOMCSS_FILE` which - if set - overrides `config.customCss`, added variable `MM_MODULES_DIR` which - if set - overrides `config.foreignModulesDir` (#3530) +- [core] elements are now removed from `index.html` when loading script or stylesheet files fails ### Removed @@ -27,6 +27,7 @@ _This release is scheduled to be released on 2024-10-01._ - [core] Updated dependencies including stylistic-eslint - [core] Updated SocketIO catch all to new API - [core] Allow custom modules positions by scanning index.html for the defined regions, instead of hard coded (PR #3518 fixes issue #3504) +- [core] Detail optimizations in `config_check.js` ### Fixed diff --git a/cspell.config.json b/cspell.config.json index bbb26dfcf1..16addf44a9 100644 --- a/cspell.config.json +++ b/cspell.config.json @@ -36,6 +36,7 @@ "crazyscot", "Creepin", "currentweather", + "CUSTOMCSS", "customregions", "Cymraeg", "dariom", diff --git a/js/check_config.js b/js/check_config.js index 825aa3f325..59c4221251 100644 --- a/js/check_config.js +++ b/js/check_config.js @@ -1,18 +1,16 @@ const path = require("node:path"); const fs = require("node:fs"); +const Ajv = require("ajv"); const colors = require("ansis"); const { Linter } = require("eslint"); -const linter = new Linter(); - -const Ajv = require("ajv"); - -const ajv = new Ajv(); - const rootPath = path.resolve(`${__dirname}/../`); const Log = require(`${rootPath}/js/logger.js`); const Utils = require(`${rootPath}/js/utils.js`); +const linter = new Linter(); +const ajv = new Ajv(); + /** * Returns a string with path of configuration file. * Check if set by environment variable MM_CONFIG_FILE @@ -38,28 +36,28 @@ function checkConfigFile () { // Check permission try { fs.accessSync(configFileName, fs.F_OK); - } catch (e) { - Log.error(e); + } catch (error) { + Log.error(error); throw new Error("No permission to access config file!"); } // Validate syntax of the configuration file. - Log.info("Checking file... ", configFileName); + Log.info(`Checking config file ${configFileName} ...`); // I'm not sure if all ever is utf-8 const configFile = fs.readFileSync(configFileName, "utf-8"); - // Explicitly tell linter that he might encounter es6 syntax ("let config = {...}") + // Explicitly tell linter that he might encounter es2024 syntax ("let config = {...}") const errors = linter.verify(configFile, { env: { - es6: true + es2024: true } }); if (errors.length === 0) { Log.info(colors.green("Your configuration file doesn't contain syntax errors :)")); } else { - Log.error(colors.red("Your configuration file contains syntax errors :(")); + Log.error("Your configuration file contains syntax errors :("); for (const error of errors) { Log.error(`Line ${error.line} column ${error.column}: ${error.message}`); @@ -67,9 +65,9 @@ function checkConfigFile () { process.exit(1); } - Log.info("Checking modules structure configuration... "); + Log.info("Checking modules structure configuration ..."); - const position_list = Utils.getModulePositions(); + const positionList = Utils.getModulePositions(); // Make Ajv schema configuration of modules config // only scan "module" and "position" @@ -86,7 +84,7 @@ function checkConfigFile () { }, position: { type: "string", - enum: position_list + enum: positionList } }, required: ["module"] @@ -95,25 +93,25 @@ function checkConfigFile () { } }; - // scan all modules + // Scan all modules const validate = ajv.compile(schema); const data = require(configFileName); const valid = validate(data); - if (!valid) { - let module = validate.errors[0].instancePath.split("/")[2]; - let position = validate.errors[0].instancePath.split("/")[3]; + if (valid) { + Log.info(colors.green("Your modules structure configuration doesn't contain errors :)")); + } else { + const module = validate.errors[0].instancePath.split("/")[2]; + const position = validate.errors[0].instancePath.split("/")[3]; - Log.error(colors.red("This module configuration contains errors:")); + Log.error("This module configuration contains errors:"); Log.error(`\n${JSON.stringify(data.modules[module], null, 2)}`); if (position) { - Log.error(colors.red(`${position}: ${validate.errors[0].message}`)); + Log.error(`${position}: ${validate.errors[0].message}`); Log.error(`\n${JSON.stringify(validate.errors[0].params.allowedValues, null, 2).slice(1, -1)}`); } else { - Log.error(colors.red(validate.errors[0].message)); + Log.error(validate.errors[0].message); } - } else { - Log.info(colors.green("Your modules structure configuration doesn't contain errors :)")); } }