From 2989831d5e6ed02120eb6d9b3be513c15adc77ca Mon Sep 17 00:00:00 2001 From: Kristjan ESPERANTO <35647502+KristjanESPERANTO@users.noreply.github.com> Date: Tue, 14 May 2024 02:05:56 +0200 Subject: [PATCH 1/3] Check config before starting MM --- CHANGELOG.md | 2 ++ package.json | 6 +++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index eceb4719fe..5fb7576b90 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ _This release is scheduled to be released on 2024-07-01._ ### Added +- Check config at every start of MagicMirror². + ### Removed ### Updated diff --git a/package.json b/package.json index da54181ef6..6d181f2aa9 100644 --- a/package.json +++ b/package.json @@ -24,9 +24,9 @@ ], "main": "js/electron.js", "scripts": { - "start": "DISPLAY=\"${DISPLAY:=:0}\" ./node_modules/.bin/electron js/electron.js", - "start:dev": "DISPLAY=\"${DISPLAY:=:0}\" ./node_modules/.bin/electron js/electron.js dev", - "server": "node ./serveronly", + "start": "node js/check_config.js && DISPLAY=\"${DISPLAY:=:0}\" ./node_modules/.bin/electron js/electron.js", + "start:dev": "node js/check_config.js && DISPLAY=\"${DISPLAY:=:0}\" ./node_modules/.bin/electron js/electron.js dev", + "server": "node js/check_config.js && node ./serveronly", "install-mm": "npm install --no-audit --no-fund --no-update-notifier --only=prod --omit=dev", "install-mm:dev": "npm install --no-audit --no-fund --no-update-notifier", "install-vendor": "echo \"Installing vendor files ...\n\" && cd vendor && npm install --loglevel=error --no-audit --no-fund --no-update-notifier", From a5ce58a5c010095c20151753753ca9ee478e854b Mon Sep 17 00:00:00 2001 From: Kristjan ESPERANTO <35647502+KristjanESPERANTO@users.noreply.github.com> Date: Wed, 18 Sep 2024 03:00:06 +0200 Subject: [PATCH 2/3] Check after creating config from template --- js/app.js | 32 ++++++++++++++++++++++---------- js/check_config.js | 1 + package.json | 6 +++--- 3 files changed, 26 insertions(+), 13 deletions(-) diff --git a/js/app.js b/js/app.js index 77d0dc206f..a5ec91d5f8 100644 --- a/js/app.js +++ b/js/app.js @@ -24,14 +24,18 @@ if (process.env.MM_CONFIG_FILE) { global.configuration_file = process.env.MM_CONFIG_FILE.replace(`${global.root_path}/`, ""); } -// FIXME: Hotfix Pull Request -// https://github.com/MagicMirrorOrg/MagicMirror/pull/673 +/* + * FIXME: Hotfix Pull Request + * https://github.com/MagicMirrorOrg/MagicMirror/pull/673 + */ if (process.env.MM_PORT) { global.mmPort = process.env.MM_PORT; } -// The next part is here to prevent a major exception when there -// is no internet connection. This could probable be solved better. +/* + * The next part is here to prevent a major exception when there + * is no internet connection. This could probable be solved better. + */ process.on("uncaughtException", function (err) { // ignore strange exceptions under aarch64 coming from systeminformation: if (!err.stack.includes("node_modules/systeminformation")) { @@ -59,8 +63,10 @@ function App () { Log.log("Loading config ..."); const defaults = require(`${__dirname}/defaults`); - // For this check proposed to TestSuite - // https://forum.magicmirror.builders/topic/1456/test-suite-for-magicmirror/8 + /* + * For this check proposed to TestSuite + * https://forum.magicmirror.builders/topic/1456/test-suite-for-magicmirror/8 + */ const configFilename = path.resolve(global.configuration_file || `${global.root_path}/config/config.js`); let templateFile = `${configFilename}.template`; @@ -102,8 +108,10 @@ function App () { system: true }; - // envsubst variables in templateFile and create new config.js - // naming for envsub must be templateFile and outputFile + /* + * envsubst variables in templateFile and create new config.js + * naming for envsub must be templateFile and outputFile + */ const outputFile = configFilename; try { await envsub({ templateFile, outputFile, options }); @@ -112,6 +120,8 @@ function App () { } } + require(`${global.root_path}/js/check_config.js`); + try { fs.accessSync(configFilename, fs.F_OK); const c = require(configFilename); @@ -323,8 +333,10 @@ function App () { Log.log("Node_helpers stopped ..."); - // To be able to stop the app even if it hasn't been started (when - // running with Electron against another server) + /* + * To be able to stop the app even if it hasn't been started (when + * running with Electron against another server) + */ if (!httpServer) { return Promise.resolve(); } diff --git a/js/check_config.js b/js/check_config.js index ec5ebcae66..756a91fb0e 100644 --- a/js/check_config.js +++ b/js/check_config.js @@ -59,6 +59,7 @@ function checkConfigFile () { for (const error of errors) { Log.error(`Line ${error.line} column ${error.column}: ${error.message}`); } + process.exit(1); } } diff --git a/package.json b/package.json index 6d181f2aa9..da54181ef6 100644 --- a/package.json +++ b/package.json @@ -24,9 +24,9 @@ ], "main": "js/electron.js", "scripts": { - "start": "node js/check_config.js && DISPLAY=\"${DISPLAY:=:0}\" ./node_modules/.bin/electron js/electron.js", - "start:dev": "node js/check_config.js && DISPLAY=\"${DISPLAY:=:0}\" ./node_modules/.bin/electron js/electron.js dev", - "server": "node js/check_config.js && node ./serveronly", + "start": "DISPLAY=\"${DISPLAY:=:0}\" ./node_modules/.bin/electron js/electron.js", + "start:dev": "DISPLAY=\"${DISPLAY:=:0}\" ./node_modules/.bin/electron js/electron.js dev", + "server": "node ./serveronly", "install-mm": "npm install --no-audit --no-fund --no-update-notifier --only=prod --omit=dev", "install-mm:dev": "npm install --no-audit --no-fund --no-update-notifier", "install-vendor": "echo \"Installing vendor files ...\n\" && cd vendor && npm install --loglevel=error --no-audit --no-fund --no-update-notifier", From e48b992ebef4128ec2e0e66357535311e9a9cad1 Mon Sep 17 00:00:00 2001 From: Kristjan ESPERANTO <35647502+KristjanESPERANTO@users.noreply.github.com> Date: Wed, 18 Sep 2024 03:19:17 +0200 Subject: [PATCH 3/3] Undo some formating to keep the PR clean --- js/app.js | 30 ++++++++++-------------------- js/check_config.js | 6 ++---- 2 files changed, 12 insertions(+), 24 deletions(-) diff --git a/js/app.js b/js/app.js index 0326323200..b42b515fae 100644 --- a/js/app.js +++ b/js/app.js @@ -24,18 +24,14 @@ if (process.env.MM_CONFIG_FILE) { global.configuration_file = process.env.MM_CONFIG_FILE.replace(`${global.root_path}/`, ""); } -/* - * FIXME: Hotfix Pull Request - * https://github.com/MagicMirrorOrg/MagicMirror/pull/673 - */ +// FIXME: Hotfix Pull Request +// https://github.com/MagicMirrorOrg/MagicMirror/pull/673 if (process.env.MM_PORT) { global.mmPort = process.env.MM_PORT; } -/* - * The next part is here to prevent a major exception when there - * is no internet connection. This could probable be solved better. - */ +// The next part is here to prevent a major exception when there +// is no internet connection. This could probable be solved better. process.on("uncaughtException", function (err) { // ignore strange exceptions under aarch64 coming from systeminformation: if (!err.stack.includes("node_modules/systeminformation")) { @@ -67,10 +63,8 @@ function App () { defaults.address = "0.0.0.0"; } - /* - * For this check proposed to TestSuite - * https://forum.magicmirror.builders/topic/1456/test-suite-for-magicmirror/8 - */ + // For this check proposed to TestSuite + // https://forum.magicmirror.builders/topic/1456/test-suite-for-magicmirror/8 const configFilename = path.resolve(global.configuration_file || `${global.root_path}/config/config.js`); let templateFile = `${configFilename}.template`; @@ -112,10 +106,8 @@ function App () { system: true }; - /* - * envsubst variables in templateFile and create new config.js - * naming for envsub must be templateFile and outputFile - */ + // envsubst variables in templateFile and create new config.js + // naming for envsub must be templateFile and outputFile const outputFile = configFilename; try { await envsub({ templateFile, outputFile, options }); @@ -351,10 +343,8 @@ function App () { Log.log("Node_helpers stopped ..."); - /* - * To be able to stop the app even if it hasn't been started (when - * running with Electron against another server) - */ + // To be able to stop the app even if it hasn't been started (when + // running with Electron against another server) if (!httpServer) { return Promise.resolve(); } diff --git a/js/check_config.js b/js/check_config.js index fc18bc0dfb..a0f9867ae7 100644 --- a/js/check_config.js +++ b/js/check_config.js @@ -71,10 +71,8 @@ function checkConfigFile () { const position_list = Utils.getModulePositions(); - /* - * Make Ajv schema configuration of modules config - * only scan "module" and "position" - */ + // Make Ajv schema confguration of modules config + // only scan "module" and "position" const schema = { type: "object", properties: {