Skip to content

Commit

Permalink
Enable In-App Update (#868)
Browse files Browse the repository at this point in the history
* updated update notifier

* added about screen

* cleanup + start working on update screen

* error handling

* automatically retrieve download url for arch

* download working

* download and installation working

* finished and tested

* codefactor
  • Loading branch information
UnchartedBull committed Jul 31, 2020
1 parent 73c2040 commit dcb0033
Show file tree
Hide file tree
Showing 20 changed files with 958 additions and 197 deletions.
109 changes: 103 additions & 6 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
/* eslint-disable no-undef */
/* eslint-disable @typescript-eslint/no-var-requires */
const { app, BrowserWindow, ipcMain } = require("electron");
const url = require("url");
const path = require("path");
const electronStore = require("electron-store");
const fs = require("fs");
const Store = require("electron-store");
const got = require('got');
const path = require("path");
const url = require("url");
const stream = require('stream');
const {promisify} = require('util');
const progress = require('progress-stream');

const store = new Store();
const exec = require("child_process").exec;

const store = new electronStore();

const args = process.argv.slice(1);
const dev = args.some((val) => val === "--serve");
const big = args.some((val) => val === "--big");
const dev = args.some((val) => val === "--serve");

app.commandLine.appendSwitch("touch-events", "enabled");
app.allowRendererProcessReuse = true;
Expand Down Expand Up @@ -68,10 +73,10 @@ function createWindow() {
window.setFullScreen(true);
}

// setTimeout(sendVersionInfo, 30 * 1000);
activateAppInfoListener();
activateScreenSleepListener();
activateReloadListener();
activateUpdateListener();

window.on("closed", () => {
window = null;
Expand Down Expand Up @@ -109,6 +114,12 @@ function activateAppInfoListener() {
});
}

function activateUpdateListener() {
ipcMain.on("update", (_, updateInfo) => {
downloadUpdate(updateInfo);
});
}

function sendCustomStyles() {
fs.readFile(path.join(app.getPath("userData"), "custom-styles.css"), "utf-8", (err, data) => {
if (err) {
Expand All @@ -135,6 +146,92 @@ function sendVersionInfo() {
});
}

function downloadUpdate(updateInfo) {
const downloadPath = "/tmp/octodash.deb";

exec("arch", (err, stdout, stderr) => {
if (err || stderr) {
window.webContents.send("updateError", {
error: err ? err : { message: stderr },
});
}
got(updateInfo.assetsURL)
.then((releaseFiles) => {
const reducer = (accumulator, currentValue) => accumulator + currentValue;
let averageETA = [];
let downloadURL;
let packageSize;
for (let package of JSON.parse(releaseFiles.body)) {
if (package.name.includes(stdout.trim())) {
downloadURL = package.browser_download_url;
packageSize = package.size;
}
}
if (downloadURL) {
const downloadPipeline = promisify(stream.pipeline);
let downloadProgress = progress({
length: packageSize,
time: 300,
});

downloadProgress.on('progress', (progress) => {
averageETA.push(progress.eta);
if (averageETA.length > 4) averageETA.shift();
window.webContents.send("updateDownloadProgress", {
percentage: progress.percentage,
transferred: (progress.transferred / 100000).toFixed(1),
total: (progress.length / 1000000).toFixed(1),
remaining: (progress.remaining / 100000).toFixed(1),
eta: new Date(averageETA.reduce(reducer) * 1000).toISOString().substr(14, 5),
runtime: new Date(progress.runtime * 1000).toISOString().substr(14, 5),
delta: (progress.delta / 100000).toFixed(1),
speed: (progress.speed / 1000000).toFixed(2),
})
})

try {
if (fs.existsSync(downloadPath)) fs.unlinkSync(downloadPath)
} catch {
// no need to handle this properly
}

downloadPipeline(
got.stream(downloadURL),
downloadProgress,
fs.createWriteStream(downloadPath)
).catch((error) => {
window.webContents.send("updateError", {
error: {
message: `Can't download package! ${error.message}.`
}
})
}).then(() => {
window.webContents.send("updateDownloadFinished");
exec('sudo ~/scripts/update-octodash', (err, _, stderr) => {
if (err || stderr) {
window.webContents.send("updateError", {
error: err ? err : { message: stderr },
});
} else {
window.webContents.send("updateInstalled");
}
})
})
} else {
window.webContents.send("updateError", {
error: {
message: `Can't find matching package for architecture ${stdout}.`
}
})
}
})
.catch((error) => {
error.message = `Can't load releases. ${error.message}`;
window.webContents.send("updateError", {error});
})
});
}

app.on("ready", createWindow);

app.on("window-all-closed", () => {
Expand Down
Loading

0 comments on commit dcb0033

Please sign in to comment.