Typescriptify main.js

This commit is contained in:
Scott Nonnenberg
2021-10-01 11:49:59 -07:00
committed by GitHub
parent e033fd2cf3
commit 9a1430a460
22 changed files with 721 additions and 524 deletions

View File

@@ -354,9 +354,17 @@ export function setUpdateListener(performUpdateCallback: () => void): void {
ipcMain.once('start-update', performUpdateCallback);
}
export function getAutoDownloadUpdateSetting(
mainWindow: BrowserWindow
export async function getAutoDownloadUpdateSetting(
mainWindow: BrowserWindow | undefined,
logger: LoggerType
): Promise<boolean> {
if (!mainWindow) {
logger.warn(
'getAutoDownloadUpdateSetting: No main window, returning false'
);
return false;
}
return new Promise((resolve, reject) => {
ipcMain.once(
'settings:get-success:autoDownloadUpdate',

View File

@@ -14,7 +14,7 @@ let initialized = false;
let updater: UpdaterInterface | undefined;
export async function start(
getMainWindow: () => BrowserWindow,
getMainWindow: () => BrowserWindow | undefined,
logger?: LoggerType
): Promise<void> {
const { platform } = process;

View File

@@ -30,7 +30,7 @@ import { DialogType } from '../types/Dialogs';
const INTERVAL = 30 * durations.MINUTE;
export async function start(
getMainWindow: () => BrowserWindow,
getMainWindow: () => BrowserWindow | undefined,
logger: LoggerType
): Promise<UpdaterInterface> {
logger.info('macos/start: starting checks...');
@@ -61,7 +61,7 @@ let updateFilePath: string;
let loggerForQuitHandler: LoggerType;
async function checkForUpdatesMaybeInstall(
getMainWindow: () => BrowserWindow,
getMainWindow: () => BrowserWindow | undefined,
logger: LoggerType,
force = false
) {
@@ -75,12 +75,13 @@ async function checkForUpdatesMaybeInstall(
if (fileName !== newFileName || !version || gt(newVersion, version)) {
const autoDownloadUpdates = await getAutoDownloadUpdateSetting(
getMainWindow()
getMainWindow(),
logger
);
if (!autoDownloadUpdates) {
setUpdateListener(async () => {
logger.info(
'performUpdate: have not downloaded update, going to download'
'checkForUpdatesMaybeInstall: have not downloaded update, going to download'
);
await downloadAndInstall(
newFileName,
@@ -90,14 +91,22 @@ async function checkForUpdatesMaybeInstall(
true
);
});
getMainWindow().webContents.send(
'show-update-dialog',
DialogType.DownloadReady,
{
downloadSize: result.size,
version: result.version,
}
);
const mainWindow = getMainWindow();
if (mainWindow) {
mainWindow.webContents.send(
'show-update-dialog',
DialogType.DownloadReady,
{
downloadSize: result.size,
version: result.version,
}
);
} else {
logger.warn(
'checkForUpdatesMaybeInstall: no mainWindow, cannot show update dialog'
);
}
return;
}
await downloadAndInstall(newFileName, newVersion, getMainWindow, logger);
@@ -107,7 +116,7 @@ async function checkForUpdatesMaybeInstall(
async function downloadAndInstall(
newFileName: string,
newVersion: string,
getMainWindow: () => BrowserWindow,
getMainWindow: () => BrowserWindow | undefined,
logger: LoggerType,
updateOnProgress?: boolean
) {
@@ -151,20 +160,25 @@ async function downloadAndInstall(
} catch (error) {
const readOnly = 'Cannot update while running on a read-only volume';
const message: string = error.message || '';
if (message.includes(readOnly)) {
const mainWindow = getMainWindow();
if (mainWindow && message.includes(readOnly)) {
logger.info('downloadAndInstall: showing read-only dialog...');
getMainWindow().webContents.send(
mainWindow.webContents.send(
'show-update-dialog',
DialogType.MacOS_Read_Only
);
} else {
} else if (mainWindow) {
logger.info(
'downloadAndInstall: showing general update failure dialog...'
);
getMainWindow().webContents.send(
mainWindow.webContents.send(
'show-update-dialog',
DialogType.Cannot_Update
);
} else {
logger.warn(
'downloadAndInstall: no mainWindow, cannot show update dialog'
);
}
throw error;
@@ -179,9 +193,17 @@ async function downloadAndInstall(
markShouldQuit();
autoUpdater.quitAndInstall();
});
getMainWindow().webContents.send('show-update-dialog', DialogType.Update, {
version,
});
const mainWindow = getMainWindow();
if (mainWindow) {
mainWindow.webContents.send('show-update-dialog', DialogType.Update, {
version,
});
} else {
logger.warn(
'checkForUpdatesMaybeInstall: no mainWindow, cannot show update dialog'
);
}
} catch (error) {
logger.error(`downloadAndInstall: ${getPrintableError(error)}`);
}

View File

@@ -37,7 +37,7 @@ let installing: boolean;
let loggerForQuitHandler: LoggerType;
export async function start(
getMainWindow: () => BrowserWindow,
getMainWindow: () => BrowserWindow | undefined,
logger: LoggerType
): Promise<UpdaterInterface> {
logger.info('windows/start: starting checks...');
@@ -64,7 +64,7 @@ export async function start(
}
async function checkForUpdatesMaybeInstall(
getMainWindow: () => BrowserWindow,
getMainWindow: () => BrowserWindow | undefined,
logger: LoggerType,
force = false
) {
@@ -78,12 +78,13 @@ async function checkForUpdatesMaybeInstall(
if (fileName !== newFileName || !version || gt(newVersion, version)) {
const autoDownloadUpdates = await getAutoDownloadUpdateSetting(
getMainWindow()
getMainWindow(),
logger
);
if (!autoDownloadUpdates) {
setUpdateListener(async () => {
logger.info(
'performUpdate: have not downloaded update, going to download'
'checkForUpdatesMaybeInstall: have not downloaded update, going to download'
);
await downloadAndInstall(
newFileName,
@@ -93,14 +94,21 @@ async function checkForUpdatesMaybeInstall(
true
);
});
getMainWindow().webContents.send(
'show-update-dialog',
DialogType.DownloadReady,
{
downloadSize: result.size,
version: result.version,
}
);
const mainWindow = getMainWindow();
if (mainWindow) {
mainWindow.webContents.send(
'show-update-dialog',
DialogType.DownloadReady,
{
downloadSize: result.size,
version: result.version,
}
);
} else {
logger.warn(
'checkForUpdatesMaybeInstall: No mainWindow, not showing update dialog'
);
}
return;
}
await downloadAndInstall(newFileName, newVersion, getMainWindow, logger);
@@ -110,7 +118,7 @@ async function checkForUpdatesMaybeInstall(
async function downloadAndInstall(
newFileName: string,
newVersion: string,
getMainWindow: () => BrowserWindow,
getMainWindow: () => BrowserWindow | undefined,
logger: LoggerType,
updateOnProgress?: boolean
) {
@@ -151,11 +159,18 @@ async function downloadAndInstall(
await verifyAndInstall(updateFilePath, newVersion, logger);
installing = true;
} catch (error) {
logger.info('createUpdater: showing general update failure dialog...');
getMainWindow().webContents.send(
'show-update-dialog',
DialogType.Cannot_Update
);
const mainWindow = getMainWindow();
if (mainWindow) {
logger.info(
'createUpdater: showing general update failure dialog...'
);
mainWindow.webContents.send(
'show-update-dialog',
DialogType.Cannot_Update
);
} else {
logger.warn('createUpdater: no mainWindow, just failing over...');
}
throw error;
}
@@ -163,9 +178,17 @@ async function downloadAndInstall(
markShouldQuit();
app.quit();
});
getMainWindow().webContents.send('show-update-dialog', DialogType.Update, {
version,
});
const mainWindow = getMainWindow();
if (mainWindow) {
mainWindow.webContents.send('show-update-dialog', DialogType.Update, {
version,
});
} else {
logger.warn(
'downloadAndInstall: no mainWindow, cannot show update dialog'
);
}
} catch (error) {
logger.error(`downloadAndInstall: ${getPrintableError(error)}`);
}