Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import parseUrl from 'parse-url';
import * as AppMenu from './menus/menu';
import * as plugins from './plugins';
import {newWindow} from './ui/window';
import {installUpdateIfNoWindows} from './updater';
import {installCLI} from './utils/cli-install';
import * as windowUtils from './utils/window-utils';

Expand Down Expand Up @@ -168,6 +169,9 @@ app.on('ready', () =>
});

app.on('window-all-closed', () => {
if (installUpdateIfNoWindows()) {
return;
}
if (process.platform !== 'darwin') {
app.quit();
}
Expand Down
17 changes: 17 additions & 0 deletions app/updater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import autoUpdaterLinux from './auto-updater-linux';
import {getDefaultProfile} from './config';
import {version} from './package.json';
import {getDecoratedConfig} from './plugins';
import {shouldAutoInstallUpdate} from './utils/should-auto-install-update';

const {platform} = process;
const isLinux = platform === 'linux';
Expand All @@ -36,6 +37,15 @@ const checkForUpdates = async () => {
let isInit = false;
// Default to the "stable" update channel
let canaryUpdates = false;
let updateReady = false;

export function installUpdateIfNoWindows(): boolean {
if (!shouldAutoInstallUpdate(updateReady, app.getWindows().size, platform)) {
return false;
}
autoUpdater.quitAndInstall();
return true;
}

const buildFeedUrl = (canary: boolean, currentVersion: string) => {
const updatePrefix = canary ? 'releases-canary' : 'releases';
Expand All @@ -61,6 +71,13 @@ async function init() {

autoUpdater.setFeedURL({url: feedURL});

if (!isLinux) {
autoUpdater.on('update-downloaded', () => {
updateReady = true;
installUpdateIfNoWindows();
});
}

setTimeout(() => {
void checkForUpdates();
}, ms('10s'));
Expand Down
7 changes: 7 additions & 0 deletions app/utils/should-auto-install-update.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function shouldAutoInstallUpdate(
updateReady: boolean,
openWindowCount: number,
platform: NodeJS.Platform
): boolean {
return updateReady && openWindowCount === 0 && platform === 'darwin';
}
24 changes: 24 additions & 0 deletions test/unit/updater.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import test from 'ava';

import {shouldAutoInstallUpdate} from '../../app/utils/should-auto-install-update';

test('installs on darwin when an update is ready and no windows are open', (t) => {
t.true(shouldAutoInstallUpdate(true, 0, 'darwin'));
});

test('does not install on win32 when an update is ready and no windows are open', (t) => {
t.false(shouldAutoInstallUpdate(true, 0, 'win32'));
});

test('does not install on linux when an update is ready and no windows are open', (t) => {
t.false(shouldAutoInstallUpdate(true, 0, 'linux'));
});

test('does not install while windows are still open', (t) => {
t.false(shouldAutoInstallUpdate(true, 1, 'darwin'));
t.false(shouldAutoInstallUpdate(true, 3, 'darwin'));
});

test('does not install when no update has been downloaded', (t) => {
t.false(shouldAutoInstallUpdate(false, 0, 'darwin'));
});