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
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
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
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 });

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
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,

View File

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

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