2020-05-31 22:12:26 +02:00
|
|
|
const SimpleGit = require("simple-git");
|
|
|
|
const simpleGits = [];
|
|
|
|
const fs = require("fs");
|
|
|
|
const path = require("path");
|
|
|
|
const defaultModules = require(__dirname + "/../defaultmodules.js");
|
2021-02-18 19:14:53 +01:00
|
|
|
const Log = require("logger");
|
2020-05-31 22:12:26 +02:00
|
|
|
const NodeHelper = require("node_helper");
|
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
|
|
|
|
2020-05-11 22:22:32 +02:00
|
|
|
start: function () {},
|
2019-06-29 15:59:03 -05:00
|
|
|
|
2021-03-13 11:12:08 +01:00
|
|
|
configureModules: async function (modules) {
|
2019-06-29 15:59:03 -05:00
|
|
|
// Push MagicMirror itself , biggest chance it'll show up last in UI and isn't overwritten
|
2020-02-17 22:56:06 +01:00
|
|
|
// others will be added in front
|
|
|
|
// this method returns promises so we can't wait for every one to resolve before continuing
|
2021-03-13 11:58:33 +01:00
|
|
|
simpleGits.push({ module: "default", git: this.createGit(path.normalize(__dirname + "/../../../")) });
|
2019-06-29 15:59:03 -05:00
|
|
|
|
2021-03-13 11:12:08 +01:00
|
|
|
for (let moduleName in modules) {
|
2020-02-11 18:13:39 +01:00
|
|
|
if (!this.ignoreUpdateChecking(moduleName)) {
|
2016-11-18 12:53:49 +01:00
|
|
|
// Default modules are included in the main MagicMirror repo
|
2021-03-13 11:12:08 +01:00
|
|
|
let moduleFolder = path.normalize(__dirname + "/../../" + moduleName);
|
2016-11-18 12:53:49 +01:00
|
|
|
|
|
|
|
try {
|
2020-05-31 22:12:26 +02:00
|
|
|
Log.info("Checking git for module: " + moduleName);
|
2021-03-13 11:12:08 +01:00
|
|
|
// 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 });
|
2020-05-11 22:22:32 +02:00
|
|
|
} catch (err) {
|
2021-03-13 11:12:08 +01:00
|
|
|
// Error when directory .git doesn't exist or doesn't have any remotes
|
2016-11-18 12:53:49 +01:00
|
|
|
// This module is not managed with git, skip
|
|
|
|
continue;
|
|
|
|
}
|
2016-11-16 18:19:44 +01:00
|
|
|
}
|
2016-11-18 12:53:49 +01:00
|
|
|
}
|
2016-10-15 13:08:46 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
socketNotificationReceived: function (notification, payload) {
|
|
|
|
if (notification === "CONFIG") {
|
|
|
|
this.config = payload;
|
2020-05-11 22:22:32 +02:00
|
|
|
} else if (notification === "MODULES") {
|
2019-06-29 15:59:03 -05:00
|
|
|
// if this is the 1st time thru the update check process
|
2020-02-11 18:13:39 +01:00
|
|
|
if (!this.updateProcessStarted) {
|
|
|
|
this.updateProcessStarted = true;
|
2020-02-17 22:56:06 +01:00
|
|
|
this.configureModules(payload).then(() => this.performFetch());
|
2019-06-29 15:59:03 -05:00
|
|
|
}
|
2016-10-15 13:08:46 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2021-03-13 11:12:08 +01:00
|
|
|
resolveRemote: async function (moduleFolder) {
|
2021-03-13 11:58:33 +01:00
|
|
|
let git = this.createGit(moduleFolder);
|
2021-03-13 11:12:08 +01:00
|
|
|
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;
|
2020-02-17 22:56:06 +01:00
|
|
|
},
|
|
|
|
|
2021-03-13 11:12:08 +01:00
|
|
|
performFetch: async function () {
|
2021-03-13 12:02:56 +01:00
|
|
|
for (let sg of simpleGits) {
|
2021-03-13 11:12:08 +01:00
|
|
|
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
|
2019-07-01 12:35:47 -05:00
|
|
|
});
|
2016-11-16 18:19:44 +01:00
|
|
|
}
|
2021-03-13 11:12:08 +01:00
|
|
|
} catch (err) {
|
2021-03-13 11:58:33 +01:00
|
|
|
Log.error("Failed to fetch git data for " + sg.module + ": " + err);
|
2021-03-13 11:12:08 +01:00
|
|
|
}
|
|
|
|
}
|
2016-10-15 13:08:46 +02:00
|
|
|
|
|
|
|
this.scheduleNextFetch(this.config.updateInterval);
|
|
|
|
},
|
|
|
|
|
2020-05-11 22:22:32 +02:00
|
|
|
scheduleNextFetch: function (delay) {
|
2016-10-15 13:08:46 +02:00
|
|
|
if (delay < 60 * 1000) {
|
2019-07-01 12:35:47 -05:00
|
|
|
delay = 60 * 1000;
|
2016-10-15 13:08:46 +02:00
|
|
|
}
|
|
|
|
|
2021-03-13 11:12:08 +01:00
|
|
|
let self = this;
|
2016-10-15 13:08:46 +02:00
|
|
|
clearTimeout(this.updateTimer);
|
2020-05-11 22:22:32 +02:00
|
|
|
this.updateTimer = setTimeout(function () {
|
2020-02-11 18:13:39 +01:00
|
|
|
self.performFetch();
|
2016-10-15 13:08:46 +02:00
|
|
|
}, delay);
|
2020-02-11 18:13:39 +01:00
|
|
|
},
|
|
|
|
|
2021-03-13 11:58:33 +01:00
|
|
|
createGit: function (folder) {
|
|
|
|
return SimpleGit({ baseDir: folder, timeout: { block: this.config.timeout } });
|
|
|
|
},
|
|
|
|
|
2020-05-11 22:22:32 +02:00
|
|
|
ignoreUpdateChecking: function (moduleName) {
|
2020-02-11 18:13:39 +01:00
|
|
|
// Should not check for updates for default modules
|
|
|
|
if (defaultModules.indexOf(moduleName) >= 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should not check for updates for ignored modules
|
|
|
|
if (this.config.ignoreModules.indexOf(moduleName) >= 0) {
|
|
|
|
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
|
|
|
});
|