/
LocalElectronPlugin.ts
64 lines (50 loc) · 1.87 KB
/
LocalElectronPlugin.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { PluginBase } from '@electron-forge/plugin-base';
import { ForgeHookFn, ForgeHookMap } from '@electron-forge/shared-types';
import fs from 'fs-extra';
import { LocalElectronPluginConfig } from './Config';
export default class LocalElectronPlugin extends PluginBase<LocalElectronPluginConfig> {
name = 'local-electron';
constructor(c: LocalElectronPluginConfig) {
super(c);
this.getHooks = this.getHooks.bind(this);
this.startLogic = this.startLogic.bind(this);
}
get enabled(): boolean {
if (typeof this.config.enabled === 'undefined') {
return true;
}
return this.config.enabled;
}
async startLogic(): Promise<false> {
if (this.enabled) {
this.checkPlatform(process.platform);
process.env.ELECTRON_OVERRIDE_DIST_PATH = this.config.electronPath;
}
return false;
}
getHooks(): ForgeHookMap {
return {
packageAfterExtract: this.afterExtract,
};
}
private checkPlatform = (platform: string) => {
if ((this.config.electronPlatform || process.platform) !== platform) {
throw new Error(
`Can not use local Electron version, required platform "${platform}" but local platform is "${this.config.electronPlatform || process.platform}"`
);
}
};
private checkArch = (arch: string) => {
if ((this.config.electronArch || process.arch) !== arch) {
throw new Error(`Can not use local Electron version, required arch "${arch}" but local arch is "${this.config.electronArch || process.arch}"`);
}
};
private afterExtract: ForgeHookFn<'packageAfterExtract'> = async (_config, buildPath, _electronVersion, platform, arch) => {
if (!this.enabled) return;
this.checkPlatform(platform);
this.checkArch(arch);
await fs.remove(buildPath);
await fs.copy(this.config.electronPath, buildPath);
};
}
export { LocalElectronPlugin, LocalElectronPluginConfig };