diff --git a/packages/graphql-playground-electron/src/main/createWindow.ts b/packages/graphql-playground-electron/src/main/createWindow.ts index a890d5706..d9750833e 100644 --- a/packages/graphql-playground-electron/src/main/createWindow.ts +++ b/packages/graphql-playground-electron/src/main/createWindow.ts @@ -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) { diff --git a/packages/graphql-playground-electron/src/main/index.ts b/packages/graphql-playground-electron/src/main/index.ts index 7818de4e2..c5b9ed2b3 100644 --- a/packages/graphql-playground-electron/src/main/index.ts +++ b/packages/graphql-playground-electron/src/main/index.ts @@ -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() @@ -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) @@ -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 -// } diff --git a/packages/graphql-playground-electron/src/main/menu.ts b/packages/graphql-playground-electron/src/main/menu.ts index 7ca70479b..5a8afc9de 100644 --- a/packages/graphql-playground-electron/src/main/menu.ts +++ b/packages/graphql-playground-electron/src/main/menu.ts @@ -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, @@ -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' }, diff --git a/packages/graphql-playground-electron/src/main/notify.ts b/packages/graphql-playground-electron/src/main/notify.ts index 780b20ab9..bffe66e7d 100644 --- a/packages/graphql-playground-electron/src/main/notify.ts +++ b/packages/graphql-playground-electron/src/main/notify.ts @@ -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, diff --git a/packages/graphql-playground-electron/src/main/updates.ts b/packages/graphql-playground-electron/src/main/updates.ts index 8ae901c50..4046d0374 100644 --- a/packages/graphql-playground-electron/src/main/updates.ts +++ b/packages/graphql-playground-electron/src/main/updates.ts @@ -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')) @@ -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 } @@ -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 } diff --git a/packages/graphql-playground-electron/src/renderer/components/App.tsx b/packages/graphql-playground-electron/src/renderer/components/App.tsx index ce12a3bc4..9d7c62f61 100644 --- a/packages/graphql-playground-electron/src/renderer/components/App.tsx +++ b/packages/graphql-playground-electron/src/renderer/components/App.tsx @@ -43,7 +43,7 @@ interface State { config?: GraphQLConfigData } -ipcRenderer.once('SettingsRequest', () => { +ipcRenderer.on('SettingsRequest', () => { ipcRenderer.send('SettingsResponse', localStorage.getItem('settings')) }) diff --git a/packages/graphql-playground-electron/src/shared/utils.ts b/packages/graphql-playground-electron/src/shared/utils.ts index 777cc10c9..ab6fe47d6 100644 --- a/packages/graphql-playground-electron/src/shared/utils.ts +++ b/packages/graphql-playground-electron/src/shared/utils.ts @@ -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) - }, -} +} \ No newline at end of file