-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforge.config.ts
More file actions
225 lines (219 loc) · 8.17 KB
/
Copy pathforge.config.ts
File metadata and controls
225 lines (219 loc) · 8.17 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import { join, normalize } from 'node:path';
import { readdirSync, rmdirSync, statSync } from 'node:fs';
import type { ForgeConfig } from '@electron-forge/shared-types';
import { MakerSquirrel } from '@electron-forge/maker-squirrel';
import { MakerZIP } from '@electron-forge/maker-zip';
import { MakerDeb } from '@electron-forge/maker-deb';
import { MakerRpm } from '@electron-forge/maker-rpm';
import { VitePlugin } from '@electron-forge/plugin-vite';
import { FusesPlugin } from '@electron-forge/plugin-fuses';
import { FuseV1Options, FuseVersion } from '@electron/fuses';
import packageJson from './package.json';
import { Walker, DepType, type Module } from 'flora-colossus';
let nativeModuleDependenciesToPackage: string[] = [];
export const EXTERNAL_DEPENDENCIES = ['electron-squirrel-startup', 'better-sqlite3'];
const config: ForgeConfig = {
// Workaround to include better-sqlite3 in the build
// https://github.com/electron/forge/issues/3738#issuecomment-2622541945
hooks: {
prePackage: async () => {
const projectRoot = normalize(__dirname);
const getExternalNestedDependencies = async (
nodeModuleNames: string[],
includeNestedDeps = true
) => {
const foundModules = new Set(nodeModuleNames);
if (includeNestedDeps) {
for (const external of nodeModuleNames) {
type MyPublicClass<T> = {
[P in keyof T]: T[P];
};
type MyPublicWalker = MyPublicClass<Walker> & {
modules: Module[];
walkDependenciesForModule: (moduleRoot: string, depType: DepType) => Promise<void>;
};
const moduleRoot = join(projectRoot, 'node_modules', external);
const walker = new Walker(moduleRoot) as unknown as MyPublicWalker;
walker.modules = [];
await walker.walkDependenciesForModule(moduleRoot, DepType.PROD);
walker.modules
.filter(dep => (dep.nativeModuleType as number) === DepType.PROD)
// for a package like '@realm/fetch', need to split the path and just take the first part
.map(dep => dep.name.split('/')[0])
.forEach(name => foundModules.add(name));
}
}
return foundModules;
};
const nativeModuleDependencies = await getExternalNestedDependencies(EXTERNAL_DEPENDENCIES);
nativeModuleDependenciesToPackage = Array.from(nativeModuleDependencies);
},
packageAfterPrune: async (_forgeConfig, buildPath) => {
function getItemsFromFolder(
path: string,
totalCollection: {
path: string;
type: 'directory' | 'file';
empty: boolean;
}[] = []
) {
try {
const normalizedPath = normalize(path);
const childItems = readdirSync(normalizedPath);
const getItemStats = statSync(normalizedPath);
if (getItemStats.isDirectory()) {
totalCollection.push({
path: normalizedPath,
type: 'directory',
empty: childItems.length === 0,
});
}
childItems.forEach(childItem => {
const childItemNormalizedPath = join(normalizedPath, childItem);
const childItemStats = statSync(childItemNormalizedPath);
if (childItemStats.isDirectory()) {
getItemsFromFolder(childItemNormalizedPath, totalCollection);
} else {
totalCollection.push({
path: childItemNormalizedPath,
type: 'file',
empty: false,
});
}
});
} catch {
return;
}
return totalCollection;
}
const getItems = getItemsFromFolder(buildPath) ?? [];
for (const item of getItems) {
const DELETE_EMPTY_DIRECTORIES = true;
if (item.empty === true) {
if (DELETE_EMPTY_DIRECTORIES) {
const pathToDelete = normalize(item.path);
// one last check to make sure it is a directory and is empty
const stats = statSync(pathToDelete);
if (!stats.isDirectory()) {
// SKIPPING DELETION: pathToDelete is not a directory
return;
}
const childItems = readdirSync(pathToDelete);
if (childItems.length !== 0) {
// SKIPPING DELETION: pathToDelete is not empty
return;
}
rmdirSync(pathToDelete);
}
}
}
},
},
packagerConfig: {
prune: true,
asar: { unpackDir: '' },
executableName: 'pubgmhud-sync',
icon: join(__dirname, 'src/assets/icon'),
ignore: file => {
const filePath = file.toLowerCase();
const KEEP_FILE = {
keep: false,
log: true,
};
// NOTE: must return false for empty string or nothing will be packaged
if (filePath === '') KEEP_FILE.keep = true;
if (!KEEP_FILE.keep && filePath === '/package.json') KEEP_FILE.keep = true;
if (!KEEP_FILE.keep && filePath === '/node_modules') KEEP_FILE.keep = true;
if (!KEEP_FILE.keep && filePath === '/.vite') KEEP_FILE.keep = true;
if (!KEEP_FILE.keep && filePath.startsWith('/.vite/')) KEEP_FILE.keep = true;
if (!KEEP_FILE.keep && filePath.startsWith('/node_modules/')) {
// check if matches any of the external dependencies
for (const dep of nativeModuleDependenciesToPackage) {
if (filePath === `/node_modules/${dep}/` || filePath === `/node_modules/${dep}`) {
KEEP_FILE.keep = true;
break;
}
if (filePath === `/node_modules/${dep}/package.json`) {
KEEP_FILE.keep = true;
break;
}
if (filePath.startsWith(`/node_modules/${dep}/`)) {
KEEP_FILE.keep = true;
KEEP_FILE.log = false;
break;
}
}
}
if (KEEP_FILE.keep) {
if (KEEP_FILE.log) console.log('Keeping:', file);
return false;
}
return true;
},
extraResource: ['./src/lib/db/migrations'],
overwrite: true,
},
rebuildConfig: {
extraModules: ['better-sqlite3'],
force: true,
},
makers: [
new MakerSquirrel({
name: packageJson.productName,
version: packageJson.version,
description: packageJson.description,
setupIcon: join(__dirname, 'src/assets/icon.ico'),
iconUrl:
'https://cdn.discordapp.com/attachments/1071115465964912782/1381580776809697383/icon.ico?ex=684808dd&is=6846b75d&hm=a5fa8629508a65d4bed2d6c9f15fe58fe0736449225af4ead40947e5e2f90098&',
}),
new MakerZIP({}, ['darwin', 'linux']),
new MakerRpm({
options: {
name: packageJson.name,
productName: packageJson.productName,
version: packageJson.version,
description: packageJson.description,
license: packageJson.license,
icon: join(__dirname, 'src/assets/icon.png'),
},
}),
new MakerDeb({}),
],
plugins: [
new VitePlugin({
// `build` can specify multiple entry builds, which can be Main process, Preload scripts, Worker process, etc.
// If you are familiar with Vite configuration, it will look really familiar.
build: [
{
// `entry` is just an alias for `build.lib.entry` in the corresponding file of `config`.
entry: 'src/main.ts',
config: 'vite.main.config.ts',
target: 'main',
},
{
entry: 'src/preload.ts',
config: 'vite.preload.config.ts',
target: 'preload',
},
],
renderer: [
{
name: 'main_window',
config: 'vite.renderer.config.ts',
},
],
}),
// Fuses are used to enable/disable various Electron functionality
// at package time, before code signing the application
new FusesPlugin({
version: FuseVersion.V1,
[FuseV1Options.RunAsNode]: false,
[FuseV1Options.EnableCookieEncryption]: true,
[FuseV1Options.EnableNodeOptionsEnvironmentVariable]: false,
[FuseV1Options.EnableNodeCliInspectArguments]: false,
[FuseV1Options.EnableEmbeddedAsarIntegrityValidation]: true,
[FuseV1Options.OnlyLoadAppFromAsar]: true,
}),
],
};
export default config;