Skip to content

Commit

Permalink
finished updating
Browse files Browse the repository at this point in the history
  • Loading branch information
schickling committed Dec 26, 2017
1 parent 257f452 commit 9c17ae7
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 80 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { BrowserWindow, app, ipcMain } from 'electron'
import * as path from 'path'
import dev = require('electron-is-dev')
import { newWindowConfig, log } from '../shared/utils'
import { newWindowConfig } from '../shared/utils'
import * as log from 'electron-log'
import { WindowContext } from './types'

export function createWindow(windowContext: WindowContext) {
Expand Down
27 changes: 4 additions & 23 deletions packages/graphql-playground-electron/src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@ import {
} from 'electron'
import * as queryString from 'query-string'
import * as fs from 'fs'
import { log } from '../shared/utils'
import * as log from 'electron-log'
import { buildTemplate } from './menu'
import { createWindow } from './createWindow'
import { WindowContext } from './types'
import { startUpdates } from './updates'
import squirrelStartup = require('electron-squirrel-startup')

log.transports.file.level = 'info'
log.transports.console.level = 'debug'

// Immediately quit the app if squirrel is launching it
if (squirrelStartup) {
app.quit()
Expand Down Expand Up @@ -93,17 +96,6 @@ app.on('ready', () => {
createWindow(windowContext)

startUpdates()
// const logger = require('electron-log')
// logger.transports.file.level = 'info'
// autoUpdater.logger = logger

// forceSend('SettingsRequest', '')

// ipcMain.once('SettingsResponse', (event, settingsString) => {
// log.info('settings', settingsString)
// autoUpdater.allowPrerelease = getBetaUpdates(settingsString)
// autoUpdater.checkForUpdatesAndNotify()
// })

const menu = Menu.buildFromTemplate(buildTemplate(windowContext))
Menu.setApplicationMenu(menu)
Expand Down Expand Up @@ -177,14 +169,3 @@ async function forceSend(channel: string, arg: string, byPath?: string) {
await windowContext.readyWindowsPromises[window.id]
window.webContents.send(channel, arg)
}

// function getBetaUpdates(settingsString: string | undefined): boolean {
// try {
// const settings = JSON.parse(settingsString)
// return !!settings['general.betaUpdates']
// } catch (e) {
// //
// }

// return false
// }
32 changes: 11 additions & 21 deletions packages/graphql-playground-electron/src/main/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import {
MenuItemConstructorOptions,
BrowserWindow,
app,
dialog,
autoUpdater,
} from 'electron'
import { autoUpdater } from 'electron-updater'
import { log } from '../shared/utils'
import * as log from 'electron-log'
import { WindowContext } from './types'
import { createWindow } from './createWindow'
import { notify } from './notify'

export const buildTemplate = (
windowContext: WindowContext,
Expand All @@ -21,25 +21,15 @@ export const buildTemplate = (
} as MenuItemConstructorOptions,
{
label: 'Check For Updates',
click: async () => {
const {
updateInfo,
downloadPromise,
} = await autoUpdater.checkForUpdates()
if (updateInfo.version !== autoUpdater.currentVersion) {
const buttonIndex = dialog.showMessageBox({
message: `New version available: ${updateInfo.version}`,
buttons: ['Install Update', 'Later'],
})
if (buttonIndex === 0) {
await downloadPromise
autoUpdater.quitAndInstall()
}
} else {
dialog.showMessageBox({
message: 'Already up to date.',
click: () => {
autoUpdater.once('update-not-available', () => {
notify({
title: 'GraphQL Playground Updates',
body: 'Already up to date.',
})
}
})

autoUpdater.checkForUpdates()
},
},
{ type: 'separator' },
Expand Down
9 changes: 8 additions & 1 deletion packages/graphql-playground-electron/src/main/notify.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { shell, Notification } from 'electron'

export const notify = ({ title, body, url, onClick }) => {
interface NotificationOptions {
title: string
body: string
url?: string
onClick?: () => void
}

export const notify = ({ title, body, url, onClick }: NotificationOptions) => {
const notification = new Notification({
title,
body,
Expand Down
90 changes: 64 additions & 26 deletions packages/graphql-playground-electron/src/main/updates.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,34 @@
import { app, autoUpdater } from 'electron'
import { app, autoUpdater, ipcMain, BrowserWindow, dialog } from 'electron'
import ms = require('ms')
import dev = require('electron-is-dev')
import * as log from 'electron-log'
import { notify } from './notify'

const setUpdateURL = () => {
const server = 'https://hazel-server-ppbimurjwk.now.sh/update'
export const startUpdates = () => {
if (!dev) {
startAppUpdates()
}
}

const setUpdateURL = async () => {
let betaUpdates = false

await new Promise(resolve => {
ipcMain.once('SettingsResponse', (event, settingsString) => {
log.info('settings', settingsString)
betaUpdates = getBetaUpdates(settingsString)
resolve()
})

send('SettingsRequest', '')
})

const channel = betaUpdates ? 'kygnjrcroc' : 'ppbimurjwk'
const server = `https://hazel-server-${channel}.now.sh/update`
autoUpdater.setFeedURL(`${server}/${process.platform}/${app.getVersion()}`)
}

const checkForUpdates = () => {
const checkForUpdates = async () => {
if (process.env.CONNECTION === 'offline') {
// Try again after half an hour
setTimeout(checkForUpdates, ms('30m'))
Expand All @@ -16,8 +37,9 @@ const checkForUpdates = () => {

// Ensure we're pulling from the correct channel
try {
setUpdateURL()
await setUpdateURL()
} catch (err) {
log.error(err)
// Retry later if setting the update URL failed
return
}
Expand All @@ -28,43 +50,59 @@ const checkForUpdates = () => {

const startAppUpdates = () => {
autoUpdater.on('error', error => {
// Report errors to console. We can't report
// to Slack and restart here, because it will
// cause the app to never start again
console.error(error)

// Then check again for update after 15 minutes
log.error(error)
setTimeout(checkForUpdates, ms('15m'))
})

// Check for app update after startup
setTimeout(checkForUpdates, ms('10s'))

autoUpdater.on('update-downloaded', () => {
autoUpdater.quitAndInstall()
app.quit()
autoUpdater.on('checking-for-update', () => {
log.info('Checking for app updates...')
})

autoUpdater.on('checking-for-update', () => {
console.log('Checking for app updates...')
autoUpdater.on('update-downloaded', () => {
log.info('Update downloaded')
const buttonIndex = dialog.showMessageBox({
message: `Update downlaoded. Install now?`,
buttons: ['Install Update & Restart', 'Later'],
})
if (buttonIndex === 0) {
autoUpdater.quitAndInstall()
app.quit()
}
})

autoUpdater.on('update-available', () => {
console.log('Found update for the app! Downloading...')
log.info('Found update for the app! Downloading...')
notify({
title: 'GraphQL Playground Updates',
body: 'Update available. Downloading...',
})
})

autoUpdater.on('update-not-available', () => {
console.log('No updates found. Checking again in 5 minutes...')
log.info('No updates found. Checking again in 5 minutes...')
setTimeout(checkForUpdates, ms('5m'))
})

setTimeout(checkForUpdates, ms('10s'))
}

export const startUpdates = () => {
if (process.platform === 'linux') {
return
function send(channel: string, arg: string) {
const window = BrowserWindow.getAllWindows()[0]
if (window) {
log.info('sending to open window', channel, arg)
window.webContents.send(channel, arg)
} else {
log.info('no opened window')
}
}

if (!dev) {
startAppUpdates()
function getBetaUpdates(settingsString: string | undefined): boolean {
try {
const settings = JSON.parse(settingsString)
return !!settings['general.betaUpdates']
} catch (e) {
//
}

return false
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ interface State {
config?: GraphQLConfigData
}

ipcRenderer.once('SettingsRequest', () => {
ipcRenderer.on('SettingsRequest', () => {
ipcRenderer.send('SettingsResponse', localStorage.getItem('settings'))
})

Expand Down
8 changes: 1 addition & 7 deletions packages/graphql-playground-electron/src/shared/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,4 @@ export function createRemoteWindow() {
: `file://${path.join(__dirname, '..', '/dist/index.html')}`

win.loadURL(url)
}

export const log = {
info: (...args) => {
console.log(...args)
},
}
}

0 comments on commit 9c17ae7

Please sign in to comment.