mirror of
https://github.com/MichMich/MagicMirror.git
synced 2025-12-01 02:21:39 +00:00
- Combine file existence and permission checks with better error messages - Replace thrown exceptions with clean error output (no stack traces) - Support custom module positions by changing strict validation to warnings - Add missing process.exit(1) after validation errors Users now see clear, actionable error messages without stack traces, and custom region configurations work correctly. ## example before ```bash $ npm run start > magicmirror@2.34.0-develop start > node --run start:x11 [2025-10-22 17:56:06.303] [LOG] Starting MagicMirror: v2.34.0-develop [2025-10-22 17:56:06.304] [LOG] Loading config ... [2025-10-22 17:56:06.304] [LOG] config template file not exists, no envsubst [2025-10-22 17:56:06.356] [ERROR] File not found: /home/kristjan/MagicMirror/config/config.js No config file present! [2025-10-22 17:56:06.356] [ERROR] [checkconfig] Error: Error: ENOENT: no such file or directory, access '/home/kristjan/MagicMirror/config/config.js' No permission to access config file! at checkConfigFile (/home/kristjan/MagicMirror/js/check_config.js:43:9) at Object.<anonymous> (/home/kristjan/MagicMirror/js/check_config.js:138:2) at Module._compile (node:internal/modules/cjs/loader:1714:14) at Module._extensions..js (node:internal/modules/cjs/loader:1848:10) at Module.load (node:internal/modules/cjs/loader:1448:32) at Module._load (node:internal/modules/cjs/loader:1270:12) at c._load (node:electron/js2c/node_init:2:17993) at TracingChannel.traceSync (node:diagnostics_channel:322:14) at wrapModuleLoad (node:internal/modules/cjs/loader:244:24) at Module.require (node:internal/modules/cjs/loader:1470:12) at require (node:internal/modules/helpers:147:16) at loadConfig (/home/kristjan/MagicMirror/js/app.js:126:3) at App.start (/home/kristjan/MagicMirror/js/app.js:291:18) at Object.<anonymous> (/home/kristjan/MagicMirror/js/electron.js:228:7) at Module._compile (node:internal/modules/cjs/loader:1714:14) at Module._extensions..js (node:internal/modules/cjs/loader:1848:10) ``` ## example after ```bash $ npm run start > magicmirror@2.34.0-develop start > node --run start:x11 [2025-10-22 21:33:27.930] [LOG] Starting MagicMirror: v2.34.0-develop [2025-10-22 21:33:27.931] [LOG] Loading config ... [2025-10-22 21:33:27.931] [LOG] config template file not exists, no envsubst [2025-10-22 21:33:27.985] [ERROR] [check_config] File not found: /home/kristjan/MagicMirror/config/config.js ```
157 lines
4.3 KiB
JavaScript
157 lines
4.3 KiB
JavaScript
// Ensure internal require aliases (e.g., "logger") resolve when this file is run as a standalone script
|
|
require("./alias-resolver");
|
|
|
|
const path = require("node:path");
|
|
const fs = require("node:fs");
|
|
const { styleText } = require("node:util");
|
|
const Ajv = require("ajv");
|
|
const globals = require("globals");
|
|
const { Linter } = require("eslint");
|
|
const Log = require("logger");
|
|
|
|
const rootPath = path.resolve(`${__dirname}/../`);
|
|
const Utils = require(`${rootPath}/js/utils.js`);
|
|
|
|
const linter = new Linter({ configType: "flat" });
|
|
const ajv = new Ajv();
|
|
|
|
/**
|
|
* Returns a string with path of configuration file.
|
|
* Check if set by environment variable MM_CONFIG_FILE
|
|
* @returns {string} path and filename of the config file
|
|
*/
|
|
function getConfigFile () {
|
|
// FIXME: This function should be in core. Do you want refactor me ;) ?, be good!
|
|
return path.resolve(process.env.MM_CONFIG_FILE || `${rootPath}/config/config.js`);
|
|
}
|
|
|
|
/**
|
|
* Checks the config file using eslint.
|
|
*/
|
|
function checkConfigFile () {
|
|
const configFileName = getConfigFile();
|
|
|
|
// Check if file exists and is accessible
|
|
try {
|
|
fs.accessSync(configFileName, fs.constants.R_OK);
|
|
} catch (error) {
|
|
if (error.code === "ENOENT") {
|
|
Log.error(`File not found: ${configFileName}`);
|
|
} else if (error.code === "EACCES") {
|
|
Log.error(`No permission to read config file: ${configFileName}`);
|
|
} else {
|
|
Log.error(`Cannot access config file: ${configFileName}\n${error.message}`);
|
|
}
|
|
process.exit(1);
|
|
}
|
|
|
|
// Validate syntax of the configuration file.
|
|
Log.info(`Checking config file ${configFileName} ...`);
|
|
|
|
// I'm not sure if all ever is utf-8
|
|
const configFile = fs.readFileSync(configFileName, "utf-8");
|
|
|
|
const errors = linter.verify(
|
|
configFile,
|
|
{
|
|
languageOptions: {
|
|
ecmaVersion: "latest",
|
|
globals: {
|
|
...globals.node
|
|
}
|
|
},
|
|
rules: {
|
|
"no-sparse-arrays": "error",
|
|
"no-undef": "error"
|
|
}
|
|
},
|
|
configFileName
|
|
);
|
|
|
|
if (errors.length === 0) {
|
|
Log.info(styleText("green", "Your configuration file doesn't contain syntax errors :)"));
|
|
validateModulePositions(configFileName);
|
|
} else {
|
|
let errorMessage = "Your configuration file contains syntax errors :(";
|
|
|
|
for (const error of errors) {
|
|
errorMessage += `\nLine ${error.line} column ${error.column}: ${error.message}`;
|
|
}
|
|
Log.error(errorMessage);
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param {string} configFileName - The path and filename of the configuration file to validate.
|
|
*/
|
|
function validateModulePositions (configFileName) {
|
|
Log.info("Checking modules structure configuration ...");
|
|
|
|
const positionList = Utils.getModulePositions();
|
|
|
|
// Make Ajv schema configuration of modules config
|
|
// Only scan "module" and "position"
|
|
const schema = {
|
|
type: "object",
|
|
properties: {
|
|
modules: {
|
|
type: "array",
|
|
items: {
|
|
type: "object",
|
|
properties: {
|
|
module: {
|
|
type: "string"
|
|
},
|
|
position: {
|
|
type: "string"
|
|
}
|
|
},
|
|
required: ["module"]
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
// Scan all modules
|
|
const validate = ajv.compile(schema);
|
|
const data = require(configFileName);
|
|
|
|
const valid = validate(data);
|
|
if (valid) {
|
|
Log.info(styleText("green", "Your modules structure configuration doesn't contain errors :)"));
|
|
|
|
// Check for unknown positions (warning only, not an error)
|
|
if (data.modules) {
|
|
for (const [index, module] of data.modules.entries()) {
|
|
if (module.position && !positionList.includes(module.position)) {
|
|
Log.warn(`Module ${index} ("${module.module}") uses unknown position: "${module.position}"`);
|
|
Log.warn(`Known positions are: ${positionList.join(", ")}`);
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
const module = validate.errors[0].instancePath.split("/")[2];
|
|
const position = validate.errors[0].instancePath.split("/")[3];
|
|
let errorMessage = "This module configuration contains errors:";
|
|
errorMessage += `\n${JSON.stringify(data.modules[module], null, 2)}`;
|
|
if (position) {
|
|
errorMessage += `\n${position}: ${validate.errors[0].message}`;
|
|
errorMessage += `\n${JSON.stringify(validate.errors[0].params.allowedValues, null, 2).slice(1, -1)}`;
|
|
} else {
|
|
errorMessage += validate.errors[0].message;
|
|
}
|
|
Log.error(errorMessage);
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
try {
|
|
checkConfigFile();
|
|
} catch (error) {
|
|
const message = error && error.message ? error.message : error;
|
|
Log.error(`Unexpected error: ${message}`);
|
|
process.exit(1);
|
|
}
|