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

This commit is contained in:
liuyi
2024-08-13 14:26:26 +08:00
committed by GitHub
parent 1db6b9fe3b
commit 83a9beed83
5 changed files with 53 additions and 29 deletions

View File

@@ -67,23 +67,25 @@ if (!process.env.SKIP_WEB_BUILD) {
// step 1.5: amend sourceMappingURL to allow debugging in devtools // step 1.5: amend sourceMappingURL to allow debugging in devtools
await glob('**/*.{js,css}', { cwd: affineWebOutDir }).then(files => { await glob('**/*.{js,css}', { cwd: affineWebOutDir }).then(files => {
return files.map(async file => { return Promise.all(
const dir = path.dirname(file); files.map(async file => {
const fullpath = path.join(affineWebOutDir, file); const dir = path.dirname(file);
let content = await fs.readFile(fullpath, 'utf-8'); const fullpath = path.join(affineWebOutDir, file);
// replace # sourceMappingURL=76-6370cd185962bc89.js.map let content = await fs.readFile(fullpath, 'utf-8');
// to # sourceMappingURL=assets://./{dir}/76-6370cd185962bc89.js.map // replace # sourceMappingURL=76-6370cd185962bc89.js.map
content = content.replace(/# sourceMappingURL=(.*)\.map/g, (_, p1) => { // to # sourceMappingURL=assets://./{dir}/76-6370cd185962bc89.js.map
return `# sourceMappingURL=assets://./${dir}/${p1}.map`; content = content.replace(/# sourceMappingURL=(.*)\.map/g, (_, p1) => {
}); return `# sourceMappingURL=assets://./${dir}/${p1}.map`;
try { });
await fs.writeFile(fullpath, content); try {
console.log('amended sourceMappingURL for', fullpath); await fs.writeFile(fullpath, content);
} catch (e) { console.log('amended sourceMappingURL for', fullpath);
// do not crash the build } catch (e) {
console.error('error writing file', fullpath, e); // do not crash the build
} console.error('error writing file', fullpath, e);
}); }
})
);
}); });
await fs.move(affineWebOutDir, publicAffineOutDir, { overwrite: true }); await fs.move(affineWebOutDir, publicAffineOutDir, { overwrite: true });

View File

@@ -1,8 +1,5 @@
// credits: migrated from https://github.com/electron-userland/electron-builder/blob/master/packages/electron-updater/src/providers/GitHubProvider.ts // 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 { import type {
CustomPublishOptions, CustomPublishOptions,
GithubOptions, GithubOptions,
@@ -10,7 +7,6 @@ import type {
XElement, XElement,
} from 'builder-util-runtime'; } from 'builder-util-runtime';
import { HttpError, newError, parseXml } from 'builder-util-runtime'; import { HttpError, newError, parseXml } from 'builder-util-runtime';
import { app } from 'electron';
import type { import type {
AppUpdater, AppUpdater,
ResolvedUpdateFileInfo, ResolvedUpdateFileInfo,
@@ -24,6 +20,9 @@ import {
resolveFiles, resolveFiles,
} from 'electron-updater/out/providers/Provider'; } from 'electron-updater/out/providers/Provider';
import * as semver from 'semver'; import * as semver from 'semver';
import { isSquirrelBuild } from './utils';
interface GithubUpdateInfo extends UpdateInfo { interface GithubUpdateInfo extends UpdateInfo {
tag: string; tag: string;
} }
@@ -41,13 +40,6 @@ interface GithubRelease {
const hrefRegExp = /\/tag\/([^/]+)$/; 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> { export class CustomGitHubProvider extends BaseGitHubProvider<GithubUpdateInfo> {
constructor( constructor(
options: CustomPublishOptions, options: CustomPublishOptions,

View File

@@ -1,10 +1,11 @@
import { app } from 'electron'; import { app } from 'electron';
import { autoUpdater } from 'electron-updater'; import { autoUpdater as defaultAutoUpdater } from 'electron-updater';
import { buildType } from '../config'; import { buildType } from '../config';
import { logger } from '../logger'; import { logger } from '../logger';
import { CustomGitHubProvider } from './custom-github-provider'; import { CustomGitHubProvider } from './custom-github-provider';
import { updaterSubjects } from './event'; import { updaterSubjects } from './event';
import { WindowsUpdater } from './windows-updater';
const mode = process.env.NODE_ENV; const mode = process.env.NODE_ENV;
const isDev = mode === 'development'; const isDev = mode === 'development';
@@ -12,6 +13,9 @@ const isDev = mode === 'development';
// skip auto update in dev mode & internal // skip auto update in dev mode & internal
const disabled = buildType === 'internal' || isDev; const disabled = buildType === 'internal' || isDev;
export const autoUpdater =
process.platform === 'win32' ? new WindowsUpdater() : defaultAutoUpdater;
export const quitAndInstall = async () => { export const quitAndInstall = async () => {
autoUpdater.quitAndInstall(); autoUpdater.quitAndInstall();
}; };

View File

@@ -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;
}

View File

@@ -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'));
}