From 3ed223a550570e4279b23de005435fc20f1bcc3a Mon Sep 17 00:00:00 2001 From: buxxi Date: Sat, 13 Mar 2021 11:12:08 +0100 Subject: [PATCH] Refactoring update notification to use async/await --- .../default/updatenotification/node_helper.js | 74 +++++++++---------- 1 file changed, 37 insertions(+), 37 deletions(-) diff --git a/modules/default/updatenotification/node_helper.js b/modules/default/updatenotification/node_helper.js index a8b75c3e..fad519a5 100644 --- a/modules/default/updatenotification/node_helper.js +++ b/modules/default/updatenotification/node_helper.js @@ -14,32 +14,32 @@ module.exports = NodeHelper.create({ start: function () {}, - configureModules: function (modules) { + configureModules: async function (modules) { // Push MagicMirror itself , biggest chance it'll show up last in UI and isn't overwritten // others will be added in front // this method returns promises so we can't wait for every one to resolve before continuing simpleGits.push({ module: "default", git: SimpleGit(path.normalize(__dirname + "/../../../")) }); - var promises = []; - - for (var moduleName in modules) { + for (let moduleName in modules) { if (!this.ignoreUpdateChecking(moduleName)) { // Default modules are included in the main MagicMirror repo - var moduleFolder = path.normalize(__dirname + "/../../" + moduleName); + let moduleFolder = path.normalize(__dirname + "/../../" + moduleName); try { Log.info("Checking git for module: " + moduleName); - let stat = fs.statSync(path.join(moduleFolder, ".git")); - promises.push(this.resolveRemote(moduleName, moduleFolder)); + // Throws error if file doesn't exist + fs.statSync(path.join(moduleFolder, ".git")); + // Fetch the git or throw error if no remotes + let git = await this.resolveRemote(moduleFolder); + // Folder has .git and has at least one git remote, watch this folder + simpleGits.unshift({ module: moduleName, git: git }); } catch (err) { - // Error when directory .git doesn't exist + // Error when directory .git doesn't exist or doesn't have any remotes // This module is not managed with git, skip continue; } } } - - return Promise.all(promises); }, socketNotificationReceived: function (notification, payload) { @@ -54,36 +54,36 @@ module.exports = NodeHelper.create({ } }, - resolveRemote: function (moduleName, moduleFolder) { - return new Promise((resolve, reject) => { - var git = SimpleGit(moduleFolder); - git.getRemotes(true, (err, remotes) => { - if (remotes.length < 1 || remotes[0].name.length < 1) { - // No valid remote for folder, skip - return resolve(); - } - // Folder has .git and has at least one git remote, watch this folder - simpleGits.unshift({ module: moduleName, git: git }); - resolve(); - }); - }); + resolveRemote: async function (moduleFolder) { + let git = SimpleGit(moduleFolder); + let remotes = await git.getRemotes(true); + + if (remotes.length < 1 || remotes[0].name.length < 1) { + throw new Error("No valid remote for folder " + moduleFolder); + } + + return git; }, - performFetch: function () { - var self = this; - simpleGits.forEach((sg) => { - sg.git.fetch(["--dry-run"]).status((err, data) => { - data.module = sg.module; - if (!err) { - sg.git.log({ "-1": null }, (err, data2) => { - if (!err && data2.latest && "hash" in data2.latest) { - data.hash = data2.latest.hash; - self.sendSocketNotification("STATUS", data); - } + performFetch: async function () { + for (sg of simpleGits) { + try { + let fetchData = await sg.git.fetch(["--dry-run"]).status(); + let logData = await sg.git.log({ "-1": null }); + + if (logData.latest && "hash" in logData.latest) { + this.sendSocketNotification("STATUS", { + module: sg.module, + behind: fetchData.behind, + current: fetchData.current, + hash: logData.latest.hash, + tracking: fetchData.tracking }); } - }); - }); + } catch (err) { + Log.error("Failed to fetch data for git: " + err); + } + } this.scheduleNextFetch(this.config.updateInterval); }, @@ -93,7 +93,7 @@ module.exports = NodeHelper.create({ delay = 60 * 1000; } - var self = this; + let self = this; clearTimeout(this.updateTimer); this.updateTimer = setTimeout(function () { self.performFetch();