forked from Zettlr/Zettlr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
forge.config.js
395 lines (373 loc) · 15.3 KB
/
forge.config.js
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
const { spawn } = require('child_process')
const fs = require('fs').promises
const path = require('path')
const { FusesPlugin } = require('@electron-forge/plugin-fuses')
const { FuseV1Options, FuseVersion } = require('@electron/fuses')
const { getGitHash } = require('./scripts/get-git-hash.js')
/**
* This function runs the get-pandoc script in order to download the requested
* version of Pandoc. This way we can guarantee that the correct Pandoc version
* will be present when packaging the application.
*
* @param {string} platform The platform for which to download.
* @param {string} arch The architecture for which to download.
*/
async function downloadPandoc (platform, arch) {
// Check we have a valid platform ...
if (![ 'darwin', 'linux', 'win32' ].includes(platform)) {
throw new Error(`Cannot download Pandoc: Platform ${platform} is not recognised!`)
}
// ... and a valid architecture.
if (![ 'x64', 'arm' ].includes(arch)) {
throw new Error(`Cannot download Pandoc: Architecture ${arch} is not supported!`)
}
// Now run the script and wait for it to finish.
await new Promise((resolve, reject) => {
const argWin = [ 'bash.exe', [ './scripts/get-pandoc.sh', platform, arch ] ]
const argUnix = [ './scripts/get-pandoc.sh', [ platform, arch ] ]
// Use the spread operator to spawn the process using the correct arguments.
const shellProcess = (process.platform === 'win32') ? spawn(...argWin) : spawn(...argUnix)
// To not mess with Electron forge's output, suppress this processes output.
// But we should reject if there's any error output.
let shouldReject = false
shellProcess.stderr.on('data', (_data) => {
shouldReject = true
})
// Resolve or reject once the process has finished.
shellProcess.on('close', (code, _signal) => {
if (code !== 0 || shouldReject) {
reject(new Error(`Failed to download Pandoc: Process quit with code ${code}. If the code is 0, then there was error output.`))
} else {
resolve()
}
})
// Reject on errors.
shellProcess.on('error', (err) => {
reject(err)
})
})
}
module.exports = {
hooks: {
generateAssets: async (forgeConfig, targetPlatform, targetArch) => {
// Two steps need to be done here. First, we need to set an environment
// variable that is then accessible by the webpack process so that we can
// either include or not include fsevents for macOS platforms.
process.env.BUNDLE_FSEVENTS = (targetPlatform === 'darwin') ? '1' : '0'
// This will be baked into the binary so that we know which commit this
// build was based off on.
process.env.GIT_COMMIT_HASH = await getGitHash()
// Second, we need to make sure we can bundle Pandoc.
const isMacOS = targetPlatform === 'darwin'
const isLinux = targetPlatform === 'linux'
const isWin32 = targetPlatform === 'win32'
const isArm64 = targetArch === 'arm64'
const is64Bit = targetArch === 'x64'
// macOS has Rosetta 2 built-in, so we can bundle Pandoc 64bit
const supportsPandoc = is64Bit || (isMacOS && isArm64) || (isLinux && isArm64)
if (supportsPandoc && isWin32) {
// Download Pandoc beforehand, if it's not yet there.
try {
await fs.lstat(path.join(__dirname, './resources/pandoc-win32-x64.exe'))
} catch (err) {
await downloadPandoc('win32', 'x64')
}
await fs.copyFile(path.join(__dirname, './resources/pandoc-win32-x64.exe'), path.join(__dirname, './resources/pandoc.exe'))
forgeConfig.packagerConfig.extraResource.push(path.join(__dirname, './resources/pandoc.exe'))
} else if (supportsPandoc && (isMacOS || isLinux)) {
// Download Pandoc either for macOS or Linux ...
const platform = isMacOS ? 'darwin' : 'linux'
// ... and the ARM or x64 version.
const arch = isArm64 ? 'arm' : 'x64'
try {
await fs.lstat(path.join(__dirname, `./resources/pandoc-${platform}-${arch}`))
} catch (err) {
await downloadPandoc(platform, arch)
}
await fs.copyFile(path.join(__dirname, `./resources/pandoc-${platform}-${arch}`), path.join(__dirname, './resources/pandoc'))
forgeConfig.packagerConfig.extraResource.push(path.join(__dirname, './resources/pandoc'))
} else {
// If someone is building this on an unsupported platform, drop a warning.
console.log(`\nBuilding for an unsupported platform/arch-combination ${targetPlatform}/${targetArch} - not bundling Pandoc.`)
}
},
postMake: async (forgeConfig, makeResults) => {
const basePath = __dirname
const releaseDir = path.join(basePath, 'release')
// Ensure the output dir exists
try {
await fs.stat(releaseDir)
} catch (err) {
await fs.mkdir(releaseDir, { recursive: true })
}
// makeResults is an array for each maker that has the keys `artifacts`,
// `packageJSON`, `platform`, and `arch`.
for (const result of makeResults) {
// Get the necessary information from the object
const { version, productName } = result.packageJSON
// NOTE: Other makers may produce more than one artifact, but I'll have
// to hardcode what to do in those cases.
if (result.artifacts.length > 1) {
throw new Error(`More than one artifact generated -- please resolve ambiguity for ${result.platform} ${result.arch} in build script.`)
}
const sourceFile = result.artifacts[0]
const ext = path.extname(sourceFile)
// NOTE: Arch needs to vary depending on the target platform
let arch = result.arch
if (arch === 'x64' && ext === '.deb') {
arch = 'amd64' // Debian x64
} else if (arch === 'x64' && [ '.rpm', '.AppImage' ].includes(ext)) {
arch = 'x86_64' // Fedora x64 and AppImage x64
} else if (arch === 'arm64' && ext === '.rpm') {
arch = 'aarch64' // Fedora ARM
} // Else: Keep it at either x64 or arm64
// Now we can finally build the correct file name
const baseName = `${productName}-${version}-${arch}${ext}`
// Move the file
await fs.rename(sourceFile, path.join(releaseDir, baseName))
}
}
},
rebuildConfig: {
// Since we must build native modules for both x64 as well as arm64, we have
// to explicitly build it everytime for the correct architecture
force: false // NOTE: By now covered by the global flag on packaging.
},
packagerConfig: {
appBundleId: 'com.zettlr.app',
// This info.plist file contains file association for the app on macOS.
extendInfo: './scripts/assets/info.plist',
asar: {
// We must add native node modules to this option. Doing so ensures that
// the modules will be code-signed. (They still end up in the final
// app.asar file, but they will be code-signed.) Code signing these dylibs
// is required on macOS for the Node process to properly load them.
unpack: '*.{node,dll}'
},
darwinDarkModeSupport: 'true',
// Electron-forge automatically adds the file extension based on OS
icon: './resources/icons/icon',
// The binary name should always be uppercase Zettlr. As we cannot specify
// this on a per-maker basis, we need to output everything this way. With
// this property, macOS builds are named Zettlr.app, Windows builds
// Zettlr.exe and the linux binaries are called Zettlr (albeit on Linux,
// lowercase is preferred). Due to the last issue (Linux binaries being
// with capital Z) we have to explicitly set executableName on the Linux
// target.
name: 'Zettlr',
// The certificate is written to the default keychain during CI build.
// See ./scripts/add-osx-cert.sh
osxSign: {
identity: 'Developer ID Application: Hendrik Erz (QS52BN8W68)',
'hardened-runtime': true,
'gatekeeper-assess': false,
entitlements: 'scripts/assets/entitlements.plist',
'entitlements-inherit': 'scripts/assets/entitlements.plist',
'signature-flags': 'library'
},
// Since electron-notarize 1.1.0 it will throw instead of simply print a
// warning to the console, so we have to actively check if we should
// notarize or not. We do so by checking for the necessary environment
// variables and set the osxNotarize option to false otherwise to prevent
// notarization.
osxNotarize: ('APPLE_ID' in process.env && 'APPLE_ID_PASS' in process.env)
? {
tool: 'notarytool',
appleId: process.env.APPLE_ID,
appleIdPassword: process.env.APPLE_ID_PASS,
teamId: 'QS52BN8W68'
}
: false,
extraResource: [
'resources/icons/icon.code.icns'
]
},
plugins: [
{
name: '@electron-forge/plugin-webpack',
config: {
mainConfig: './webpack.main.config.js',
// Since electron-forge v6.0.0-beta.58, this property controls the CSP
// for the development process. Since the defaults by electron-forge are
// not suitable for our needs (since they prevent the usage of our
// custom safe-file:// protocol), we must manually set this. Here we are
// basically copying the CSP from the HTML-files, but with 'unsafe-eval'
// added (which webpack needs for the sourcemaps).
devContentSecurityPolicy: "script-src 'self' 'unsafe-inline' 'unsafe-eval'",
// The default port for php-fpm is 9000, and since forge and PHP will
// collide on every system on which PHP is installed, we change the
// default ports for both the logger and the dev servers. We have to set
// both ports, because changing only one doesn't solve the issue.
port: 3000,
loggerPort: 9001,
renderer: {
config: './webpack.renderer.config.js',
entryPoints: [
{
html: './static/index.htm',
js: './source/win-main/index.ts',
name: 'main_window',
preload: {
js: './source/common/modules/preload/index.ts'
}
},
{
html: './static/index.htm',
js: './source/win-print/index.ts',
name: 'print',
preload: {
js: './source/common/modules/preload/index.ts'
}
},
{
html: './static/index.htm',
js: './source/win-log-viewer/index.ts',
name: 'log_viewer',
preload: {
js: './source/common/modules/preload/index.ts'
}
},
{
html: './static/index.htm',
js: './source/win-preferences/index.ts',
name: 'preferences',
preload: {
js: './source/common/modules/preload/index.ts'
}
},
{
html: './static/index.htm',
js: './source/win-tag-manager/index.ts',
name: 'tag_manager',
preload: {
js: './source/common/modules/preload/index.ts'
}
},
{
html: './static/index.htm',
js: './source/win-paste-image/index.ts',
name: 'paste_image',
preload: {
js: './source/common/modules/preload/index.ts'
}
},
{
html: './static/index.htm',
js: './source/win-error/index.ts',
name: 'error',
preload: {
js: './source/common/modules/preload/index.ts'
}
},
{
html: './static/index.htm',
js: './source/win-about/index.ts',
name: 'about',
preload: {
js: './source/common/modules/preload/index.ts'
}
},
{
html: './static/index.htm',
js: './source/win-stats/index.ts',
name: 'stats',
preload: {
js: './source/common/modules/preload/index.ts'
}
},
{
html: './static/index.htm',
js: './source/win-assets/index.ts',
name: 'assets',
preload: {
js: './source/common/modules/preload/index.ts'
}
},
{
html: './static/index.htm',
js: './source/win-update/index.ts',
name: 'update',
preload: {
js: './source/common/modules/preload/index.ts'
}
},
{
html: './static/index.htm',
js: './source/win-project-properties/index.ts',
name: 'project_properties',
preload: {
js: './source/common/modules/preload/index.ts'
}
},
{
html: './static/index.htm',
js: './source/win-splash-screen/index.ts',
name: 'splash_screen',
preload: {
js: './source/common/modules/preload/index.ts'
}
}
]
}
}
},
// When building for production, turn off a few fuses that disable certain
// debug controls of the app.
...((process.env.NODE_ENV === 'production')
? [new FusesPlugin({
version: FuseVersion.V1,
[FuseV1Options.RunAsNode]: false,
[FuseV1Options.EnableCookieEncryption]: true,
[FuseV1Options.EnableNodeOptionsEnvironmentVariable]: false,
[FuseV1Options.EnableNodeCliInspectArguments]: false,
[FuseV1Options.GrantFileProtocolExtraPrivileges]: true
})]
: [])
],
makers: [
{
name: '@electron-forge/maker-deb',
config: {
options: {
name: 'zettlr',
bin: 'Zettlr', // See packagerConfig.name property,
categories: [ 'Office', 'Education', 'Science' ],
section: 'editors',
// size: 500, // NOTE: Estimate, need to refine
description: 'Your one-stop publication workbench.',
productDescription: 'Your one-stop publication workbench.',
recommends: [ 'quarto', 'pandoc', 'texlive | texlive-base | texlive-full' ],
genericName: 'Markdown Editor',
// Electron forge recommends 512px
icon: './resources/icons/png/512x512.png',
priority: 'optional',
mimeType: [ 'text/markdown', 'application/x-tex', 'application/json', 'application/yaml' ],
maintainer: 'Hendrik Erz',
homepage: 'https://www.zettlr.com'
}
}
},
{
name: '@electron-forge/maker-rpm',
config: {
options: {
name: 'zettlr',
bin: 'Zettlr', // See packagerConfig.name property,
categories: [ 'Office', 'Education', 'Science' ],
description: 'Your one-stop publication workbench.',
productDescription: 'Your one-stop publication workbench.',
productName: 'Zettlr',
genericName: 'Markdown Editor',
// Electron forge recommends 512px
icon: './resources/icons/png/512x512.png',
license: 'GPL-3.0',
mimeType: [ 'text/markdown', 'application/x-tex', 'application/json', 'application/yaml' ],
homepage: 'https://www.zettlr.com'
}
}
},
{
name: '@electron-forge/maker-zip'
}
]
}