Skip to content

Commit 3b6b96e

Browse files
committed
add http(s) support and minor changes
* [eslint] max-lines-per-function set to 410 * [core] move Utils.logSystemInformation() to better place * add description of functions
1 parent fc80362 commit 3b6b96e

File tree

3 files changed

+51
-22
lines changed

3 files changed

+51
-22
lines changed

eslint.config.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ const config = [
6868
"jest/prefer-mock-promise-shorthand": "warn",
6969
"jest/prefer-to-be": "warn",
7070
"jest/prefer-to-have-length": "warn",
71-
"max-lines-per-function": ["warn", 400],
71+
"max-lines-per-function": ["warn", 410],
7272
"max-statements": "off",
7373
"no-global-assign": "off",
7474
"no-inline-comments": "off",

js/app.js

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@ global.version = require(`${__dirname}/../package.json`).version;
2222
global.mmTestMode = process.env.mmTestMode === "true";
2323
Log.log(`Starting MagicMirror: v${global.version}`);
2424

25-
// Log system information.
26-
Utils.logSystemInformation();
27-
2825
// global absolute root path
2926
global.root_path = path.resolve(`${__dirname}/../`);
3027

@@ -246,25 +243,47 @@ function App () {
246243
Log.log("All module helpers loaded.");
247244
}
248245

246+
/**
247+
* Load remote config file of a module from moduleConfig string
248+
* @param {object} module object
249+
* @returns {object} module config found
250+
*/
249251
function loadModuleConfig (module) {
250-
var moduleFolder = path.resolve(`${__dirname}/../modules/`, module.module);
252+
return new Promise(function (resolve) {
253+
if (module.moduleConfig.startsWith("http://") || module.moduleConfig.startsWith("https://")) {
254+
Log.info(`Loading config for ${module.module} from ${module.moduleConfig}`);
255+
fetch(`${module.moduleConfig}`)
256+
.then((response) => response.text())
257+
.then((txt) => {
258+
const configFile = eval(txt);
259+
Log.debug("Config Result:", configFile);
260+
resolve(configFile);
261+
})
262+
.catch((e) => {
263+
Log.error(`Config loading error for module: ${module.module}.`, e.message);
264+
resolve(null);
265+
});
266+
} else {
267+
var moduleFolder = path.resolve(`${__dirname}/../modules/`, module.module);
251268

252-
if (defaultModules.includes(module.module)) {
253-
moduleFolder = path.resolve(`${__dirname}/../modules/default/`, module.module);
254-
}
269+
if (defaultModules.includes(module.module)) {
270+
moduleFolder = path.resolve(`${__dirname}/../modules/default/`, module.module);
271+
}
255272

256-
const moduleConfigFile = `${moduleFolder}/config/${module.moduleConfig}`;
257-
Log.info(`Loading config for ${module.module} in ${moduleConfigFile}`);
258-
try {
259-
fs.accessSync(moduleConfigFile, fs.R_OK);
260-
const configFile = eval(require(moduleConfigFile));
261-
Log.debug("Config Result:", configFile);
262-
return configFile;
263-
} catch (e) {
264-
Log.error(`Config loading error for module: ${module.module}.`, e.message);
265-
Log.error(`Config: ${moduleConfigFile}`);
266-
return null;
267-
}
273+
const moduleConfigFile = `${moduleFolder}/config/${module.moduleConfig}`;
274+
Log.info(`Loading config for ${module.module} in ${moduleConfigFile}`);
275+
try {
276+
fs.accessSync(moduleConfigFile, fs.R_OK);
277+
const configFile = eval(require(moduleConfigFile));
278+
Log.debug("Config Result:", configFile);
279+
resolve(configFile);
280+
} catch (e) {
281+
Log.error(`Config loading error for module: ${module.module}.`, e.message);
282+
Log.error(`Config: ${moduleConfigFile}`);
283+
resolve(null);
284+
}
285+
}
286+
});
268287
}
269288

270289
/**
@@ -298,6 +317,8 @@ function App () {
298317
* @returns {Promise<object>} the config used
299318
*/
300319
this.start = async function () {
320+
// Log system information.
321+
await Utils.logSystemInformation();
301322
config = await loadConfig();
302323
Log.setLogLevel(config.logLevel);
303324

js/module.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,17 +226,25 @@ const Module = Class.extend({
226226
this.config = deep ? configMerge({}, this.defaults, config) : Object.assign({}, this.defaults, config);
227227
},
228228

229+
230+
/**
231+
* Load remote config file of a module from moduleConfig string
232+
* @param {object} module object
233+
*/
229234
moduleConfig (module) {
230235
return new Promise(function (resolve) {
231-
fetch(`${module.path}/config/${module.moduleConfig}`)
236+
const isRemote = module.moduleConfig.startsWith("http://") || module.moduleConfig.startsWith("https://");
237+
const url = isRemote ? module.moduleConfig : `${module.path}/config/${module.moduleConfig}`;
238+
239+
fetch(url)
232240
.then((response) => response.text())
233241
.then((txt) => {
234242
const definedConfig = eval(txt);
235243
module.config = definedConfig;
236244
resolve();
237245
})
238246
.catch((error) => {
239-
console.error("Error when loading File:", error);
247+
Log.error("Error when loading File:", error);
240248
resolve();
241249
});
242250
});

0 commit comments

Comments
 (0)