Skip to content

Commit 2a031d7

Browse files
refactor: use explicit global config
1 parent 00350ee commit 2a031d7

14 files changed

Lines changed: 38 additions & 39 deletions

File tree

defaultmodules/calendar/calendar.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ Module.register("calendar", {
105105
}
106106

107107
// Set locale.
108-
moment.updateLocale(config.language, CalendarUtils.getLocaleSpecification(config.timeFormat));
108+
moment.updateLocale(globalThis.config.language, CalendarUtils.getLocaleSpecification(globalThis.config.timeFormat));
109109

110110
// clear data holder before start
111111
this.calendarData = {};

defaultmodules/clock/clock.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Module.register("clock", {
55
defaults: {
66
displayType: "digital", // options: digital, analog, both
77

8-
timeFormat: config.timeFormat,
8+
timeFormat: globalThis.config.timeFormat,
99
timezone: null,
1010

1111
displaySeconds: true,
@@ -85,7 +85,7 @@ Module.register("clock", {
8585
setTimeout(notificationTimer, delayCalculator(this.second));
8686

8787
// Set locale.
88-
moment.locale(config.language);
88+
moment.locale(globalThis.config.language);
8989
},
9090
// Override dom generator.
9191
getDom () {

defaultmodules/newsfeed/newsfeed.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Module.register("newsfeed", {
3939

4040
getUrlPrefix (item) {
4141
if (item.useCorsProxy) {
42-
return `${location.protocol}//${location.host}${config.basePath}cors?url=`;
42+
return `${location.protocol}//${location.host}${globalThis.config.basePath}cors?url=`;
4343
} else {
4444
return "";
4545
}
@@ -68,7 +68,7 @@ Module.register("newsfeed", {
6868
Log.info(`Starting module: ${this.name}`);
6969

7070
// Set locale.
71-
moment.locale(config.language);
71+
moment.locale(globalThis.config.language);
7272

7373
this.newsItems = [];
7474
this.loaded = false;

defaultmodules/weather/weather.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ Module.register("weather", {
66
weatherProvider: "openweathermap",
77
roundTemp: false,
88
type: "current", // current, forecast, daily (equivalent to forecast), hourly
9-
lang: config.language,
10-
units: config.units,
11-
tempUnits: config.units,
12-
windUnits: config.units,
13-
timeFormat: config.timeFormat,
9+
lang: globalThis.config.language,
10+
units: globalThis.config.units,
11+
tempUnits: globalThis.config.units,
12+
windUnits: globalThis.config.units,
13+
timeFormat: globalThis.config.timeFormat,
1414
updateInterval: 10 * 60 * 1000, // every 10 minutes
1515
animationSpeed: 1000,
1616
showFeelsLike: true,

eslint.config.mjs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ export default defineConfig([
4141
Log: "readonly",
4242
MM: "readonly",
4343
Module: "readonly",
44-
config: "readonly",
4544
moment: "readonly"
4645
}
4746
},

js/loader.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ const moduleObjects = [];
1212
*/
1313
function getEnvVarsFromConfig () {
1414
return {
15-
modulesDir: config.foreignModulesDir || "modules",
16-
defaultModulesDir: config.defaultModulesDir || "defaultmodules",
17-
customCss: config.customCss || "config/custom.css"
15+
modulesDir: globalThis.config.foreignModulesDir || "modules",
16+
defaultModulesDir: globalThis.config.defaultModulesDir || "defaultmodules",
17+
customCss: globalThis.config.customCss || "config/custom.css"
1818
};
1919
}
2020

@@ -30,7 +30,7 @@ async function getEnvVars () {
3030

3131
// In production, fetch env vars from server
3232
try {
33-
const res = await fetch(new URL("env", `${location.origin}${config.basePath}`));
33+
const res = await fetch(new URL("env", `${location.origin}${globalThis.config.basePath}`));
3434
return JSON.parse(await res.text());
3535
} catch (error) {
3636
// Fallback to config values if server fetch fails
@@ -79,7 +79,7 @@ async function startModules () {
7979
* @returns {object[]} module data as configured in config
8080
*/
8181
function getAllModules () {
82-
const AllModules = config.modules.filter((module) => (module.module !== undefined) && (MM.getAvailableModulePositions.indexOf(module.position) > -1 || typeof (module.position) === "undefined"));
82+
const AllModules = globalThis.config.modules.filter((module) => (module.module !== undefined) && (MM.getAvailableModulePositions.indexOf(module.position) > -1 || typeof (module.position) === "undefined"));
8383
return AllModules;
8484
}
8585

js/main.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -451,12 +451,12 @@ function updateWrapperStates () {
451451
*/
452452
async function loadConfig () {
453453
try {
454-
const res = await fetch(new URL("config/", `${location.origin}${config.basePath}`));
454+
const res = await fetch(new URL("config/", `${location.origin}${globalThis.config.basePath}`));
455455

456456
// The server tags functions as { __mmFunction: "<source>" } because
457457
// JSON.stringify can't serialise live functions. This reviver turns
458458
// those tagged objects back into callable functions.
459-
config = JSON.parse(await res.text(), (key, value) => {
459+
globalThis.config = JSON.parse(await res.text(), (key, value) => {
460460
if (value && typeof value === "object" && typeof value.__mmFunction === "string") {
461461
try {
462462
return new Function(`return (${value.__mmFunction})`)();
@@ -572,9 +572,9 @@ export const MM = {
572572
Log.info("Initializing MagicMirror².");
573573
await loadConfig();
574574

575-
Log.setLogLevel(config.logLevel);
575+
Log.setLogLevel(globalThis.config.logLevel);
576576

577-
await Translator.loadCoreTranslations(config.language);
577+
await Translator.loadCoreTranslations(globalThis.config.language);
578578
await loadModules();
579579
},
580580

@@ -595,20 +595,20 @@ export const MM = {
595595

596596
// Setup global socket listener for RELOAD event (watch mode)
597597
const socket = io("/", {
598-
path: `${config.basePath || "/"}socket.io`
598+
path: `${globalThis.config.basePath || "/"}socket.io`
599599
});
600600

601601
socket.on("RELOAD", () => {
602602
Log.warn("Reload notification received from server");
603603
window.location.reload(true);
604604
});
605605

606-
if (config.reloadAfterServerRestart) {
606+
if (globalThis.config.reloadAfterServerRestart) {
607607
setInterval(async () => {
608608
// if server startup time has changed (which means server was restarted)
609609
// the client reloads the mm page
610610
try {
611-
const res = await fetch(`${location.protocol}//${location.host}${config.basePath}startup`);
611+
const res = await fetch(`${location.protocol}//${location.host}${globalThis.config.basePath}startup`);
612612
const curr = await res.text();
613613
if (startUp === "") startUp = curr;
614614
if (startUp !== curr) {
@@ -619,7 +619,7 @@ export const MM = {
619619
} catch (err) {
620620
Log.error(`MagicMirror not reachable: ${err}`);
621621
}
622-
}, config.checkServerInterval);
622+
}, globalThis.config.checkServerInterval);
623623
}
624624
},
625625

js/module.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ export class Module {
298298
*/
299299
async loadTranslations () {
300300
const translations = this.getTranslations() || {};
301-
const language = config.language.toLowerCase();
301+
const language = globalThis.config.language.toLowerCase();
302302

303303
const languages = Object.keys(translations);
304304
const fallbackLanguage = languages[0];

js/node_helper.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ class NodeHelper {
108108
io.of(this.name).on("connection", (socket) => {
109109
// register catch all.
110110
socket.onAny((notification, payload) => {
111-
if (config?.hideConfigSecrets && payload && typeof payload === "object") {
111+
if (global.config?.hideConfigSecrets && payload && typeof payload === "object") {
112112
try {
113113
// Calculate exactly which secrets this module is allowed to receive
114114
const allowedSecrets = getAllowedSecrets(this.name);

js/server_functions.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ async function cors (req, res) {
7171
} else {
7272
url = match[1];
7373
if (typeof global.config !== "undefined") {
74-
if (config.hideConfigSecrets) {
74+
if (global.config.hideConfigSecrets) {
7575
url = replaceSecretPlaceholder(url);
7676
}
7777
}

0 commit comments

Comments
 (0)