Skip to content

Commit

Permalink
setting up auto-updates
Browse files Browse the repository at this point in the history
  • Loading branch information
tnrich committed Sep 19, 2019
1 parent 1ecfb41 commit 0cba703
Show file tree
Hide file tree
Showing 10 changed files with 4,186 additions and 5,350 deletions.
Binary file modified .DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GH_TOKEN="58af99321a881bcb4405857738aad12bb262db92"
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ This packages the open-vector-editor web app as an electron tool that can be use

# Developing
```
npm install
npm run start
yarn
yarn start
```

# Releasing
1. Bump the package.json version number

2. Build windows and mac
```
npm run dist
yarn dist
```
3. Go to https://github.com/tnrich/ove-electron/releases/new
4. Tag, title, describe and upload the .dmg and .exe files and create a new release!
49 changes: 47 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,57 @@
type="text/css"
href="node_modules/open-vector-editor/umd/main.css"
/>
<style>
#notification {
position: fixed;
bottom: 20px;
left: 20px;
width: 200px;
padding: 20px;
border-radius: 5px;
background-color: white;
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2);
}
.hidden {
display: none;
}
</style>
<div id="notification" class="hidden">
<p id="message"></p>
<button id="close-button" onClick="closeNotification()">
Close
</button>
<button id="restart-button" onClick="restartApp()" class="hidden">
Restart
</button>
</div>
<script
type="text/javascript"
src="node_modules/open-vector-editor/umd/open-vector-editor.js"
></script>
<script src="./src/renderer.js"></script>

<script>
const notification = document.getElementById("notification");
const message = document.getElementById("message");
const restartButton = document.getElementById("restart-button");
ipcRenderer.on("update_available", () => {
ipcRenderer.removeAllListeners("update_available");
message.innerText = "A new update is available. Downloading now...";
notification.classList.remove("hidden");
});
ipcRenderer.on("update_downloaded", () => {
ipcRenderer.removeAllListeners("update_downloaded");
message.innerText =
"Update Downloaded. It will be installed on restart. Restart now?";
restartButton.classList.remove("hidden");
notification.classList.remove("hidden");
});
function closeNotification() {
notification.classList.add("hidden");
}
function restartApp() {
ipcRenderer.send("restart_app");
}
</script>
</body>
</html>

135 changes: 106 additions & 29 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
/* eslint-disable no-console*/
// Modules to control application life and create native browser window
const { app, BrowserWindow } = require("electron");
const { app, BrowserWindow, ipcMain } = require("electron");
const path = require("path");
const bioParsers = require("bio-parsers");
const fs = require("fs");
const createMenu = require("./src/utils/menu");
const windowStateKeeper = require("electron-window-state");
let win;

const { autoUpdater } = require("electron-updater");

// ************************************************************************
// this function is super handy for debugging what is happening
// in the main process from the renderer process !!
// you'll need to comment it in in renderer.js file

// console.stdlog = console.log.bind(console);
// console.logs = [];
// console.log = function() {
// console.logs.push(Array.from(arguments));
// console.stdlog.apply(console, arguments);
// };
// ************************************************************************

let isAppReady = false;
let isMacOpenTriggered = false;
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let windows = [];
Expand All @@ -19,13 +35,25 @@ function getSeqJsonFromPath(_filePath) {
const data = fs.readFileSync(path.resolve(filePath), "utf-8");
//open, read, handle file
if (!data) return;
const fileName = filePath.replace(/^.*[\\\/]/, "");
const fileName = filePath.replace(/^.*[\\/]/, "");
return bioParsers.anyToJson(data, { fileName }).then(res => {
return res[0].parsedSequence;
});
}

function waitTillAppReady() {
return new Promise((resolve, reject) => {
const waitTillReadyInterval = setInterval(() => {
if (isAppReady) {
resolve();
clearInterval(waitTillReadyInterval);
}
}, 100);
});
}

async function createWindow(windowVars) {
await waitTillAppReady();
//if no windowVars are passed then we should
// Create the browser window.
let mainWindowState = windowStateKeeper({
Expand All @@ -38,12 +66,22 @@ async function createWindow(windowVars) {
y: mainWindowState.y,
width: mainWindowState.width,
height: mainWindowState.height,

show: false,
webPreferences: {
// nodeIntegration: true, //we don't want to enable this because it is a security risk and slows down the app
preload: path.join(__dirname, "src/preload.js")
}
});
console.log(`newWindow being created`);
newWindow.once("ready-to-show", () => {
newWindow.show();
});
const interval1 = setInterval(() => {
if (!newWindow) {
return clearInterval(interval1);
}
newWindow.logs = console.logs;
}, 100);

// Let us register listeners on the window, so we can update the state
// automatically (the listeners will be removed when the window is closed)
Expand Down Expand Up @@ -87,35 +125,59 @@ async function createWindow(windowVars) {
});
}

app.on("will-finish-launching", () => {
app.on("open-file", async (event, path) => {
//mac only
event.preventDefault();
try {
const initialSeqJson = await getSeqJsonFromPath(path);
createWindow({ initialSeqJson });
// startupWindowVars.initialSeqJson = initialSeqJson;
} catch (e) {
console.error(`e73562891230:`, e);
}
});
// app.on("open-files", async (event, path) => {
// //mac only
// event.preventDefault();
// try {
// const initialSeqJson = await getSeqJsonFromPath(path);
// startupWindowVars.initialSeqJson = initialSeqJson;
// } catch (e) {
// console.error(`e73562891230:`, e);
// }
// });
// let macOpenFilePaths: [];
// let runningTimeout = null;
// app.on("open-file", async (event, path) => {
// event.preventDefault();

// // Keep in array because more might come!
// macOpenFilePaths.push(path);

// // Clear previous handler if any
// if (runningTimeout !== null) {
// clearTimeout(runningTimeout);
// runningTimeout = null;
// }

// // Handle paths delayed in case more are coming!
// runningTimeout = setTimeout(async () => {
// if (this.windowsMainService) {
// try {
// const initialSeqJson = await getSeqJsonFromPath(path);
// createWindow({ initialSeqJson });
// // startupWindowVars.initialSeqJson = initialSeqJson;
// } catch (e) {
// console.error(`e73562891230:`, e);
// }

// macOpenFileURIs = [];
// runningTimeout = null;
// }
// }, 100);
// });

app.on("open-file", async (event, path) => {
isMacOpenTriggered = true;
//mac only
console.log(`open-file:`, path);
event.preventDefault();
try {
const initialSeqJson = await getSeqJsonFromPath(path);
console.log(`initialSeqJson:`, initialSeqJson);
createWindow({ initialSeqJson });
} catch (e) {
console.error(`e73562891230:`, e);
}
});

// 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", () => {
if (!windows.length) {
console.log(`onReady`);
autoUpdater.checkForUpdatesAndNotify();
isAppReady = true;
if (!windows.length && !isMacOpenTriggered) {
createWindow();
}
});
Expand All @@ -130,8 +192,23 @@ app.on("window-all-closed", function() {
app.on("activate", function() {
// 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 (!windows.length) createWindow();
if (!windows.length) {
console.log(`onActivate`);
createWindow();
}
});

autoUpdater.on("update-available", () => {
let browserWindows = BrowserWindow.getAllWindows();
browserWindows.forEach(win => win.webContents.send("update_available"));
});
autoUpdater.on("update-downloaded", () => {
let browserWindows = BrowserWindow.getFocusedWindow();
browserWindows.forEach(win => win.webContents.send("update_downloaded"));
});

ipcMain.on('restart_app', () => {
autoUpdater.quitAndInstall();
});
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
Loading

0 comments on commit 0cba703

Please sign in to comment.