From 140054085200ac01978ccfceaeec1d69a790552f Mon Sep 17 00:00:00 2001 From: Thomas Nordquist Date: Tue, 9 Apr 2019 12:04:20 +0200 Subject: [PATCH] Refactor electron launcher Don't try auto-update on portable builds --- src/autoUpdater.ts | 26 ++++++++++++++++++++ src/buildOptions.ts | 13 ++++++++++ src/development.ts | 22 +++++++++++++++++ src/electron.ts | 60 +++++++-------------------------------------- 4 files changed, 70 insertions(+), 51 deletions(-) create mode 100644 src/autoUpdater.ts create mode 100644 src/buildOptions.ts create mode 100644 src/development.ts diff --git a/src/autoUpdater.ts b/src/autoUpdater.ts new file mode 100644 index 0000000..99fd7ed --- /dev/null +++ b/src/autoUpdater.ts @@ -0,0 +1,26 @@ +import { autoUpdater } from 'electron-updater' +import { BuildInfo } from 'electron-telemetry/build/Model' +import { UpdateInfo } from '../events' +import { updateNotifier } from '../backend/src/index' + +export function shouldUpdate(build: BuildInfo) { + return build.package !== 'portable' +} + +export function handleAutoUpdate() { + autoUpdater.on('update-available', (info: UpdateInfo) => { + console.log('There is an update available') + }) + + autoUpdater.on('error', () => { + console.log('could not update due to error') + }) + + updateNotifier.onCheckUpdateRequest.subscribe(() => { + try { + autoUpdater.checkForUpdatesAndNotify() + } catch (error) { + console.error(error) + } + }) +} diff --git a/src/buildOptions.ts b/src/buildOptions.ts new file mode 100644 index 0000000..1af164b --- /dev/null +++ b/src/buildOptions.ts @@ -0,0 +1,13 @@ +import * as fs from 'fs' +import * as path from 'path' +import { BuildInfo } from 'electron-telemetry/build/Model' + +let buildOptions: BuildInfo = ({ platform: process.platform, package: 'unpacked' } as any) +try { + const options = JSON.parse(fs.readFileSync(path.join(__dirname, '..', '..', 'buildOptions.json')).toString()) + if (typeof options.platform === 'string' && typeof options.package === 'string') { + buildOptions = options + } +} catch (loadingBuildOptionsMayFail) {} + +export default buildOptions \ No newline at end of file diff --git a/src/development.ts b/src/development.ts new file mode 100644 index 0000000..a241436 --- /dev/null +++ b/src/development.ts @@ -0,0 +1,22 @@ +import axios from 'axios' + +export async function waitForDevServer() { + let response + + while (!response) { + try { + response = await axios.get('http://localhost:8080') + } catch { + console.log('Waiting for dev server') + await new Promise(resolve => setTimeout(resolve, 1000)) + } + } +} + +export function isDev() { + return Boolean(process.argv.find(arg => arg === '--development')) +} + +export function runningUiTestOnCi() { + return Boolean(process.argv.find(arg => arg === '--runningUiTestOnCi')) +} diff --git a/src/electron.ts b/src/electron.ts index b3e15bb..706dc37 100644 --- a/src/electron.ts +++ b/src/electron.ts @@ -1,34 +1,19 @@ -import * as fs from 'fs' import * as log from 'electron-log' import * as path from 'path' import ConfigStorage from '../backend/src/ConfigStorage' import { app, BrowserWindow, Menu } from 'electron' import { autoUpdater } from 'electron-updater' -import { BuildInfo } from 'electron-telemetry/build/Model' -import { ConnectionManager, updateNotifier } from '../backend/src/index' +import { ConnectionManager } from '../backend/src/index' import { electronTelemetryFactory } from 'electron-telemetry' import { menuTemplate } from './MenuTemplate' -import { UpdateInfo } from '../events' -import axios from 'axios' +import buildOptions from './buildOptions' +import { waitForDevServer, isDev, runningUiTestOnCi } from './development' +import { shouldAutoUpdate as shouldAutoUpdate, handleAutoUpdate } from './autoUpdater' -const isDev = Boolean(process.argv.find(arg => arg === '--development')) - -if (!isDev) { - let buildOptions: BuildInfo = ({ platform: process.platform, package: 'unpacked' } as any) - try { - const options = JSON.parse(fs.readFileSync(path.join(__dirname, '..', '..', 'buildOptions.json')).toString()) - if (typeof options.platform === 'string' && typeof options.package === 'string') { - buildOptions = options - } - } catch (loadingBuildOptionsMayFail) {} - - console.log(buildOptions) +if (!isDev()) { const electronTelemetry = electronTelemetryFactory('9b0c8ca04a361eb8160d98c5', buildOptions) } -// const isDebugEnabled = Boolean(process.argv.find(arg => arg === 'debug')) -const runningUiTestOnCi = Boolean(process.argv.find(arg => arg === '--runningUiTestOnCi')) - autoUpdater.logger = log log.info('App starting...') @@ -44,16 +29,7 @@ let mainWindow: BrowserWindow | undefined async function createWindow() { if (isDev) { - let response - - while (!response) { - try { - response = await axios.get('http://localhost:8080') - } catch { - console.log('Waiting for dev server') - await new Promise(resolve => setTimeout(resolve, 1000)) - } - } + await waitForDevServer() } const iconPath = path.join(__dirname, 'icon.png') @@ -107,27 +83,9 @@ app.on('ready', () => { Menu.setApplicationMenu(menuTemplate) createWindow() - let updateInfo: UpdateInfo - autoUpdater.on('update-available', (info: UpdateInfo) => { - console.log('there is an update') - updateInfo = info - }) - - autoUpdater.on('error', () => { - console.log('could not update due to error') - - if (updateInfo) { - updateNotifier.notify(updateInfo) - } - }) - - updateNotifier.onCheckUpdateRequest.subscribe(() => { - try { - autoUpdater.checkForUpdatesAndNotify() - } catch (error) { - console.error(error) - } - }) + if (shouldAutoUpdate(buildOptions)) { + handleAutoUpdate() + } }) // Quit when all windows are closed.