Skip to content

Commit 9c17ae7

Browse files
committed
finished updating
1 parent 257f452 commit 9c17ae7

File tree

7 files changed

+91
-80
lines changed

7 files changed

+91
-80
lines changed

packages/graphql-playground-electron/src/main/createWindow.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { BrowserWindow, app, ipcMain } from 'electron'
22
import * as path from 'path'
33
import dev = require('electron-is-dev')
4-
import { newWindowConfig, log } from '../shared/utils'
4+
import { newWindowConfig } from '../shared/utils'
5+
import * as log from 'electron-log'
56
import { WindowContext } from './types'
67

78
export function createWindow(windowContext: WindowContext) {

packages/graphql-playground-electron/src/main/index.ts

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,16 @@ import {
1010
} from 'electron'
1111
import * as queryString from 'query-string'
1212
import * as fs from 'fs'
13-
import { log } from '../shared/utils'
13+
import * as log from 'electron-log'
1414
import { buildTemplate } from './menu'
1515
import { createWindow } from './createWindow'
1616
import { WindowContext } from './types'
1717
import { startUpdates } from './updates'
1818
import squirrelStartup = require('electron-squirrel-startup')
1919

20+
log.transports.file.level = 'info'
21+
log.transports.console.level = 'debug'
22+
2023
// Immediately quit the app if squirrel is launching it
2124
if (squirrelStartup) {
2225
app.quit()
@@ -93,17 +96,6 @@ app.on('ready', () => {
9396
createWindow(windowContext)
9497

9598
startUpdates()
96-
// const logger = require('electron-log')
97-
// logger.transports.file.level = 'info'
98-
// autoUpdater.logger = logger
99-
100-
// forceSend('SettingsRequest', '')
101-
102-
// ipcMain.once('SettingsResponse', (event, settingsString) => {
103-
// log.info('settings', settingsString)
104-
// autoUpdater.allowPrerelease = getBetaUpdates(settingsString)
105-
// autoUpdater.checkForUpdatesAndNotify()
106-
// })
10799

108100
const menu = Menu.buildFromTemplate(buildTemplate(windowContext))
109101
Menu.setApplicationMenu(menu)
@@ -177,14 +169,3 @@ async function forceSend(channel: string, arg: string, byPath?: string) {
177169
await windowContext.readyWindowsPromises[window.id]
178170
window.webContents.send(channel, arg)
179171
}
180-
181-
// function getBetaUpdates(settingsString: string | undefined): boolean {
182-
// try {
183-
// const settings = JSON.parse(settingsString)
184-
// return !!settings['general.betaUpdates']
185-
// } catch (e) {
186-
// //
187-
// }
188-
189-
// return false
190-
// }

packages/graphql-playground-electron/src/main/menu.ts

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ import {
22
MenuItemConstructorOptions,
33
BrowserWindow,
44
app,
5-
dialog,
5+
autoUpdater,
66
} from 'electron'
7-
import { autoUpdater } from 'electron-updater'
8-
import { log } from '../shared/utils'
7+
import * as log from 'electron-log'
98
import { WindowContext } from './types'
109
import { createWindow } from './createWindow'
10+
import { notify } from './notify'
1111

1212
export const buildTemplate = (
1313
windowContext: WindowContext,
@@ -21,25 +21,15 @@ export const buildTemplate = (
2121
} as MenuItemConstructorOptions,
2222
{
2323
label: 'Check For Updates',
24-
click: async () => {
25-
const {
26-
updateInfo,
27-
downloadPromise,
28-
} = await autoUpdater.checkForUpdates()
29-
if (updateInfo.version !== autoUpdater.currentVersion) {
30-
const buttonIndex = dialog.showMessageBox({
31-
message: `New version available: ${updateInfo.version}`,
32-
buttons: ['Install Update', 'Later'],
33-
})
34-
if (buttonIndex === 0) {
35-
await downloadPromise
36-
autoUpdater.quitAndInstall()
37-
}
38-
} else {
39-
dialog.showMessageBox({
40-
message: 'Already up to date.',
24+
click: () => {
25+
autoUpdater.once('update-not-available', () => {
26+
notify({
27+
title: 'GraphQL Playground Updates',
28+
body: 'Already up to date.',
4129
})
42-
}
30+
})
31+
32+
autoUpdater.checkForUpdates()
4333
},
4434
},
4535
{ type: 'separator' },

packages/graphql-playground-electron/src/main/notify.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import { shell, Notification } from 'electron'
22

3-
export const notify = ({ title, body, url, onClick }) => {
3+
interface NotificationOptions {
4+
title: string
5+
body: string
6+
url?: string
7+
onClick?: () => void
8+
}
9+
10+
export const notify = ({ title, body, url, onClick }: NotificationOptions) => {
411
const notification = new Notification({
512
title,
613
body,
Lines changed: 64 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,34 @@
1-
import { app, autoUpdater } from 'electron'
1+
import { app, autoUpdater, ipcMain, BrowserWindow, dialog } from 'electron'
22
import ms = require('ms')
33
import dev = require('electron-is-dev')
4+
import * as log from 'electron-log'
5+
import { notify } from './notify'
46

5-
const setUpdateURL = () => {
6-
const server = 'https://hazel-server-ppbimurjwk.now.sh/update'
7+
export const startUpdates = () => {
8+
if (!dev) {
9+
startAppUpdates()
10+
}
11+
}
12+
13+
const setUpdateURL = async () => {
14+
let betaUpdates = false
15+
16+
await new Promise(resolve => {
17+
ipcMain.once('SettingsResponse', (event, settingsString) => {
18+
log.info('settings', settingsString)
19+
betaUpdates = getBetaUpdates(settingsString)
20+
resolve()
21+
})
22+
23+
send('SettingsRequest', '')
24+
})
25+
26+
const channel = betaUpdates ? 'kygnjrcroc' : 'ppbimurjwk'
27+
const server = `https://hazel-server-${channel}.now.sh/update`
728
autoUpdater.setFeedURL(`${server}/${process.platform}/${app.getVersion()}`)
829
}
930

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

1738
// Ensure we're pulling from the correct channel
1839
try {
19-
setUpdateURL()
40+
await setUpdateURL()
2041
} catch (err) {
42+
log.error(err)
2143
// Retry later if setting the update URL failed
2244
return
2345
}
@@ -28,43 +50,59 @@ const checkForUpdates = () => {
2850

2951
const startAppUpdates = () => {
3052
autoUpdater.on('error', error => {
31-
// Report errors to console. We can't report
32-
// to Slack and restart here, because it will
33-
// cause the app to never start again
34-
console.error(error)
35-
36-
// Then check again for update after 15 minutes
53+
log.error(error)
3754
setTimeout(checkForUpdates, ms('15m'))
3855
})
3956

40-
// Check for app update after startup
41-
setTimeout(checkForUpdates, ms('10s'))
42-
43-
autoUpdater.on('update-downloaded', () => {
44-
autoUpdater.quitAndInstall()
45-
app.quit()
57+
autoUpdater.on('checking-for-update', () => {
58+
log.info('Checking for app updates...')
4659
})
4760

48-
autoUpdater.on('checking-for-update', () => {
49-
console.log('Checking for app updates...')
61+
autoUpdater.on('update-downloaded', () => {
62+
log.info('Update downloaded')
63+
const buttonIndex = dialog.showMessageBox({
64+
message: `Update downlaoded. Install now?`,
65+
buttons: ['Install Update & Restart', 'Later'],
66+
})
67+
if (buttonIndex === 0) {
68+
autoUpdater.quitAndInstall()
69+
app.quit()
70+
}
5071
})
5172

5273
autoUpdater.on('update-available', () => {
53-
console.log('Found update for the app! Downloading...')
74+
log.info('Found update for the app! Downloading...')
75+
notify({
76+
title: 'GraphQL Playground Updates',
77+
body: 'Update available. Downloading...',
78+
})
5479
})
5580

5681
autoUpdater.on('update-not-available', () => {
57-
console.log('No updates found. Checking again in 5 minutes...')
82+
log.info('No updates found. Checking again in 5 minutes...')
5883
setTimeout(checkForUpdates, ms('5m'))
5984
})
85+
86+
setTimeout(checkForUpdates, ms('10s'))
6087
}
6188

62-
export const startUpdates = () => {
63-
if (process.platform === 'linux') {
64-
return
89+
function send(channel: string, arg: string) {
90+
const window = BrowserWindow.getAllWindows()[0]
91+
if (window) {
92+
log.info('sending to open window', channel, arg)
93+
window.webContents.send(channel, arg)
94+
} else {
95+
log.info('no opened window')
6596
}
97+
}
6698

67-
if (!dev) {
68-
startAppUpdates()
99+
function getBetaUpdates(settingsString: string | undefined): boolean {
100+
try {
101+
const settings = JSON.parse(settingsString)
102+
return !!settings['general.betaUpdates']
103+
} catch (e) {
104+
//
69105
}
106+
107+
return false
70108
}

packages/graphql-playground-electron/src/renderer/components/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ interface State {
4343
config?: GraphQLConfigData
4444
}
4545

46-
ipcRenderer.once('SettingsRequest', () => {
46+
ipcRenderer.on('SettingsRequest', () => {
4747
ipcRenderer.send('SettingsResponse', localStorage.getItem('settings'))
4848
})
4949

packages/graphql-playground-electron/src/shared/utils.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,4 @@ export function createRemoteWindow() {
1818
: `file://${path.join(__dirname, '..', '/dist/index.html')}`
1919

2020
win.loadURL(url)
21-
}
22-
23-
export const log = {
24-
info: (...args) => {
25-
console.log(...args)
26-
},
27-
}
21+
}

0 commit comments

Comments
 (0)