Refactor electron launcher
Don't try auto-update on portable builds
This commit is contained in:
26
src/autoUpdater.ts
Normal file
26
src/autoUpdater.ts
Normal file
@@ -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)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
13
src/buildOptions.ts
Normal file
13
src/buildOptions.ts
Normal file
@@ -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
|
||||||
22
src/development.ts
Normal file
22
src/development.ts
Normal file
@@ -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'))
|
||||||
|
}
|
||||||
@@ -1,34 +1,19 @@
|
|||||||
import * as fs from 'fs'
|
|
||||||
import * as log from 'electron-log'
|
import * as log from 'electron-log'
|
||||||
import * as path from 'path'
|
import * as path from 'path'
|
||||||
import ConfigStorage from '../backend/src/ConfigStorage'
|
import ConfigStorage from '../backend/src/ConfigStorage'
|
||||||
import { app, BrowserWindow, Menu } from 'electron'
|
import { app, BrowserWindow, Menu } from 'electron'
|
||||||
import { autoUpdater } from 'electron-updater'
|
import { autoUpdater } from 'electron-updater'
|
||||||
import { BuildInfo } from 'electron-telemetry/build/Model'
|
import { ConnectionManager } from '../backend/src/index'
|
||||||
import { ConnectionManager, updateNotifier } from '../backend/src/index'
|
|
||||||
import { electronTelemetryFactory } from 'electron-telemetry'
|
import { electronTelemetryFactory } from 'electron-telemetry'
|
||||||
import { menuTemplate } from './MenuTemplate'
|
import { menuTemplate } from './MenuTemplate'
|
||||||
import { UpdateInfo } from '../events'
|
import buildOptions from './buildOptions'
|
||||||
import axios from 'axios'
|
import { waitForDevServer, isDev, runningUiTestOnCi } from './development'
|
||||||
|
import { shouldAutoUpdate as shouldAutoUpdate, handleAutoUpdate } from './autoUpdater'
|
||||||
|
|
||||||
const isDev = Boolean(process.argv.find(arg => arg === '--development'))
|
if (!isDev()) {
|
||||||
|
|
||||||
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)
|
|
||||||
const electronTelemetry = electronTelemetryFactory('9b0c8ca04a361eb8160d98c5', buildOptions)
|
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
|
autoUpdater.logger = log
|
||||||
log.info('App starting...')
|
log.info('App starting...')
|
||||||
|
|
||||||
@@ -44,16 +29,7 @@ let mainWindow: BrowserWindow | undefined
|
|||||||
|
|
||||||
async function createWindow() {
|
async function createWindow() {
|
||||||
if (isDev) {
|
if (isDev) {
|
||||||
let response
|
await waitForDevServer()
|
||||||
|
|
||||||
while (!response) {
|
|
||||||
try {
|
|
||||||
response = await axios.get('http://localhost:8080')
|
|
||||||
} catch {
|
|
||||||
console.log('Waiting for dev server')
|
|
||||||
await new Promise(resolve => setTimeout(resolve, 1000))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const iconPath = path.join(__dirname, 'icon.png')
|
const iconPath = path.join(__dirname, 'icon.png')
|
||||||
@@ -107,29 +83,11 @@ app.on('ready', () => {
|
|||||||
Menu.setApplicationMenu(menuTemplate)
|
Menu.setApplicationMenu(menuTemplate)
|
||||||
createWindow()
|
createWindow()
|
||||||
|
|
||||||
let updateInfo: UpdateInfo
|
if (shouldAutoUpdate(buildOptions)) {
|
||||||
autoUpdater.on('update-available', (info: UpdateInfo) => {
|
handleAutoUpdate()
|
||||||
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)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
// Quit when all windows are closed.
|
// Quit when all windows are closed.
|
||||||
app.on('window-all-closed', () => {
|
app.on('window-all-closed', () => {
|
||||||
// On macOS it is common for applications and their menu bar
|
// On macOS it is common for applications and their menu bar
|
||||||
|
|||||||
Reference in New Issue
Block a user