From 965544beef41f9c6549c64cd4f53e1a3935259ad Mon Sep 17 00:00:00 2001 From: DIYgod Date: Tue, 22 Oct 2024 23:53:36 +0800 Subject: [PATCH] fix: app got deleted when auto update on windows --- .../main/src/updater/custom-github-provider.ts | 15 ++------------- apps/main/src/updater/index.ts | 9 ++++++--- apps/main/src/updater/utils.ts | 18 ++++++++++++++++++ apps/main/src/updater/windows-updater.ts | 9 +++++++++ 4 files changed, 35 insertions(+), 16 deletions(-) create mode 100644 apps/main/src/updater/utils.ts create mode 100644 apps/main/src/updater/windows-updater.ts diff --git a/apps/main/src/updater/custom-github-provider.ts b/apps/main/src/updater/custom-github-provider.ts index 5f7a50fb1..ad7ebb2e7 100644 --- a/apps/main/src/updater/custom-github-provider.ts +++ b/apps/main/src/updater/custom-github-provider.ts @@ -1,8 +1,4 @@ -// credits: migrated from https://github.com/toeverything/AFFiNE/blob/canary/packages/frontend/electron/src/main/updater/custom-github-provider.ts - -import fs from "node:fs" -import path from "node:path" -import { URL } from "node:url" +// credits: migrated from https://github.com/toeverything/AFFiNE/blob/a802dc4fd6aa720f7a7a995816c78c0b24c514f5/packages/frontend/electron/src/main/updater/custom-github-provider.ts import type { CustomPublishOptions, @@ -11,7 +7,6 @@ import type { XElement, } from "builder-util-runtime" import { HttpError, newError, parseXml } from "builder-util-runtime" -import { app } from "electron" import type { AppUpdater, ResolvedUpdateFileInfo, UpdateInfo } from "electron-updater" import { CancellationToken } from "electron-updater" import { BaseGitHubProvider } from "electron-updater/out/providers/GitHubProvider" @@ -20,6 +15,7 @@ import { parseUpdateInfo, resolveFiles } from "electron-updater/out/providers/Pr import * as semver from "semver" import { isWindows } from "../env" +import { isSquirrelBuild } from "./utils" interface GithubUpdateInfo extends UpdateInfo { tag: string @@ -38,13 +34,6 @@ interface GithubRelease { const hrefRegExp = /\/tag\/([^/]+)$/ -function isSquirrelBuild() { - // if it is squirrel build, there will be 'squirrel.exe' - // otherwise it is in nsis web mode - const files = fs.readdirSync(path.dirname(app.getPath("exe"))) - return files.some((it) => it.includes("squirrel.exe")) -} - export class CustomGitHubProvider extends BaseGitHubProvider { constructor( options: CustomPublishOptions, diff --git a/apps/main/src/updater/index.ts b/apps/main/src/updater/index.ts index 461ef2d68..9f7493407 100644 --- a/apps/main/src/updater/index.ts +++ b/apps/main/src/updater/index.ts @@ -1,16 +1,19 @@ import { getRendererHandlers } from "@egoist/tipc/main" -import { autoUpdater } from "electron-updater" +import { autoUpdater as defaultAutoUpdater } from "electron-updater" -import { channel, isDev } from "../env" +import { channel, isDev, isWindows } from "../env" import { logger } from "../logger" import type { RendererHandlers } from "../renderer-handlers" import { destroyMainWindow, getMainWindow } from "../window" import { CustomGitHubProvider } from "./custom-github-provider" +import { WindowsUpdater } from "./windows-updater" // skip auto update in dev mode const disabled = isDev -export const quitAndInstall = async () => { +const autoUpdater = isWindows ? new WindowsUpdater() : defaultAutoUpdater + +export const quitAndInstall = () => { const mainWindow = getMainWindow() destroyMainWindow() diff --git a/apps/main/src/updater/utils.ts b/apps/main/src/updater/utils.ts new file mode 100644 index 000000000..88d0b0c7f --- /dev/null +++ b/apps/main/src/updater/utils.ts @@ -0,0 +1,18 @@ +import fs from "node:fs" +import path from "node:path" + +import { app } from "electron" + +let _isSquirrelBuild: boolean | null = null +export function isSquirrelBuild() { + if (typeof _isSquirrelBuild === "boolean") { + return _isSquirrelBuild + } + + // if it is squirrel build, there will be 'squirrel.exe' + // otherwise it is in nsis web mode + const files = fs.readdirSync(path.dirname(app.getPath("exe"))) + _isSquirrelBuild = files.some((it) => it.includes("squirrel.exe")) + + return _isSquirrelBuild +} diff --git a/apps/main/src/updater/windows-updater.ts b/apps/main/src/updater/windows-updater.ts new file mode 100644 index 000000000..9787bbe94 --- /dev/null +++ b/apps/main/src/updater/windows-updater.ts @@ -0,0 +1,9 @@ +import { app } from "electron" +import { NsisUpdater } from "electron-updater" +import { DownloadedUpdateHelper } from "electron-updater/out/DownloadedUpdateHelper" + +export class WindowsUpdater extends NsisUpdater { + protected override downloadedUpdateHelper: DownloadedUpdateHelper = new DownloadedUpdateHelper( + app.getPath("sessionData"), + ) +}