117 lines
3.2 KiB
JavaScript
Raw Normal View History

2016-11-16 18:19:44 +01:00
var SimpleGit = require("simple-git");
var simpleGits = [];
var fs = require("fs");
var path = require("path");
var defaultModules = require(__dirname + "/../defaultmodules.js");
2016-10-15 13:08:46 +02:00
var NodeHelper = require("node_helper");
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
start: function () {},
2019-06-29 15:59:03 -05:00
configureModules: 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
// 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 + "/../../../")) });
2019-06-29 15:59:03 -05:00
var promises = [];
2020-05-02 10:39:09 +02:00
for (var moduleName in modules) {
if (!this.ignoreUpdateChecking(moduleName)) {
// Default modules are included in the main MagicMirror repo
var moduleFolder = path.normalize(__dirname + "/../../" + moduleName);
try {
2019-06-29 15:59:03 -05:00
//console.log("checking git for module="+moduleName)
let stat = fs.statSync(path.join(moduleFolder, ".git"));
promises.push(this.resolveRemote(moduleName, moduleFolder));
} catch (err) {
// Error when directory .git doesn't exist
// This module is not managed with git, skip
continue;
}
2016-11-16 18:19:44 +01:00
}
}
return Promise.all(promises);
2016-10-15 13:08:46 +02:00
},
socketNotificationReceived: function (notification, payload) {
if (notification === "CONFIG") {
this.config = payload;
} else if (notification === "MODULES") {
2019-06-29 15:59:03 -05:00
// if this is the 1st time thru the update check process
if (!this.updateProcessStarted) {
this.updateProcessStarted = true;
this.configureModules(payload).then(() => this.performFetch());
2019-06-29 15:59:03 -05:00
}
2016-10-15 13:08:46 +02:00
}
},
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();
});
});
},
performFetch: function () {
var self = this;
simpleGits.forEach((sg) => {
sg.git.fetch().status((err, data) => {
2016-11-16 18:19:44 +01:00
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);
}
});
2016-11-16 18:19:44 +01:00
}
});
2016-10-15 13:08:46 +02:00
});
this.scheduleNextFetch(this.config.updateInterval);
},
scheduleNextFetch: function (delay) {
2016-10-15 13:08:46 +02:00
if (delay < 60 * 1000) {
delay = 60 * 1000;
2016-10-15 13:08:46 +02:00
}
var self = this;
clearTimeout(this.updateTimer);
this.updateTimer = setTimeout(function () {
self.performFetch();
2016-10-15 13:08:46 +02:00
}, delay);
},
ignoreUpdateChecking: function (moduleName) {
// 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
});