Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable In-App Update #868

Merged
merged 11 commits into from
Jul 31, 2020
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