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

Improve auto updater #869

Merged
merged 25 commits into from
Nov 12, 2018
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ release/
*.tgz
.idea
.DS_Store
dev-app-update.yml
88 changes: 49 additions & 39 deletions desktop/updater/auto-updater/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,58 +5,68 @@
*/
const EventEmitter = require('events').EventEmitter;
const util = require('util');
const electron = require( 'electron' );
const autoUpdater = electron.autoUpdater;
const dialog = electron.dialog;
const { dialog } = require('electron');
const { autoUpdater } = require('electron-updater');
const sanitizeHtml = require('sanitize-html');
const TurndownService = require('turndown');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wondered if we could do the HTML to MD conversion with showdown (the MD converter we're already using), but it looks like the feature hasn't shipped yet 🤷‍♀️ We'll replace it when it's released.


const config = require('../../config');

/**
* Internal dependencies
*/
const AppQuit = require( '../..//app-quit' );

function AutoUpdater( url ) {
this.hasPrompted = false;
this.url = url;
const AppQuit = require('../../app-quit');

autoUpdater.on( 'error', () => this.emit( 'end' ) );
autoUpdater.on( 'update-not-available', () => this.emit( 'end' ) );
autoUpdater.on( 'update-downloaded', this.onDownloaded.bind( this ) );
function AutoUpdater() {
this.hasPrompted = false;

try {
autoUpdater.setFeedURL( url );
} catch (e) {
console.log( e.message );
}
autoUpdater.on('error', () => this.emit('end'));
autoUpdater.on('update-not-available', () => this.emit('end'));
autoUpdater.on('update-downloaded', this.onDownloaded.bind(this));
}

util.inherits(AutoUpdater, EventEmitter);

AutoUpdater.prototype.ping = function() {
autoUpdater.checkForUpdates();
autoUpdater.checkForUpdates();
};

AutoUpdater.prototype.onDownloaded = function(info) {
const releaseNotes = this.formatGithubReleaseNotes(info.releaseNotes);

const updateDialogOptions = {
buttons: ['Update & Restart', 'Cancel'],
title: 'Update Available',
message: `${config.name} ${info.releaseName}`,
detail: releaseNotes,
};

if (this.hasPrompted === false) {
this.hasPrompted = true;

dialog.showMessageBox(updateDialogOptions, button => {
this.hasPrompted = false;

if (button === 0) {
AppQuit.allowQuit();
autoUpdater.quitAndInstall();
} else {
this.emit('end');
}
});
}
};

AutoUpdater.prototype.onDownloaded = function( event, releaseNotes, releaseName, releaseDate, updateUrl, quitAndUpdate ) {
const updateDialogOptions = {
buttons: [ 'Update & Restart', 'Cancel' ],
title: 'Update Available',
message: releaseName,
detail: releaseNotes
};

if ( this.hasPrompted === false ) {
this.hasPrompted = true;

dialog.showMessageBox( updateDialogOptions, button => {
this.hasPrompted = false;

if ( button === 0 ) {
AppQuit.allowQuit();
quitAndUpdate();
} else {
this.emit( 'end' );
}
} );
}
AutoUpdater.prototype.formatGithubReleaseNotes = function(releaseNotes) {
const turndownService = new TurndownService();

let sanitizedHtml = releaseNotes;
sanitizedHtml = sanitizeHtml(releaseNotes, {
allowedTags: ['p', 'ul', 'ol', 'li'],
allowedAttributes: [],
}).replace(/\(@(.*)\)/g, ''); // remove user mentions

return turndownService.turndown(sanitizedHtml);
};

module.exports = AutoUpdater;
20 changes: 6 additions & 14 deletions desktop/updater/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
/**
* External Dependencies
*/
const electron = require('electron');
const app = electron.app;
const dialog = electron.dialog;
const { app } = require('electron');

/**
* Internal dependencies
Expand All @@ -18,13 +16,7 @@ const ManualUpdater = require('./manual-updater');
let updater = false;

function urlBuilder(version) {
let platformString = 'osx';

if (platform.isWindows()) {
platformString = 'windows';
} else if (platform.isLinux()) {
platformString = 'linux';
}
const platformString = 'linux';

if (config.forceUpdate) {
version = '0.0.1';
Expand All @@ -37,11 +29,11 @@ function urlBuilder(version) {

module.exports = function() {
app.on('will-finish-launching', function() {
let url = urlBuilder(app.getVersion());

if (platform.isOSX()) {
updater = new AutoUpdater(url);
// TODO:@adlk: `electron-updater` supports AppImage as well
if (platform.isOSX() || platform.isWindows()) {
updater = new AutoUpdater();
} else {
const url = urlBuilder(app.getVersion());
updater = new ManualUpdater(url);
}

Expand Down