-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforge.config.ts
More file actions
235 lines (220 loc) · 7.75 KB
/
forge.config.ts
File metadata and controls
235 lines (220 loc) · 7.75 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
226
227
228
229
230
231
232
233
234
235
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 { MakerDMG } from '@electron-forge/maker-dmg';
import { VitePlugin } from '@electron-forge/plugin-vite';
import { AutoUnpackNativesPlugin } from '@electron-forge/plugin-auto-unpack-natives';
import { PublisherGithub } from '@electron-forge/publisher-github';
import fs from 'fs';
import path from 'path';
import crypto from 'crypto';
// Import app configuration (committed to repo)
import { config as appConfig } from './app.config';
// Modules that need to be copied to the packaged app (native modules)
const modulesToCopy = ['better-sqlite3', 'bindings', 'file-uri-to-path', 'electron-squirrel-startup'];
// Helper to copy directory recursively
function copyDirSync(src: string, dest: string) {
fs.mkdirSync(dest, { recursive: true });
const entries = fs.readdirSync(src, { withFileTypes: true });
for (const entry of entries) {
const srcPath = path.join(src, entry.name);
const destPath = path.join(dest, entry.name);
if (entry.isDirectory()) {
copyDirSync(srcPath, destPath);
} else {
fs.copyFileSync(srcPath, destPath);
}
}
}
const config: ForgeConfig = {
packagerConfig: {
name: appConfig.productName.replace(/\s+/g, ''), // Remove spaces for app name
executableName: appConfig.executableName,
icon: './assets/icon',
appBundleId: appConfig.appBundleId,
appCategoryType: appConfig.appCategory,
asar: {
unpack: '**/*.{node,dylib}',
},
extraResource: [],
// macOS code signing (only applied when env vars are set)
// These MUST remain as environment variables (secrets)
...(process.env.APPLE_ID &&
process.env.APPLE_PASSWORD &&
process.env.APPLE_TEAM_ID && {
osxSign: {
identity: process.env.APPLE_SIGNING_IDENTITY || 'Developer ID Application',
identityValidation: true,
},
osxNotarize: {
appleId: process.env.APPLE_ID,
appleIdPassword: process.env.APPLE_PASSWORD,
teamId: process.env.APPLE_TEAM_ID,
},
}),
},
rebuildConfig: {
// Rebuild native modules for Electron
onlyModules: ['better-sqlite3'],
force: true,
},
hooks: {
packageAfterCopy: async (_config, buildPath) => {
// Copy native modules to the build directory
const nodeModulesSrc = path.resolve(__dirname, 'node_modules');
const nodeModulesDest = path.join(buildPath, 'node_modules');
for (const moduleName of modulesToCopy) {
const src = path.join(nodeModulesSrc, moduleName);
const dest = path.join(nodeModulesDest, moduleName);
if (fs.existsSync(src)) {
console.log(`Copying native module: ${moduleName}`);
copyDirSync(src, dest);
}
}
// Generate app-update.yml with embedded token for electron-updater
// IMPORTANT: This must be done BEFORE code signing (in packageAfterCopy, not postPackage)
// Adding files after signing invalidates the code signature!
const ghToken = process.env.GH_TOKEN;
let appUpdateYml = `provider: github
owner: ${appConfig.github.owner}
repo: ${appConfig.github.repo}
private: ${appConfig.github.private}
`;
if (ghToken) {
appUpdateYml += `token: ${ghToken}\n`;
console.log('Embedding GH_TOKEN in app-update.yml for private repo access');
} else if (appConfig.github.private) {
console.log('Warning: GH_TOKEN not set - auto-updates will not work for private repo');
}
// buildPath is the app's Contents/Resources/app directory
// We need to write to the parent Resources directory (outside asar)
const resourcesPath = path.resolve(buildPath, '..');
const updateYmlDest = path.join(resourcesPath, 'app-update.yml');
console.log(`Writing app-update.yml to ${resourcesPath}`);
fs.writeFileSync(updateYmlDest, appUpdateYml);
},
postMake: async (_config, makeResults) => {
// Generate latest-*.yml files for electron-updater
// These files tell electron-updater what version is available and file checksums
const packageJson = JSON.parse(fs.readFileSync(path.join(__dirname, 'package.json'), 'utf8'));
const version = packageJson.version;
const releaseDate = new Date().toISOString();
for (const result of makeResults) {
const { platform, arch, artifacts } = result;
// Find the distributable artifact (DMG for mac, exe/nupkg for Windows, deb/rpm for Linux)
for (const artifactPath of artifacts) {
const fileName = path.basename(artifactPath);
const artifactDir = path.dirname(artifactPath);
// Calculate SHA512 hash
const fileBuffer = fs.readFileSync(artifactPath);
const sha512 = crypto.createHash('sha512').update(fileBuffer).digest('base64');
const size = fs.statSync(artifactPath).size;
let ymlFileName: string | null = null;
let ymlContent: string | null = null;
if (platform === 'darwin' && fileName.endsWith('.zip')) {
// macOS uses ZIP files for auto-update
ymlFileName = `latest-mac.yml`;
ymlContent = `version: ${version}
files:
- url: ${fileName}
sha512: ${sha512}
size: ${size}
path: ${fileName}
sha512: ${sha512}
releaseDate: '${releaseDate}'
`;
} else if (platform === 'win32' && fileName.endsWith('.exe')) {
// Windows
ymlFileName = `latest.yml`;
ymlContent = `version: ${version}
files:
- url: ${fileName}
sha512: ${sha512}
size: ${size}
path: ${fileName}
sha512: ${sha512}
releaseDate: '${releaseDate}'
`;
} else if (platform === 'linux' && fileName.endsWith('.deb')) {
// Linux
ymlFileName = `latest-linux.yml`;
ymlContent = `version: ${version}
files:
- url: ${fileName}
sha512: ${sha512}
size: ${size}
path: ${fileName}
sha512: ${sha512}
releaseDate: '${releaseDate}'
`;
}
if (ymlFileName && ymlContent) {
const ymlPath = path.join(artifactDir, ymlFileName);
console.log(`Writing ${ymlFileName} for ${platform}-${arch}`);
fs.writeFileSync(ymlPath, ymlContent);
// Add the yml file to artifacts so it gets uploaded
artifacts.push(ymlPath);
}
}
}
},
},
makers: [
new MakerSquirrel({
name: appConfig.productName.replace(/\s+/g, ''),
setupIcon: './assets/icon.ico',
}),
new MakerZIP({}, ['darwin', 'linux']),
new MakerDMG({
format: 'ULFO',
icon: './assets/icon.icns',
}),
new MakerDeb({
options: {
icon: './assets/icon.png',
maintainer: appConfig.maintainer,
homepage: `https://github.com/${appConfig.github.owner}/${appConfig.github.repo}`,
},
}),
new MakerRpm({
options: {
icon: './assets/icon.png',
homepage: `https://github.com/${appConfig.github.owner}/${appConfig.github.repo}`,
},
}),
],
publishers: [
new PublisherGithub({
repository: {
owner: appConfig.github.owner,
name: appConfig.github.repo,
},
prerelease: false,
draft: false,
}),
],
plugins: [
new AutoUnpackNativesPlugin({}),
new VitePlugin({
build: [
{
entry: 'src/main.ts',
config: 'vite.main.config.ts',
},
{
entry: 'src/preload.ts',
config: 'vite.preload.config.ts',
},
],
renderer: [
{
name: 'main_window',
config: 'vite.renderer.config.ts',
},
],
}),
],
};
export default config;