Files

122 lines
3.0 KiB
JavaScript
Raw Permalink Normal View History

2025-04-01 20:11:02 +02:00
const fs = require("node:fs");
const path = require("node:path");
2020-05-31 22:12:26 +02:00
const NodeHelper = require("node_helper");
2025-09-30 18:02:22 +02:00
2026-04-01 12:35:16 +02:00
const defaultModules = require(`${global.root_path}/${global.defaultModulesDir}/defaultmodules`);
2023-04-04 20:44:32 +02:00
const GitHelper = require("./git_helper");
2024-01-01 15:38:08 +01:00
const UpdateHelper = require("./update_helper");
2016-10-15 13:08:46 +02:00
const ONE_MINUTE = 60 * 1000;
2016-10-15 13:08:46 +02:00
module.exports = NodeHelper.create({
config: {},
updateTimer: null,
2019-06-29 15:59:03 -05:00
updateProcessStarted: false,
2016-10-15 13:08:46 +02:00
gitHelper: new GitHelper(),
2024-01-01 15:38:08 +01:00
updateHelper: null,
2019-06-29 15:59:03 -05:00
2025-04-01 20:11:02 +02:00
getModules (modules) {
if (this.config.useModulesFromConfig) {
return modules;
} else {
// get modules from modules-directory
2025-09-30 18:02:22 +02:00
const moduleDir = path.normalize(`${global.root_path}/modules`);
2025-04-01 20:11:02 +02:00
const getDirectories = (source) => {
return fs.readdirSync(source, { withFileTypes: true })
.filter((dirent) => dirent.isDirectory() && dirent.name !== "default")
.map((dirent) => dirent.name);
};
return getDirectories(moduleDir);
}
},
2024-01-01 15:38:08 +01:00
async configureModules (modules) {
2025-04-01 20:11:02 +02:00
for (const moduleName of this.getModules(modules)) {
if (!this.ignoreUpdateChecking(moduleName)) {
await this.gitHelper.add(moduleName);
2016-11-16 18:19:44 +01:00
}
2016-11-18 12:53:49 +01:00
}
2023-04-04 20:44:32 +02:00
if (!this.ignoreUpdateChecking("MagicMirror")) {
await this.gitHelper.add("MagicMirror");
}
2016-10-15 13:08:46 +02:00
},
2024-01-01 15:38:08 +01:00
async socketNotificationReceived (notification, payload) {
2023-07-01 21:17:31 +02:00
switch (notification) {
case "CONFIG":
this.config = payload;
2024-01-01 15:38:08 +01:00
this.updateHelper = new UpdateHelper(this.config);
2023-07-01 21:17:31 +02:00
break;
case "MODULES":
// if this is the 1st time thru the update check process
if (!this.updateProcessStarted) {
this.updateProcessStarted = true;
await this.configureModules(payload);
await this.performFetch();
}
break;
case "SCAN_UPDATES":
// 1st time of check allows to force new scan
if (this.updateProcessStarted) {
clearTimeout(this.updateTimer);
await this.performFetch();
}
break;
2016-10-15 13:08:46 +02:00
}
},
2024-01-01 15:38:08 +01:00
async performFetch () {
const repos = await this.gitHelper.getRepos();
for (const repo of repos) {
2024-01-01 15:38:08 +01:00
this.sendSocketNotification("REPO_STATUS", repo);
}
2016-10-15 13:08:46 +02:00
2024-01-01 15:38:08 +01:00
const updates = await this.gitHelper.checkUpdates();
if (this.config.sendUpdatesNotifications && updates.length) {
this.sendSocketNotification("UPDATES", updates);
}
if (updates.length) {
const updateResult = await this.updateHelper.parse(updates);
for (const update of updateResult) {
if (update.inProgress) {
this.sendSocketNotification("UPDATE_STATUS", update);
}
}
2023-07-01 21:17:31 +02:00
}
2016-10-15 13:08:46 +02:00
this.scheduleNextFetch(this.config.updateInterval);
},
2024-01-01 15:38:08 +01:00
scheduleNextFetch (delay) {
2016-10-15 13:08:46 +02:00
clearTimeout(this.updateTimer);
2023-10-01 20:13:41 +02:00
this.updateTimer = setTimeout(
() => {
this.performFetch();
},
Math.max(delay, ONE_MINUTE)
);
},
2024-01-01 15:38:08 +01:00
ignoreUpdateChecking (moduleName) {
// Should not check for updates for default modules
if (defaultModules.includes(moduleName)) {
return true;
}
// Should not check for updates for ignored modules
if (this.config.ignoreModules.includes(moduleName)) {
return true;
}
// The rest of the modules that passes should check for updates
return false;
2016-10-15 13:08:46 +02:00
}
2016-10-17 17:03:10 +02:00
});