Skip to content

Commit

Permalink
Auto updates (#1)
Browse files Browse the repository at this point in the history
* Added automatic updates support
* Changed Beatsaber install directory check
  • Loading branch information
doughtnerd authored Jan 14, 2020
1 parent 89d23e4 commit 72ac6ca
Show file tree
Hide file tree
Showing 10 changed files with 694 additions and 4,543 deletions.
5,041 changes: 533 additions & 4,508 deletions package-lock.json

Large diffs are not rendered by default.

39 changes: 21 additions & 18 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
{
"name": "beathub",
"version": "1.0.0",
"description": "Beat Saber custom song browser and downloader",
"author": {
"name": "Christopher Carlson",
"email": "doughtnerd@gmail.com",
"url": "https://github.com/Doughtnerd"
},
"version": "1.1.1-beta",
"main": "./src/electron.js",
"license": "(MIT OR Apache-2.0)",
"repository": {
Expand All @@ -10,9 +16,8 @@
"devDependencies": {
"@rollup/plugin-node-resolve": "~6.0.0",
"chokidar": "^3.2.1",
"electron": "^6.0.12",
"electron": "~7.1.8",
"electron-builder": "^21.2.0",
"electron-prebuilt-compile": "4.0.0",
"node-sass": "^4.13.0",
"npm-run-all": "^4.1.5",
"rollup": "^1.12.0",
Expand All @@ -32,31 +37,29 @@
"dependencies": {
"electron-dl": "~2.0.0",
"electron-json-storage": "~4.1.8",
"electron-log": "^4.0.3",
"electron-store": "~5.1.0",
"electron-updater": "^4.2.0",
"jszip": "~3.2.2",
"svelte-awesome": "~2.2.1",
"svelte-infinite-scroll": "~0.1.0"
},
"scripts": {
"build": "rollup -c",
"build:windows": "electron-builder .",
"autobuild": "rollup -c -w",
"dev": "run-p start:dev autobuild",
"start": "sirv public --single",
"start:dev": "sirv public --port=3000 --single --dev",
"electron": "run-s build pure-electron",
"build": "electron-builder .",
"electron-dev": "run-p autobuild pure-electron-dev",
"windows:electron-dev": "run-p autobuild windows:pure-electron-dev",
"dist-darwin": "run-s build pure-dist-darwin",
"pure-electron": "electron .",
"pure-electron-dev": "NODE_ENV=development electron --inspect=3000 .",
"windows:pure-electron-dev": "set NODE_ENV=development && electron --inspect=3000 .",
"pure-dist-darwin": "electron-builder --dir --mac --config electron-builder.yml"
"windows:pure-electron-dev": "set NODE_ENV=development&&set ROLLUP_WATCH=true&&electron --inspect=3000 .",
"publish": "electron-builder --win -p always"
},
"build": {
"win": {
"publish": ["github"]
}
},
"release": "build"
"publish": [
{
"provider": "github",
"owner": "doughtnerd",
"repo": "beathub"
}
]
}
}
22 changes: 11 additions & 11 deletions src/electron.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
const { app, BrowserWindow, ipcMain } = require("electron");
const { app, BrowserWindow, Menu, protocol, ipcMain } = require("electron");
const path = require("path");
const log = require("electron-log");
const autoUpdater = require("./main/autoUpdate").register();

let mainWindow;

log.info("App starting...");

function createWindow() {
const mode = process.env.NODE_ENV;
mainWindow = new BrowserWindow({
Expand All @@ -17,7 +21,8 @@ function createWindow() {
mainWindow.setMenu(null);

let watcher;
if (process.env.NODE_ENV === "development") {
console.log(mode, mode == "development")
if (mode == "development") {
watcher = require("chokidar").watch(
path.join(__dirname, "../public/bundle.js"),
{ ignoreInitial: true }
Expand All @@ -36,23 +41,18 @@ function createWindow() {
});
}

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on("ready", createWindow);
app.on("ready", () => {
createWindow();
autoUpdater.checkForUpdatesAndNotify();
});

// Quit when all windows are closed.
app.on("window-all-closed", () => {
// On macOS it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== "darwin") {
app.quit();
}
});

app.on("activate", () => {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (mainWindow === null) {
createWindow();
}
Expand Down
56 changes: 56 additions & 0 deletions src/main/autoUpdate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
const log = require("electron-log");
const { autoUpdater } = require("electron-updater");
const { BrowserWindow, ipcMain } = require("electron");

function register() {
autoUpdater.logger = log;
autoUpdater.logger.transports.file.level = "info";

const sendStatusToWindow = (channel, text) => {
log.info(`${channel}: ${text}`);

BrowserWindow.getFocusedWindow().webContents.send(channel, text);
};

autoUpdater.on("checking-for-update", () => {
sendStatusToWindow("checkingForUpdate", "Checking for update...");
});

autoUpdater.on("update-available", info => {
sendStatusToWindow("updateAvailable", "Update available.");
});

autoUpdater.on("update-not-available", info => {
sendStatusToWindow("updateNotAvailable", "Update not available.");
});

autoUpdater.on("error", err => {
sendStatusToWindow("updateError", "Error in auto-updater. " + err);
});

autoUpdater.on("download-progress", progressObj => {
let log_message = "Download speed: " + progressObj.bytesPerSecond;
log_message = log_message + " - Downloaded " + progressObj.percent + "%";
log_message =
log_message +
" (" +
progressObj.transferred +
"/" +
progressObj.total +
")";
sendStatusToWindow("updateProgress", log_message);
});

autoUpdater.on("update-downloaded", info => {
sendStatusToWindow("updateDownloaded", "Update downloaded");
});


ipcMain.handle("restartAndUpdate", () => {
autoUpdater.quitAndInstall(true, true);
});

return autoUpdater;
}

module.exports = { register };
2 changes: 2 additions & 0 deletions src/render/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import Settings from "./pages/Settings.svelte";
import AudioPlayer from "./components/AudioPlayer.svelte";
import Badge from "./components/Badge.svelte";
import UpdateReadyDialog from "./pages/UpdateReadyDialog.svelte";
import { downloads } from "./stores/downloads.store";
$: numberOfDownloads = Object.keys($downloads.downloading).length;
Expand Down Expand Up @@ -192,3 +193,4 @@
<AudioPlayer />
</footer>
</div>
<UpdateReadyDialog />
6 changes: 3 additions & 3 deletions src/render/components/Dialog.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
}
.backdrop {
background-color: rgba(0, 0, 0, 0.5);
background-color: rgba(0, 0, 0, 0.75);
}
.dialog {
background-color: white;
background-color: var(--foreground);
padding: 16px;
box-shadow: 0 0 5px 5px rgba(0, 0, 0, 0.15);
box-shadow: 0 0 5px 5px rgba(0, 0, 0, 0.25);
width: 400px;
height: 200px;
Expand Down
4 changes: 3 additions & 1 deletion src/render/pages/Settings.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script>
import PrimaryText from "../components/PrimaryText.svelte";
import { downloads } from "../stores/downloads.store";
const appVersion = require('electron').remote.app.getVersion();
function handleOnChange(event) {
if (event.target.files[0] && event.target.files[0].path) {
downloads.changeDownloadDirectory(event.target.files[0].path);
Expand Down Expand Up @@ -46,6 +46,8 @@
on:change={handleOnChange} />

<div class="container">
<PrimaryText><h1>App Version</h1></PrimaryText>
<div><span>{appVersion}</span></div>
<PrimaryText>
<h1>Beat Saber Install Location</h1>
</PrimaryText>
Expand Down
34 changes: 34 additions & 0 deletions src/render/pages/UpdateReadyDialog.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<script>
import Dialog from "../components/Dialog.svelte";
import { createEventDispatcher } from "svelte";
import { updateDialogStore } from "../stores/update-ready-dialog.store";
function handleYesClicked() {
updateDialogStore.updateAndRestart();
}
function handleNoClicked() {
updateDialogStore.close();
}
</script>

<style>
.dialog-content {
color: var(--foregroundText);
}
</style>

{#if $updateDialogStore.isOpen}
<Dialog>
<div class="dialog-content" slot="dialog-content">
<h1>Update Ready</h1>
<div>
<p>An update is ready. Do you want to restart and update?</p>
</div>
</div>
<div slot="dialog-actions">
<button on:click={handleYesClicked}>Yes</button>
<button on:click={handleNoClicked}>No</button>
</div>
</Dialog>
{/if}
4 changes: 2 additions & 2 deletions src/render/stores/downloads.store.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const { remote } = window.require("electron");
const fs = remote.require("fs");

const DEFAULT_WINDOWS_STEAM_LOCATION =
"C:/Program Files (x86)/Steam/steamapps/common/Beat Saber/Beat Saber_Data/CustomLevels";
"C:/Program Files (x86)/Steam/steamapps/common/Beat Saber";
const DEFAULT_WINDOWS_OCULUS_LOCATION =
"C:/Program Files/Oculus/Software/Software/hyperbolic-magnetism-beat-saber";

Expand All @@ -21,7 +21,7 @@ async function downloadBeatmap(beatmap, downloadDirectory) {
Object.keys(zip.files).forEach(async filename => {
const content = await zip.file(filename).async("nodebuffer");
const songFolderName = formatFolderName(beatmap);
const destFolder = `${downloadDirectory}/${songFolderName}/`;
const destFolder = `${downloadDirectory}/Beat Saber_Data/CustomLevels/${songFolderName}/`;

if (!fs.existsSync(destFolder)) {
fs.mkdirSync(destFolder, { recursive: true });
Expand Down
29 changes: 29 additions & 0 deletions src/render/stores/update-ready-dialog.store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { writable } from "svelte/store";

const electron = require("electron");
const { ipcRenderer } = electron;

function createUpdateDialogStore() {
const store = writable({
isOpen: false
});

ipcRenderer.on("updateDownloaded", () => {
store.set({ isOpen: true });
});

return {
subscribe: store.subscribe,
open: () => {
store.set({ isOpen: true });
},
close: () => {
store.set({ isOpen: false });
},
updateAndRestart: () => {
ipcRenderer.invoke("restartAndUpdate");
}
};
}

export const updateDialogStore = createUpdateDialogStore();

0 comments on commit 72ac6ca

Please sign in to comment.