Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Review config_check.js #3545

Merged
merged 1 commit into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

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

Expand Down
1 change: 1 addition & 0 deletions cspell.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"crazyscot",
"Creepin",
"currentweather",
"CUSTOMCSS",
"customregions",
"Cymraeg",
"dariom",
Expand Down
46 changes: 22 additions & 24 deletions js/check_config.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -38,38 +36,38 @@ 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}`);
}
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"
Expand All @@ -86,7 +84,7 @@ function checkConfigFile () {
},
position: {
type: "string",
enum: position_list
enum: positionList
}
},
required: ["module"]
Expand All @@ -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 :)"));
}
}

Expand Down
Loading