Skip to content

There can be more than Notion and Miro. AFFiNE(pronounced [ə‘fain]) is a next-gen knowledge base that brings planning, sorting and creating all together. Privacy first, open-source, customizable and ready to use.

fix(electron): app got deleted when auto update on windows (#7820)

liuyi83a9beed83af
diff --git a/packages/frontend/electron/scripts/generate-assets.ts b/packages/frontend/electron/scripts/generate-assets.ts
index 68452316d4..649284bac4 100755
--- a/packages/frontend/electron/scripts/generate-assets.ts
+++ b/packages/frontend/electron/scripts/generate-assets.ts
@@ -67,23 +67,25 @@ if (!process.env.SKIP_WEB_BUILD) {
// step 1.5: amend sourceMappingURL to allow debugging in devtools
await glob('**/*.{js,css}', { cwd: affineWebOutDir }).then(files => {
- return files.map(async file => {
- const dir = path.dirname(file);
- const fullpath = path.join(affineWebOutDir, file);
- let content = await fs.readFile(fullpath, 'utf-8');
- // replace # sourceMappingURL=76-6370cd185962bc89.js.map
- // to # sourceMappingURL=assets://./{dir}/76-6370cd185962bc89.js.map
- content = content.replace(/# sourceMappingURL=(.*)\.map/g, (_, p1) => {
- return `# sourceMappingURL=assets://./${dir}/${p1}.map`;
- });
- try {
- await fs.writeFile(fullpath, content);
- console.log('amended sourceMappingURL for', fullpath);
- } catch (e) {
- // do not crash the build
- console.error('error writing file', fullpath, e);
- }
- });
+ return Promise.all(
+ files.map(async file => {
+ const dir = path.dirname(file);
+ const fullpath = path.join(affineWebOutDir, file);
+ let content = await fs.readFile(fullpath, 'utf-8');
+ // replace # sourceMappingURL=76-6370cd185962bc89.js.map
+ // to # sourceMappingURL=assets://./{dir}/76-6370cd185962bc89.js.map
+ content = content.replace(/# sourceMappingURL=(.*)\.map/g, (_, p1) => {
+ return `# sourceMappingURL=assets://./${dir}/${p1}.map`;
+ });
+ try {
+ await fs.writeFile(fullpath, content);
+ console.log('amended sourceMappingURL for', fullpath);
+ } catch (e) {
+ // do not crash the build
+ console.error('error writing file', fullpath, e);
+ }
+ })
+ );
});
await fs.move(affineWebOutDir, publicAffineOutDir, { overwrite: true });
diff --git a/packages/frontend/electron/src/main/updater/custom-github-provider.ts b/packages/frontend/electron/src/main/updater/custom-github-provider.ts
index cf37a2522d..605f308f9c 100644
--- a/packages/frontend/electron/src/main/updater/custom-github-provider.ts
+++ b/packages/frontend/electron/src/main/updater/custom-github-provider.ts
@@ -1,8 +1,5 @@
// credits: migrated from https://github.com/electron-userland/electron-builder/blob/master/packages/electron-updater/src/providers/GitHubProvider.ts
-import fs from 'node:fs';
-import path from 'node:path';
-
import type {
CustomPublishOptions,
GithubOptions,
@@ -10,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,
@@ -24,6 +20,9 @@ import {
resolveFiles,
} from 'electron-updater/out/providers/Provider';
import * as semver from 'semver';
+
+import { isSquirrelBuild } from './utils';
+
interface GithubUpdateInfo extends UpdateInfo {
tag: string;
}
@@ -41,13 +40,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<GithubUpdateInfo> {
constructor(
options: CustomPublishOptions,
diff --git a/packages/frontend/electron/src/main/updater/electron-updater.ts b/packages/frontend/electron/src/main/updater/electron-updater.ts
index 39bccd0091..401132019e 100644
--- a/packages/frontend/electron/src/main/updater/electron-updater.ts
+++ b/packages/frontend/electron/src/main/updater/electron-updater.ts
@@ -1,10 +1,11 @@
import { app } from 'electron';
-import { autoUpdater } from 'electron-updater';
+import { autoUpdater as defaultAutoUpdater } from 'electron-updater';
import { buildType } from '../config';
import { logger } from '../logger';
import { CustomGitHubProvider } from './custom-github-provider';
import { updaterSubjects } from './event';
+import { WindowsUpdater } from './windows-updater';
const mode = process.env.NODE_ENV;
const isDev = mode === 'development';
@@ -12,6 +13,9 @@ const isDev = mode === 'development';
// skip auto update in dev mode & internal
const disabled = buildType === 'internal' || isDev;
+export const autoUpdater =
+ process.platform === 'win32' ? new WindowsUpdater() : defaultAutoUpdater;
+
export const quitAndInstall = async () => {
autoUpdater.quitAndInstall();
};
diff --git a/packages/frontend/electron/src/main/updater/utils.ts b/packages/frontend/electron/src/main/updater/utils.ts
new file mode 100644
index 0000000000..ddf73ef8ff
--- /dev/null
+++ b/packages/frontend/electron/src/main/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/packages/frontend/electron/src/main/updater/windows-updater.ts b/packages/frontend/electron/src/main/updater/windows-updater.ts
new file mode 100644
index 0000000000..94242a5a9d
--- /dev/null
+++ b/packages/frontend/electron/src/main/updater/windows-updater.ts
@@ -0,0 +1,8 @@
+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'));
+}