From e7e7afab3b702a740ae49e62d0f31fee088645b4 Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Mon, 21 Mar 2016 01:39:24 -0700 Subject: [PATCH] implement barebones auto updater --- config.js | 6 ++++++ main/auto-updater.js | 41 +++++++++++++++++++++++++++++++++++++++++ main/index.js | 6 +++++- 3 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 main/auto-updater.js diff --git a/config.js b/config.js index 1fb24458..75505688 100644 --- a/config.js +++ b/config.js @@ -1,12 +1,18 @@ var applicationConfigPath = require('application-config-path') var path = require('path') +var APP_VERSION = require('./package.json').version + module.exports = { APP_COPYRIGHT: 'Copyright © 2014-2016 The WebTorrent Project', APP_FILE_ICON: path.join(__dirname, 'static', 'WebTorrentFile'), APP_ICON: path.join(__dirname, 'static', 'WebTorrent'), APP_NAME: 'WebTorrent', + AUTO_UPDATE_URL: 'https://webtorrent.io/app/updates?version=' + APP_VERSION, + AUTO_UPDATE_CHECK_STARTUP_DELAY: 60 * 1000 /* 1 minute */, + AUTO_UPDATE_CHECK_INTERVAL: 6 * 60 * 60 * 1000 /* 6 hours */, + CONFIG_PATH: applicationConfigPath('WebTorrent'), CONFIG_POSTER_PATH: path.join(applicationConfigPath('WebTorrent'), 'Posters'), CONFIG_TORRENT_PATH: path.join(applicationConfigPath('WebTorrent'), 'Torrents'), diff --git a/main/auto-updater.js b/main/auto-updater.js new file mode 100644 index 00000000..1fcb5b17 --- /dev/null +++ b/main/auto-updater.js @@ -0,0 +1,41 @@ +module.exports = { + init +} + +var electron = require('electron') + +var config = require('../config') + +// var app = electron.app +var autoUpdater = electron.autoUpdater + +function init () { + autoUpdater.on('error', function (err) { + console.error('error downloading app update', err.message || err) + }) + + autoUpdater.setFeedURL(config.AUTO_UPDATE_URL) + + // TODO: remove + autoUpdater.checkForUpdates() + + /* + * We always check for updates on app startup. To keep app startup fast, we delay this + * first check so it happens when there is less going on. + */ + setTimeout(() => autoUpdater.checkForUpdates(), config.AUTO_UPDATE_CHECK_STARTUP_DELAY) + + /* + * After the first check for updates, we continually check for updates on a regular + * interval. This is to ensure that checks happen even when the app is left open for a + * long time. + */ + setInterval(() => autoUpdater.checkForUpdates(), config.AUTO_UPDATE_CHECK_INTERVAL) + + autoUpdater.on('checking-for-update', () => console.log('checking for app update')) + autoUpdater.on('update-available', () => console.log('app update available')) + autoUpdater.on('update-not-available', () => console.log('app update not available')) + autoUpdater.on('update-downloaded', function (e) { + console.log('app update downloaded', e) + }) +} diff --git a/main/index.js b/main/index.js index 7d77750f..0e4d07e7 100644 --- a/main/index.js +++ b/main/index.js @@ -2,6 +2,7 @@ var electron = require('electron') var app = electron.app +var autoUpdater = require('./auto-updater') var config = require('../config') var ipc = require('./ipc') var menu = require('./menu') @@ -38,7 +39,10 @@ var argv = sliceArgv(process.argv) app.on('open-file', onOpen) app.on('open-url', onOpen) -app.on('will-finish-launching', setupCrashReporter) +app.on('will-finish-launching', function () { + autoUpdater.init() + setupCrashReporter() +}) app.ipcReady = false // main window has finished loading and IPC is ready app.isQuitting = false