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

Add self-updater #12

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 46 additions & 1 deletion electron.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { app, BrowserWindow, Menu, protocol, shell } = require('electron');
const { app, BrowserWindow, ipcMain, Menu, protocol, shell } = require('electron');
const { autoUpdater } = require("electron-updater");
const path = require('path');

if (process.env.NODE_ENV === 'development') {
Expand Down Expand Up @@ -84,6 +85,49 @@ function createAboutWindow() {
})
}

function sendEvent( event, data) {
if ( aboutWindow ) {
aboutWindow.webContents.send( event, data );
}
if ( win ) {
win.webContents.send( event, data );
}
}

function configureAutoupdates() {
const sendStatusToWindow = data => sendEvent( 'update-status', data );

// Configure the updater.
autoUpdater.allowPrerelease = true;

// Listen to events.
autoUpdater.on( 'checking-for-update', () => {
sendStatusToWindow( { type: 'checking-for-update' } )
})
autoUpdater.on( 'update-available', info => {
sendStatusToWindow( { type: 'update-available', info } )
})
autoUpdater.on( 'update-not-available', (ev, info) => {
console.log( ev, info );
sendStatusToWindow( { type: 'update-not-available', info } )
})
autoUpdater.on( 'error', (ev, err) => {
sendStatusToWindow( { type: 'error', err } )
})
autoUpdater.on( 'update-downloaded', (ev, info) => {
sendStatusToWindow( { type: 'update-downloaded', info } )
})
// Not supported on Mac:
// https://github.com/electron-userland/electron-builder/issues/1167
// https://github.com/Squirrel/Squirrel.Mac/issues/200
autoUpdater.on( 'download-progress', (ev, progressObj) => {
sendStatusToWindow( { type: 'download-progress', progressObj } )
})

ipcMain.on( 'check-update', () => autoUpdater.checkForUpdates() )
ipcMain.on( 'install-update', () => autoUpdater.quitAndInstall() )
}

// 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.
Expand All @@ -103,6 +147,7 @@ app.on('ready', () => {
);

createWindow();
configureAutoupdates();

const mainMenu = Menu.buildFromTemplate([
{
Expand Down
13 changes: 11 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
"description": "Manage local WordPress development environments through a handy UI.",
"private": true,
"main": "electron.js",
"dependencies": {
"electron-updater": "^1.16.0"
},
"devDependencies": {
"ansi-html": "0.0.6",
"autoprefixer": "6.4.1",
Expand All @@ -23,7 +26,7 @@
"devtron": "^1.4.0",
"dotenv": "2.0.0",
"electron": "^1.6.8",
"electron-builder": "^11.7.0",
"electron-builder": "^18.0.1",
"electron-debug": "^1.1.0",
"eslint": "3.5.0",
"eslint-config-react-app": "^0.2.1",
Expand Down Expand Up @@ -118,7 +121,8 @@
},
"files": [
"build/**/*",
"electron.js"
"electron.js",
"node_modules/**/*"
],
"directories": {
"buildResources": "resources"
Expand All @@ -129,6 +133,11 @@
"schemes": [
"chassis"
]
},
"artifactName": "chassis-desktop-${version}.${ext}",
"publish": {
"provider": "github",
"vPrefixedTagName": false
}
}
}
3 changes: 0 additions & 3 deletions src/About.css
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ html {
margin-top: 0;
margin-bottom: 0.75em;
}
.About .update .fa-spinner {
animation: Splash-loader infinite 2s linear;
}
.About .logo {
width: 200px;
padding: 1rem;
Expand Down
2 changes: 2 additions & 0 deletions src/About.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';

import Updater from './About/Updater';
import packageData from '../package.json';

import './About.css';
Expand All @@ -18,6 +19,7 @@ export default class About extends React.Component {
{' '}
<a href="https://github.com/Chassis/Desktop/releases">View releases</a>
</p>
<Updater />
<p className="credits">
Chassis is produced by {' '}
<a href="https://bronsonquick.com.au/">Bronson Quick</a>, {' '}
Expand Down
3 changes: 3 additions & 0 deletions src/About/Updater.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.Updater .fa-spinner {
animation: Splash-loader infinite 2s linear;
}
84 changes: 84 additions & 0 deletions src/About/Updater.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { ipcRenderer } from 'electron';
import React from 'react';

import Icon from '../Icon';

import './Updater.css';

export default class Updater extends React.Component {
constructor(props) {
super(props);

this.state = {
status: null,
available: null,
};
this.listener = (e, data) => {
switch ( data.type ) {
case 'checking-for-update':
case 'update-not-available':
case 'update-downloaded':
this.setState({ status: data.type });
break;

case 'update-available':
this.setState({
status: data.type,
available: data.info,
});
break;

default:
console.log( 'unknown', data );
break;
}
}
ipcRenderer.on( 'update-status', this.listener );
}

componentWillUnmount() {
ipcRenderer.removeListener( 'update-status', this.listener );
}

onCheck() {
ipcRenderer.send( 'check-update' );
}

onInstall() {
ipcRenderer.send( 'install-update' );
}

render() {
const { available, status } = this.state;

switch ( status ) {
case 'checking-for-update':
return <p className="Updater">
<Icon type="spinner" /> Checking for updates&hellip;
</p>;

case 'update-available':
return <p className="Updater">
<Icon type="spinner" /> Downloading { available.version }&hellip;
</p>;

case 'update-not-available':
return <p className="Updater">
You're up to date!
</p>

case 'update-downloaded':
return <p className="Updater" onClick={() => this.onInstall()}>
<button type="button">Restart Desktop to Update</button>
</p>;

default:
return <p className="Updater">
<button type="button" onClick={ () => this.onCheck() }>
Check for Updates
</button>
</p>;
}

}
}