mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-25 18:26:05 +08:00
build(electron): nsis updater compatibility fix (#6681)
This commit is contained in:
@@ -1,4 +1,8 @@
|
|||||||
// 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,
|
||||||
@@ -6,6 +10,7 @@ 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,
|
||||||
@@ -19,7 +24,6 @@ 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';
|
||||||
|
|
||||||
interface GithubUpdateInfo extends UpdateInfo {
|
interface GithubUpdateInfo extends UpdateInfo {
|
||||||
tag: string;
|
tag: string;
|
||||||
}
|
}
|
||||||
@@ -37,6 +41,13 @@ 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,
|
||||||
@@ -244,9 +255,21 @@ export class CustomGitHubProvider extends BaseGitHubProvider<GithubUpdateInfo> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
resolveFiles(updateInfo: GithubUpdateInfo): Array<ResolvedUpdateFileInfo> {
|
resolveFiles(updateInfo: GithubUpdateInfo): Array<ResolvedUpdateFileInfo> {
|
||||||
|
const filteredUpdateInfo = structuredClone(updateInfo);
|
||||||
|
// for windows, we need to determine its installer type (nsis or squirrel)
|
||||||
|
if (process.platform === 'win32' && updateInfo.files.length > 1) {
|
||||||
|
const isSquirrel = isSquirrelBuild();
|
||||||
|
// @ts-expect-error we should be able to modify the object
|
||||||
|
filteredUpdateInfo.files = updateInfo.files.filter(file => {
|
||||||
|
return isSquirrel
|
||||||
|
? !file.url.includes('nsis.exe')
|
||||||
|
: file.url.includes('nsis.exe');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// still replace space to - due to backward compatibility
|
// still replace space to - due to backward compatibility
|
||||||
return resolveFiles(updateInfo, this.baseUrl, p =>
|
return resolveFiles(filteredUpdateInfo, this.baseUrl, p =>
|
||||||
this.getBaseDownloadPath(updateInfo.tag, p.replace(/ /g, '-'))
|
this.getBaseDownloadPath(filteredUpdateInfo.tag, p.replace(/ /g, '-'))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,15 +1,32 @@
|
|||||||
import nodePath from 'node:path';
|
import nodePath from 'node:path';
|
||||||
|
import { fileURLToPath } from 'node:url';
|
||||||
|
|
||||||
import type { UpdateCheckResult } from 'electron-updater';
|
import type { UpdateCheckResult } from 'electron-updater';
|
||||||
import fs from 'fs-extra';
|
import fs from 'fs-extra';
|
||||||
import { flatten } from 'lodash-es';
|
import { flatten } from 'lodash-es';
|
||||||
import { http, HttpResponse } from 'msw';
|
import { http, HttpResponse } from 'msw';
|
||||||
import { setupServer } from 'msw/node';
|
import { setupServer } from 'msw/node';
|
||||||
import { afterAll, afterEach, beforeAll, describe, expect, it } from 'vitest';
|
import {
|
||||||
|
afterAll,
|
||||||
|
afterEach,
|
||||||
|
beforeAll,
|
||||||
|
describe,
|
||||||
|
expect,
|
||||||
|
it,
|
||||||
|
vi,
|
||||||
|
} from 'vitest';
|
||||||
|
|
||||||
import { CustomGitHubProvider } from '../../src/main/updater/custom-github-provider';
|
import { CustomGitHubProvider } from '../../src/main/updater/custom-github-provider';
|
||||||
import { MockedAppAdapter, MockedUpdater } from './mocks';
|
import { MockedAppAdapter, MockedUpdater } from './mocks';
|
||||||
|
|
||||||
|
const __dirname = fileURLToPath(new URL('.', import.meta.url));
|
||||||
|
|
||||||
|
vi.mock('electron', () => ({
|
||||||
|
app: {
|
||||||
|
getPath: () => __dirname,
|
||||||
|
},
|
||||||
|
}));
|
||||||
|
|
||||||
const platformTail = (() => {
|
const platformTail = (() => {
|
||||||
// https://github.com/electron-userland/electron-builder/blob/master/packages/electron-updater/src/providers/Provider.ts#L30
|
// https://github.com/electron-userland/electron-builder/blob/master/packages/electron-updater/src/providers/Provider.ts#L30
|
||||||
const platform = process.platform;
|
const platform = process.platform;
|
||||||
|
|||||||
Reference in New Issue
Block a user