Review config_check.js (#3545)

Only details changes. No functional changes.

- remove superfluous colors in Log.error
- invert negative if
- update ESLint env
- use camel case variable name
- optimize Log strings
This commit is contained in:
Kristjan ESPERANTO 2024-09-18 21:39:02 +02:00 committed by GitHub
parent c6e05c9fec
commit 06f6fbf49b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 27 additions and 27 deletions

View File

@ -12,9 +12,9 @@ _This release is scheduled to be released on 2024-10-01._
### Added ### Added
- [core] Check config at every start of MagicMirror² (#3450) - [core] Check config at every start of MagicMirror² (#3450)
- [core] Add spelling check (cspell): `npm run test:spelling` and handle spelling issues - [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` - [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 - [core] elements are now removed from `index.html` when loading script or stylesheet files fails
### Removed ### Removed
@ -27,6 +27,7 @@ _This release is scheduled to be released on 2024-10-01._
- [core] Updated dependencies including stylistic-eslint - [core] Updated dependencies including stylistic-eslint
- [core] Updated SocketIO catch all to new API - [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] 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 ### Fixed

View File

@ -36,6 +36,7 @@
"crazyscot", "crazyscot",
"Creepin", "Creepin",
"currentweather", "currentweather",
"CUSTOMCSS",
"customregions", "customregions",
"Cymraeg", "Cymraeg",
"dariom", "dariom",

View File

@ -1,18 +1,16 @@
const path = require("node:path"); const path = require("node:path");
const fs = require("node:fs"); const fs = require("node:fs");
const Ajv = require("ajv");
const colors = require("ansis"); const colors = require("ansis");
const { Linter } = require("eslint"); const { Linter } = require("eslint");
const linter = new Linter();
const Ajv = require("ajv");
const ajv = new Ajv();
const rootPath = path.resolve(`${__dirname}/../`); const rootPath = path.resolve(`${__dirname}/../`);
const Log = require(`${rootPath}/js/logger.js`); const Log = require(`${rootPath}/js/logger.js`);
const Utils = require(`${rootPath}/js/utils.js`); const Utils = require(`${rootPath}/js/utils.js`);
const linter = new Linter();
const ajv = new Ajv();
/** /**
* Returns a string with path of configuration file. * Returns a string with path of configuration file.
* Check if set by environment variable MM_CONFIG_FILE * Check if set by environment variable MM_CONFIG_FILE
@ -38,28 +36,28 @@ function checkConfigFile () {
// Check permission // Check permission
try { try {
fs.accessSync(configFileName, fs.F_OK); fs.accessSync(configFileName, fs.F_OK);
} catch (e) { } catch (error) {
Log.error(e); Log.error(error);
throw new Error("No permission to access config file!"); throw new Error("No permission to access config file!");
} }
// Validate syntax of the configuration 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 // I'm not sure if all ever is utf-8
const configFile = fs.readFileSync(configFileName, "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, { const errors = linter.verify(configFile, {
env: { env: {
es6: true es2024: true
} }
}); });
if (errors.length === 0) { if (errors.length === 0) {
Log.info(colors.green("Your configuration file doesn't contain syntax errors :)")); Log.info(colors.green("Your configuration file doesn't contain syntax errors :)"));
} else { } else {
Log.error(colors.red("Your configuration file contains syntax errors :(")); Log.error("Your configuration file contains syntax errors :(");
for (const error of errors) { for (const error of errors) {
Log.error(`Line ${error.line} column ${error.column}: ${error.message}`); Log.error(`Line ${error.line} column ${error.column}: ${error.message}`);
@ -67,9 +65,9 @@ function checkConfigFile () {
process.exit(1); 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 // Make Ajv schema configuration of modules config
// only scan "module" and "position" // only scan "module" and "position"
@ -86,7 +84,7 @@ function checkConfigFile () {
}, },
position: { position: {
type: "string", type: "string",
enum: position_list enum: positionList
} }
}, },
required: ["module"] required: ["module"]
@ -95,25 +93,25 @@ function checkConfigFile () {
} }
}; };
// scan all modules // Scan all modules
const validate = ajv.compile(schema); const validate = ajv.compile(schema);
const data = require(configFileName); const data = require(configFileName);
const valid = validate(data); const valid = validate(data);
if (!valid) { if (valid) {
let module = validate.errors[0].instancePath.split("/")[2]; Log.info(colors.green("Your modules structure configuration doesn't contain errors :)"));
let position = validate.errors[0].instancePath.split("/")[3]; } 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)}`); Log.error(`\n${JSON.stringify(data.modules[module], null, 2)}`);
if (position) { 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)}`); Log.error(`\n${JSON.stringify(validate.errors[0].params.allowedValues, null, 2).slice(1, -1)}`);
} else { } 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 :)"));
} }
} }