mirror of
https://github.com/MichMich/MagicMirror.git
synced 2025-06-27 19:53:36 +00:00
clean up app
This commit is contained in:
parent
9c8fa06ce1
commit
38f10b6e3e
128
js/app.js
128
js/app.js
@ -4,22 +4,22 @@
|
|||||||
* By Michael Teeuw https://michaelteeuw.nl
|
* By Michael Teeuw https://michaelteeuw.nl
|
||||||
* MIT Licensed.
|
* MIT Licensed.
|
||||||
*/
|
*/
|
||||||
var fs = require("fs");
|
const fs = require("fs");
|
||||||
var path = require("path");
|
const path = require("path");
|
||||||
var Log = require(__dirname + "/logger.js");
|
const Log = require(`${__dirname}/logger`);
|
||||||
var Server = require(__dirname + "/server.js");
|
const Server = require(`${__dirname}/server`);
|
||||||
var Utils = require(__dirname + "/utils.js");
|
const Utils = require(`${__dirname}/utils`);
|
||||||
var defaultModules = require(__dirname + "/../modules/default/defaultmodules.js");
|
const defaultModules = require(`${__dirname}/../modules/default/defaultmodules`);
|
||||||
|
|
||||||
// Alias modules mentioned in package.js under _moduleAliases.
|
// Alias modules mentioned in package.js under _moduleAliases.
|
||||||
require("module-alias/register");
|
require("module-alias/register");
|
||||||
|
|
||||||
// Get version number.
|
// Get version number.
|
||||||
global.version = JSON.parse(fs.readFileSync("package.json", "utf8")).version;
|
global.version = require(`${__dirname}/../package.json`).version;
|
||||||
Log.log("Starting MagicMirror: v" + global.version);
|
Log.log("Starting MagicMirror: v" + global.version);
|
||||||
|
|
||||||
// global absolute root path
|
// global absolute root path
|
||||||
global.root_path = path.resolve(__dirname + "/../");
|
global.root_path = path.resolve(`${__dirname}/../`);
|
||||||
|
|
||||||
if (process.env.MM_CONFIG_FILE) {
|
if (process.env.MM_CONFIG_FILE) {
|
||||||
global.configuration_file = process.env.MM_CONFIG_FILE;
|
global.configuration_file = process.env.MM_CONFIG_FILE;
|
||||||
@ -45,8 +45,8 @@ process.on("uncaughtException", function (err) {
|
|||||||
*
|
*
|
||||||
* @class
|
* @class
|
||||||
*/
|
*/
|
||||||
var App = function () {
|
function App() {
|
||||||
var nodeHelpers = [];
|
let nodeHelpers = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Loads the config file. Combines it with the defaults, and runs the
|
* Loads the config file. Combines it with the defaults, and runs the
|
||||||
@ -54,34 +54,31 @@ var App = function () {
|
|||||||
*
|
*
|
||||||
* @param {Function} callback Function to be called after loading the config
|
* @param {Function} callback Function to be called after loading the config
|
||||||
*/
|
*/
|
||||||
var loadConfig = function (callback) {
|
function loadConfig(callback) {
|
||||||
Log.log("Loading config ...");
|
Log.log("Loading config ...");
|
||||||
var defaults = require(__dirname + "/defaults.js");
|
const defaults = require(`${__dirname}/defaults`);
|
||||||
|
|
||||||
// For this check proposed to TestSuite
|
// For this check proposed to TestSuite
|
||||||
// https://forum.magicmirror.builders/topic/1456/test-suite-for-magicmirror/8
|
// https://forum.magicmirror.builders/topic/1456/test-suite-for-magicmirror/8
|
||||||
var configFilename = path.resolve(global.root_path + "/config/config.js");
|
const configFilename = path.resolve(global.configuration_file || `${global.root_path}/config/config`);
|
||||||
if (typeof global.configuration_file !== "undefined") {
|
|
||||||
configFilename = path.resolve(global.configuration_file);
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
fs.accessSync(configFilename, fs.F_OK);
|
fs.accessSync(configFilename, fs.F_OK);
|
||||||
var c = require(configFilename);
|
const c = require(configFilename);
|
||||||
checkDeprecatedOptions(c);
|
checkDeprecatedOptions(c);
|
||||||
var config = Object.assign(defaults, c);
|
const config = Object.assign(defaults, c);
|
||||||
callback(config);
|
callback(config);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e.code === "ENOENT") {
|
if (e.code === "ENOENT") {
|
||||||
Log.error(Utils.colors.error("WARNING! Could not find config file. Please create one. Starting with default configuration."));
|
Log.error(Utils.colors.error("WARNING! Could not find config file. Please create one. Starting with default configuration."));
|
||||||
} else if (e instanceof ReferenceError || e instanceof SyntaxError) {
|
} else if (e instanceof ReferenceError || e instanceof SyntaxError) {
|
||||||
Log.error(Utils.colors.error("WARNING! Could not validate config file. Starting with default configuration. Please correct syntax errors at or above this line: " + e.stack));
|
Log.error(Utils.colors.error(`WARNING! Could not validate config file. Starting with default configuration. Please correct syntax errors at or above this line: ${e.stack}`));
|
||||||
} else {
|
} else {
|
||||||
Log.error(Utils.colors.error("WARNING! Could not load config file. Starting with default configuration. Error found: " + e));
|
Log.error(Utils.colors.error(`WARNING! Could not load config file. Starting with default configuration. Error found: ${e}`));
|
||||||
}
|
}
|
||||||
callback(defaults);
|
callback(defaults);
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks the config for deprecated options and throws a warning in the logs
|
* Checks the config for deprecated options and throws a warning in the logs
|
||||||
@ -89,21 +86,15 @@ var App = function () {
|
|||||||
*
|
*
|
||||||
* @param {object} userConfig The user config
|
* @param {object} userConfig The user config
|
||||||
*/
|
*/
|
||||||
var checkDeprecatedOptions = function (userConfig) {
|
function checkDeprecatedOptions(userConfig) {
|
||||||
var deprecated = require(global.root_path + "/js/deprecated.js");
|
const deprecated = require(`${global.root_path}/js/deprecated`);
|
||||||
var deprecatedOptions = deprecated.configs;
|
const deprecatedOptions = deprecated.configs;
|
||||||
|
|
||||||
var usedDeprecated = [];
|
const usedDeprecated = deprecatedOptions.filter(option => userConfig.hasOwnProperty(option));
|
||||||
|
|
||||||
deprecatedOptions.forEach(function (option) {
|
|
||||||
if (userConfig.hasOwnProperty(option)) {
|
|
||||||
usedDeprecated.push(option);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
if (usedDeprecated.length > 0) {
|
if (usedDeprecated.length > 0) {
|
||||||
Log.warn(Utils.colors.warn("WARNING! Your config is using deprecated options: " + usedDeprecated.join(", ") + ". Check README and CHANGELOG for more up-to-date ways of getting the same functionality."));
|
Log.warn(Utils.colors.warn(`WARNING! Your config is using deprecated options: ${usedDeprecated.join(", ")}. Check README and CHANGELOG for more up-to-date ways of getting the same functionality.`));
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Loads a specific module.
|
* Loads a specific module.
|
||||||
@ -111,35 +102,35 @@ var App = function () {
|
|||||||
* @param {string} module The name of the module (including subpath).
|
* @param {string} module The name of the module (including subpath).
|
||||||
* @param {Function} callback Function to be called after loading
|
* @param {Function} callback Function to be called after loading
|
||||||
*/
|
*/
|
||||||
var loadModule = function (module, callback) {
|
function loadModule(module, callback) {
|
||||||
var elements = module.split("/");
|
const elements = module.split("/");
|
||||||
var moduleName = elements[elements.length - 1];
|
const moduleName = elements[elements.length - 1];
|
||||||
var moduleFolder = __dirname + "/../modules/" + module;
|
let moduleFolder = `${__dirname}/../modules/${module}`;
|
||||||
|
|
||||||
if (defaultModules.indexOf(moduleName) !== -1) {
|
if (defaultModules.includes(moduleName)) {
|
||||||
moduleFolder = __dirname + "/../modules/default/" + module;
|
moduleFolder = `${__dirname}/../modules/default/${module}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
var helperPath = moduleFolder + "/node_helper.js";
|
const helperPath = `${moduleFolder}/node_helper.js`;
|
||||||
|
|
||||||
var loadModule = true;
|
let loadHelper = true;
|
||||||
try {
|
try {
|
||||||
fs.accessSync(helperPath, fs.R_OK);
|
fs.accessSync(helperPath, fs.R_OK);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
loadModule = false;
|
loadHelper = false;
|
||||||
Log.log("No helper found for module: " + moduleName + ".");
|
Log.log(`No helper found for module: ${moduleName}.`);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (loadModule) {
|
if (loadHelper) {
|
||||||
var Module = require(helperPath);
|
const Module = require(helperPath);
|
||||||
var m = new Module();
|
let m = new Module();
|
||||||
|
|
||||||
if (m.requiresVersion) {
|
if (m.requiresVersion) {
|
||||||
Log.log("Check MagicMirror version for node helper '" + moduleName + "' - Minimum version: " + m.requiresVersion + " - Current version: " + global.version);
|
Log.log(`Check MagicMirror version for node helper '${moduleName}' - Minimum version: ${m.requiresVersion} - Current version: ${global.version}`);
|
||||||
if (cmpVersions(global.version, m.requiresVersion) >= 0) {
|
if (cmpVersions(global.version, m.requiresVersion) >= 0) {
|
||||||
Log.log("Version is ok!");
|
Log.log("Version is ok!");
|
||||||
} else {
|
} else {
|
||||||
Log.log("Version is incorrect. Skip module: '" + moduleName + "'");
|
Log.warn(`Version is incorrect. Skip module: '${moduleName}'`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -152,7 +143,7 @@ var App = function () {
|
|||||||
} else {
|
} else {
|
||||||
callback();
|
callback();
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Loads all modules.
|
* Loads all modules.
|
||||||
@ -160,12 +151,12 @@ var App = function () {
|
|||||||
* @param {Module[]} modules All modules to be loaded
|
* @param {Module[]} modules All modules to be loaded
|
||||||
* @param {Function} callback Function to be called after loading
|
* @param {Function} callback Function to be called after loading
|
||||||
*/
|
*/
|
||||||
var loadModules = function (modules, callback) {
|
function loadModules(modules, callback) {
|
||||||
Log.log("Loading module helpers ...");
|
Log.log("Loading module helpers ...");
|
||||||
|
|
||||||
var loadNextModule = function () {
|
function loadNextModule() {
|
||||||
if (modules.length > 0) {
|
if (modules.length > 0) {
|
||||||
var nextModule = modules[0];
|
const nextModule = modules[0];
|
||||||
loadModule(nextModule, function () {
|
loadModule(nextModule, function () {
|
||||||
modules = modules.slice(1);
|
modules = modules.slice(1);
|
||||||
loadNextModule();
|
loadNextModule();
|
||||||
@ -175,10 +166,10 @@ var App = function () {
|
|||||||
Log.log("All module helpers loaded.");
|
Log.log("All module helpers loaded.");
|
||||||
callback();
|
callback();
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
loadNextModule();
|
loadNextModule();
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Compare two semantic version numbers and return the difference.
|
* Compare two semantic version numbers and return the difference.
|
||||||
@ -190,11 +181,11 @@ var App = function () {
|
|||||||
* number if a is smaller and 0 if they are the same
|
* number if a is smaller and 0 if they are the same
|
||||||
*/
|
*/
|
||||||
function cmpVersions(a, b) {
|
function cmpVersions(a, b) {
|
||||||
var i, diff;
|
let i, diff;
|
||||||
var regExStrip0 = /(\.0+)+$/;
|
const regExStrip0 = /(\.0+)+$/;
|
||||||
var segmentsA = a.replace(regExStrip0, "").split(".");
|
const segmentsA = a.replace(regExStrip0, "").split(".");
|
||||||
var segmentsB = b.replace(regExStrip0, "").split(".");
|
const segmentsB = b.replace(regExStrip0, "").split(".");
|
||||||
var l = Math.min(segmentsA.length, segmentsB.length);
|
const l = Math.min(segmentsA.length, segmentsB.length);
|
||||||
|
|
||||||
for (i = 0; i < l; i++) {
|
for (i = 0; i < l; i++) {
|
||||||
diff = parseInt(segmentsA[i], 10) - parseInt(segmentsB[i], 10);
|
diff = parseInt(segmentsA[i], 10) - parseInt(segmentsB[i], 10);
|
||||||
@ -219,21 +210,19 @@ var App = function () {
|
|||||||
|
|
||||||
Log.setLogLevel(config.logLevel);
|
Log.setLogLevel(config.logLevel);
|
||||||
|
|
||||||
var modules = [];
|
let modules = [];
|
||||||
|
|
||||||
for (var m in config.modules) {
|
for (const module of config.modules) {
|
||||||
var module = config.modules[m];
|
if (!modules.includes(module.module) && !module.disabled) {
|
||||||
if (modules.indexOf(module.module) === -1 && !module.disabled) {
|
|
||||||
modules.push(module.module);
|
modules.push(module.module);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
loadModules(modules, function () {
|
loadModules(modules, function () {
|
||||||
var server = new Server(config, function (app, io) {
|
const server = new Server(config, function (app, io) {
|
||||||
Log.log("Server started ...");
|
Log.log("Server started ...");
|
||||||
|
|
||||||
for (var h in nodeHelpers) {
|
for (let nodeHelper of nodeHelpers) {
|
||||||
var nodeHelper = nodeHelpers[h];
|
|
||||||
nodeHelper.setExpressApp(app);
|
nodeHelper.setExpressApp(app);
|
||||||
nodeHelper.setSocketIO(io);
|
nodeHelper.setSocketIO(io);
|
||||||
nodeHelper.start();
|
nodeHelper.start();
|
||||||
@ -247,7 +236,7 @@ var App = function () {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Stops the core app. This calls each node_helper's STOP() function, if it
|
* Stops the core app. This calls each node_helper's STOP() function, if it
|
||||||
@ -256,8 +245,7 @@ var App = function () {
|
|||||||
* Added to fix #1056
|
* Added to fix #1056
|
||||||
*/
|
*/
|
||||||
this.stop = function () {
|
this.stop = function () {
|
||||||
for (var h in nodeHelpers) {
|
for (const nodeHelper of nodeHelpers) {
|
||||||
var nodeHelper = nodeHelpers[h];
|
|
||||||
if (typeof nodeHelper.stop === "function") {
|
if (typeof nodeHelper.stop === "function") {
|
||||||
nodeHelper.stop();
|
nodeHelper.stop();
|
||||||
}
|
}
|
||||||
@ -292,6 +280,6 @@ var App = function () {
|
|||||||
this.stop();
|
this.stop();
|
||||||
process.exit(0);
|
process.exit(0);
|
||||||
});
|
});
|
||||||
};
|
}
|
||||||
|
|
||||||
module.exports = new App();
|
module.exports = new App();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user