From e38c82b6e4d5dff0999b2a5a03db21ee40418f7a Mon Sep 17 00:00:00 2001 From: reito Date: Thu, 22 Jun 2023 15:55:42 +0800 Subject: [PATCH 1/4] Make local-electron works better under Windows Make local-electron compatible with vite plugin --- packages/api/core/src/util/electron-executable.ts | 6 ++++++ .../local-electron/src/LocalElectronPlugin.ts | 14 +++++--------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/packages/api/core/src/util/electron-executable.ts b/packages/api/core/src/util/electron-executable.ts index d0e7f435bc..d3354231e9 100644 --- a/packages/api/core/src/util/electron-executable.ts +++ b/packages/api/core/src/util/electron-executable.ts @@ -28,6 +28,12 @@ export function pluginCompileExists(packageJSON: PackageJSON): boolean { } export default async function locateElectronExecutable(dir: string, packageJSON: PackageJSON): Promise { + const overrideDist = process.env.ELECTRON_OVERRIDE_DIST_PATH; + if (overrideDist) { + console.info('Using electron dist from ELECTRON_OVERRIDE_DIST_PATH: ' + overrideDist); + return overrideDist; + } + let electronModulePath: string | undefined = await getElectronModulePath(dir, packageJSON); if (electronModulePath?.endsWith('electron-prebuilt-compile') && !pluginCompileExists(packageJSON)) { console.warn( diff --git a/packages/plugin/local-electron/src/LocalElectronPlugin.ts b/packages/plugin/local-electron/src/LocalElectronPlugin.ts index 4995a19b47..ff0e5b4cf5 100644 --- a/packages/plugin/local-electron/src/LocalElectronPlugin.ts +++ b/packages/plugin/local-electron/src/LocalElectronPlugin.ts @@ -11,7 +11,11 @@ export default class LocalElectronPlugin extends PluginBase { - if (this.enabled) { - this.checkPlatform(process.platform); - process.env.ELECTRON_OVERRIDE_DIST_PATH = this.config.electronPath; - } - return false; - } - getHooks(): ForgeHookMap { return { packageAfterExtract: this.afterExtract, From 24d54e983623e00603c9db1b805f6072b546c19b Mon Sep 17 00:00:00 2001 From: reito Date: Thu, 22 Jun 2023 16:10:49 +0800 Subject: [PATCH 2/4] Unit tests --- .../local-electron/src/LocalElectronPlugin.ts | 4 +++- .../test/LocalElectronPlugin_spec.ts | 16 ++++------------ 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/packages/plugin/local-electron/src/LocalElectronPlugin.ts b/packages/plugin/local-electron/src/LocalElectronPlugin.ts index ff0e5b4cf5..649c99ed51 100644 --- a/packages/plugin/local-electron/src/LocalElectronPlugin.ts +++ b/packages/plugin/local-electron/src/LocalElectronPlugin.ts @@ -11,12 +11,14 @@ export default class LocalElectronPlugin extends PluginBase { if (this.enabled) { this.checkPlatform(process.platform); process.env.ELECTRON_OVERRIDE_DIST_PATH = this.config.electronPath; } - } + }; get enabled(): boolean { if (typeof this.config.enabled === 'undefined') { diff --git a/packages/plugin/local-electron/test/LocalElectronPlugin_spec.ts b/packages/plugin/local-electron/test/LocalElectronPlugin_spec.ts index df547f7691..9047a4b236 100644 --- a/packages/plugin/local-electron/test/LocalElectronPlugin_spec.ts +++ b/packages/plugin/local-electron/test/LocalElectronPlugin_spec.ts @@ -20,31 +20,23 @@ describe('LocalElectronPlugin', () => { it('should set ELECTRON_OVERRIDE_DIST_PATH when enabled', async () => { expect(process.env.ELECTRON_OVERRIDE_DIST_PATH).to.equal(undefined); const p = new LocalElectronPlugin({ electronPath: 'test/foo' }); - await p.startLogic(); + p.init(); expect(process.env.ELECTRON_OVERRIDE_DIST_PATH).to.equal('test/foo'); }); it('should not set ELECTRON_OVERRIDE_DIST_PATH when disabled', async () => { expect(process.env.ELECTRON_OVERRIDE_DIST_PATH).to.equal(undefined); const p = new LocalElectronPlugin({ enabled: false, electronPath: 'test/foo' }); - await p.startLogic(); + p.init(); expect(process.env.ELECTRON_OVERRIDE_DIST_PATH).to.equal(undefined); }); it("should throw an error if platforms don't match", async () => { const p = new LocalElectronPlugin({ electronPath: 'test/bar', electronPlatform: 'wut' }); - await expect(p.startLogic()).to.eventually.be.rejectedWith( + await expect(p.init()).to.eventually.be.rejectedWith( `Can not use local Electron version, required platform "${process.platform}" but local platform is "wut"` ); }); - - it('should always return false', async () => { - let p = new LocalElectronPlugin({ electronPath: 'test/bar' }); - expect(await p.startLogic()).to.equal(false); - - p = new LocalElectronPlugin({ enabled: false, electronPath: 'test/bar' }); - expect(await p.startLogic()).to.equal(false); - }); }); describe('hooks', () => { @@ -53,7 +45,7 @@ describe('LocalElectronPlugin', () => { beforeEach(() => { p = new LocalElectronPlugin({ electronPath: 'test/foo' }); // eslint-disable-next-line @typescript-eslint/no-explicit-any - p.init('', {} as any); + p.init(); }); describe('with afterExtract hook', () => { From 0f4e105572e22c80f4f26b6461992d041de86313 Mon Sep 17 00:00:00 2001 From: reito Date: Thu, 22 Jun 2023 16:38:17 +0800 Subject: [PATCH 3/4] Update test case for local-electron plugin --- .../plugin/local-electron/test/LocalElectronPlugin_spec.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/plugin/local-electron/test/LocalElectronPlugin_spec.ts b/packages/plugin/local-electron/test/LocalElectronPlugin_spec.ts index 9047a4b236..15c61b5f5e 100644 --- a/packages/plugin/local-electron/test/LocalElectronPlugin_spec.ts +++ b/packages/plugin/local-electron/test/LocalElectronPlugin_spec.ts @@ -33,9 +33,7 @@ describe('LocalElectronPlugin', () => { it("should throw an error if platforms don't match", async () => { const p = new LocalElectronPlugin({ electronPath: 'test/bar', electronPlatform: 'wut' }); - await expect(p.init()).to.eventually.be.rejectedWith( - `Can not use local Electron version, required platform "${process.platform}" but local platform is "wut"` - ); + expect(p.init).to.throw(`Can not use local Electron version, required platform "${process.platform}" but local platform is "wut"`); }); }); From 4af4a7f79f19db6cc9a7aa73fd733c19e696297b Mon Sep 17 00:00:00 2001 From: reito Date: Thu, 22 Jun 2023 16:48:09 +0800 Subject: [PATCH 4/4] Unit tests --- .../plugin/local-electron/test/LocalElectronPlugin_spec.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/plugin/local-electron/test/LocalElectronPlugin_spec.ts b/packages/plugin/local-electron/test/LocalElectronPlugin_spec.ts index 15c61b5f5e..65c71622fd 100644 --- a/packages/plugin/local-electron/test/LocalElectronPlugin_spec.ts +++ b/packages/plugin/local-electron/test/LocalElectronPlugin_spec.ts @@ -46,6 +46,10 @@ describe('LocalElectronPlugin', () => { p.init(); }); + after(() => { + delete process.env.ELECTRON_OVERRIDE_DIST_PATH; + }); + describe('with afterExtract hook', () => { let tmpDir: string;