manager/updatetools.js
2023-04-29 23:23:37 +02:00

122 lines
3.4 KiB
JavaScript

const log = require("./sub-log")
const { BrowserWindow, app, ipcMain } = require("electron")
const https = require('https');
const fs = require('fs');
const fse = require('fs-extra');
const BWin = BrowserWindow
const path = require("path")
const decompress = require('decompress');
module.exports.checkUpdate = () => {
log.update("Verification des mises a jour ... Processing !")
fetch("https://git.raphix.fr/subsonics/manager/raw/branch/main/package.json").catch(err => catchError(err)).then(resp => resp.json()).then(resp => checkUpdateProcessing(resp))
}
function checkUpdateProcessing(serverPackage) {
const clientPackage = require("./package")
if(serverPackage.version.replace(".", "") > clientPackage.version.replace(".", "")) {
log.update("Verification des mises a jour ... Finish !")
log.update("Mise a jour disponible ! Nouvelle version : " + serverPackage.version + " - Client : " + clientPackage.version + " - Git : " + serverPackage.version)
update()
} else {
log.update("Verification des mises a jour ... Finish !")
log.update("Cette version est la derniere version ! - Client : " + clientPackage.version + " - Git : " + serverPackage.version)
}
}
function update() {
const win = BWin.getFocusedWindow()
win.loadFile("templates/update/update.html")
ipcMain.on("close", () => {
app.quit()
})
log.update("Telechargement de la mise a jour ... Processing !")
if(fs.existsSync(__dirname + path.sep + "update.tar.gz")) {
fs.rmSync(__dirname + path.sep + "update.tar.gz")
}
const updatePack = fs.createWriteStream(__dirname + path.sep + "update.tar.gz");
const request = https.get("https://git.raphix.fr/subsonics/manager/archive/main.tar.gz", function(response) {
response.pipe(updatePack);
// after download completed close filestream
updatePack.on("finish", () => {
updatePack.close();
log.update("Telechargement de la mise a jour ... Finish !")
install(__dirname + path.sep + "update.tar.gz")
});
updatePack.on("error", (err) => {
log.update("Erreur lors du telechargement de la mise a jour !")
updatePack.close();
console.log(err)
})
});
}
function install(filename) {
log.update("Extraction de la mise a jour ... Processing !")
decompress(filename, __dirname).then(files => {
log.update("Extraction de la mise a jour ... Finish !")
log.update("Installation de la mise a jour ... Processing !")
fse.copy(__dirname + path.sep + "manager", __dirname, { overwrite: true }, () => {
fs.rm(filename, () => {
fs.rm(__dirname + path.sep + "manager", { recursive: true, force: true }, (err) => {
if(err) {
console.log(err)
}
log.update("Installation de la mise a jour ... Finish !")
log.update("Redemarrage de l'application.")
app.relaunch()
app.exit()
})
})
})
}).catch(err => catchError(err, true));
}
function catchError(err, inUp) {
log.update("Erreur lors de la verification des mises a jour !")
log.update(err)
if(inUp == true) {
win.webContents.send("error")
}
}