-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
583 lines (518 loc) · 15.8 KB
/
gulpfile.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
const Path = require('path');
const CP = require('child_process');
const OS = require('os');
const FSP = require('fs').promises;
const {promisify} = require('util');
const manifest = require('./package.json');
const {src, dest, series, parallel, watch: gulpWatch} = require('gulp');
const exec = promisify(CP.exec);
const esbuild = require('esbuild');
const isDevelopment = String(process.env.NODE_ENV).toLowerCase() !== 'production';
/**
* -production: removes development only code branches and enables minification
* enabled by default when "package" task is run
* -platform: win, linux, mac (separate by comma)
* -arch: currently only x64
* -target: use comma to build multiple targets. supported:
* win: installer, portable
* mac: dmg
* linux: AppImage
*/
const argvDefaults = {
production: !isDevelopment,
platform: undefined,
arch: undefined,
target: undefined,
userData: 'userData',
};
const argv = require('minimist')(process.argv.slice(2), {default: argvDefaults});
/**
* Build config.
*/
const ENV = {
NODE_ENV: argv.production ? 'production' : 'development',
};
if (!argv.production) ENV.DROVP_FORCED_USER_DATA_PATH = Path.join(process.cwd(), argv.userData);
const PATHS = {
assets: ['src/windows/*/*.html', 'src/assets/**/*', `!**/*.icns`],
build: 'build',
out: 'out',
scripts: 'src/**/*.ts(|x)',
styles: 'src/**/*.sass',
themesFile: Path.join(__dirname, 'src', 'config', 'themes.js'),
};
// Extract only stuff the application runtime cares about
const cleanManifest = (manifest) => ({
name: manifest.name,
productName: manifest.productName,
description: manifest.description,
version: manifest.version,
binariesVersion: manifest.binariesVersion,
electronVersion: manifest.electronVersion,
date: manifest.date,
homepage: manifest.homepage,
repository: manifest.repository,
discussions: manifest.discussions,
bugs: manifest.bugs,
author: manifest.author,
main: manifest.main,
licenses: manifest.licenses,
});
async function scripts() {
// Load manifest and create a plugin that intercepts it and cleans it up
const rawManifestContents = await FSP.readFile('package.json', {encoding: 'utf8'});
const manifest = cleanManifest(JSON.parse(rawManifestContents));
const manifestContents = JSON.stringify(manifest, null, '\t');
const cleanupManifestPlugin = {
name: 'env',
setup(build) {
// Intercept manifest import path so esbuild doesn't attempt
// to map them to a file system location. Tag them with the "manifest"
// namespace to reserve them for this plugin.
build.onResolve({filter: /^manifest$/}, (args) => ({
path: args.path,
namespace: 'manifest',
}));
// Load paths tagged with the "manifest" namespace and behave as if
// they point to a JSON file containing the environment variables.
build.onLoad({filter: /.*/, namespace: 'manifest'}, async () => ({
contents: manifestContents,
loader: 'json',
}));
},
};
const define = Object.keys(ENV).reduce(
(map, key) => ({
...map,
[`process.env.${key}`]: typeof ENV[key] === 'string' ? `'${ENV[key].replaceAll('\\', '\\\\')}'` : ENV[key],
}),
{}
);
return esbuild.build({
entryPoints: [
'src/main.ts',
'src/thread.ts',
'src/update.ts',
'src/dynamic/marked.ts',
'src/dynamic/pluginTemplate.ts',
'src/windows/main/index.tsx',
],
external: [
'electron',
'buffer',
'util',
'fs',
'http',
'https',
'path',
'child_process',
'os',
'module',
'stream',
'zlib',
'assert',
],
plugins: [cleanupManifestPlugin],
define: define,
format: 'cjs',
target: ['node18.7.1'],
logLevel: 'warning',
bundle: true,
minify: ENV.NODE_ENV === 'production',
// Sourcemaps are useless atm. All errors are reported from wrong places.
// Might be electron dev tools getting confused or something.
// sourcemap: ENV.NODE_ENV === 'production' ? false : 'inline',
outdir: PATHS.build,
outbase: 'src',
});
}
function styles() {
const sassGlob = require('gulp-sass-glob');
const postcss = require('gulp-postcss');
const sass = require('gulp-dart-sass');
const sassOptions = {
includePaths: ['src'],
};
// Delete themes file from require cache so that it gets reloaded
delete require.cache[PATHS.themesFile];
/** @type any[] */
const postCssPlugins = [];
if (ENV.NODE_ENV === 'production') postCssPlugins.push(require('postcss-prune-var')());
// @ts-ignore
postCssPlugins.push(require('postcss-preset-env')({stage: 0, browsers: 'chrome 89'}));
postCssPlugins.push(require('postcss-declarations')(require(PATHS.themesFile)));
if (ENV.NODE_ENV === 'production') postCssPlugins.push(require('cssnano')({preset: 'default'}));
return src('src/windows/*/*.sass', {base: 'src'})
.pipe(sassGlob())
.pipe(sass(sassOptions).on('error', sass.logError))
.pipe(postcss(postCssPlugins))
.pipe(dest(PATHS.build));
}
function cleanBuild() {
return require('del')(PATHS.build);
}
function cleanOut() {
return require('del')(PATHS.out);
}
function assets() {
const editJson = require('gulp-json-editor');
return src('package.json')
.pipe(
editJson({
date: new Date().toISOString(),
devDependencies: undefined,
dependencies: undefined,
scripts: undefined,
config: undefined,
})
)
.pipe(src(PATHS.assets, {base: 'src'}))
.pipe(dest(PATHS.build));
}
async function watch() {
const {spawn} = require('child_process');
// Set paths that should be used from now on
let appRoot;
let appExecutable;
switch (process.platform) {
case 'win32':
appRoot = Path.join(process.cwd(), PATHS.out, `win-unpacked`);
appExecutable = Path.join(appRoot, `${manifest.productName}.exe`);
PATHS.build = Path.join(appRoot, 'resources', 'app');
break;
case 'linux':
appRoot = Path.join(process.cwd(), PATHS.out, `linux-unpacked`);
appExecutable = Path.join(appRoot, manifest.name);
PATHS.build = Path.join(appRoot, 'resources', 'app');
break;
default:
throw new Error(`Development environment not set up for ${process.platform}.`);
}
try {
await FSP.stat(appExecutable);
} catch (error) {
console.log(`App executable missing:`, appExecutable);
}
// Styles
gulpWatch([PATHS.styles, PATHS.themesFile], styles);
// Assets
// @ts-ignore
const assetsWatcher = gulpWatch(PATHS.assets, {events: ['add', 'change']}, assets);
assetsWatcher.on('unlink', (path) => {
require('del')(String(path).replace(/^src/, PATHS.build));
});
// Scripts
gulpWatch(PATHS.scripts, scripts);
// Start the app
let app;
console.log('starting the app...');
start();
function start() {
app = spawn(appExecutable, {shell: true, cwd: appRoot});
app.stdout.on('data', (data) => console.log(data.toString()));
app.stderr.on('data', (data) => console.error(data.toString()));
app.on('close', (code) => {
console.log(`app exited with code ${code}, restarting...`);
start();
});
}
}
async function packageDev() {
const currentArch = process.arch;
if (isValidArch(currentArch)) {
await _package('dir', currentArch);
} else {
throw new Error(`Current arch "${currentArch}" is not supported.`);
}
}
/**
* Packages app into requested or default targets for CURRENT platform.
* Building for platforms other than current is not supported.
*/
async function package() {
/**
* array of [arch, target] tuples
* @type [Target, Arch][]
* */
const packages = [];
const os = osFromPlatform();
// Force production environment
ENV.NODE_ENV = 'production';
delete ENV.DROVP_FORCED_USER_DATA_PATH;
// Ensure out exists
await FSP.mkdir(PATHS.out, {recursive: true});
// Decide what targets & archs need to be packaged
/** @type {Target[]} */
let targets = [];
/** @type {Arch[]} */
let archs = [];
/** @type {string[]} */
const argvTargets =
argv.target
?.split(',')
.map((x) => x.trim())
.filter((x) => !!x) || [];
/** @type {string[]} */
const argvArchs =
argv.arch
?.split(',')
.map((x) => x.trim())
.filter((x) => !!x) || [];
for (const target of argvTargets) {
if (isValidTarget(target)) targets.push(target);
else throw new Error(`"${target}" is not a valid target for current platform.`);
}
for (const arch of argvArchs) {
if (isValidArch(arch)) archs.push(arch);
else throw new Error(`"${arch}" is not a valid arch for current platform.`);
}
// Populate defaults
switch (os) {
case 'win':
if (archs.length === 0) archs.push('x64');
if (targets.length === 0) targets.push('nsis', 'portable');
break;
case 'linux':
if (archs.length === 0) archs.push('x64');
if (targets.length === 0) targets.push('AppImage');
break;
case 'mac':
if (archs.length === 0) archs.push('x64', 'arm64');
if (targets.length === 0) targets.push('dmg');
break;
}
for (const target of targets) {
for (const arch of archs) packages.push([target, arch]);
}
// Build core
console.log(`Building core...`);
await runTask(build);
// Package core app files
console.log(`Packaging core...`);
await archive(await listFiles(PATHS.build), Path.join(PATHS.out, `${manifest.productName}-core.7z`), '7z');
// Package
for (const [target, arch] of packages) {
await _package(target, arch);
// 7zip dmg contents
if (target === 'dmg') {
const contentsFolderPath = Path.join(PATHS.out, 'mac', `${manifest.productName}.app`, 'Contents');
const packgeName = `${manifest.productName}-contents-${os}-${arch}.7z`;
const resultArchivePath = Path.join(PATHS.out, packgeName);
console.log(`Packaging dmg contents: ${packgeName}`);
await archive(contentsFolderPath, resultArchivePath, '7z');
}
}
}
/**
* @param {Target} target
* @param {Arch} arch Supported: win: x64, linux+mac: x64, arm64
*/
async function _package(target, arch = 'x64') {
const os = osFromPlatform();
const builder = require('electron-builder');
const Platform = builder.Platform[{win: 'WINDOWS', linux: 'LINUX', mac: 'MAC'}[os]];
const binariesRoot = `bin/${os}/${arch}`;
let extraFiles = undefined;
const logMessage = `Packaging: ${os} ${target} ${arch}`;
const underline = logMessage
.split('')
.map((x) => '-')
.join('');
console.log(`${logMessage}\n${underline}`);
// I don't even know how electron-builder's native portable mode is
// intended to work. Playing with it produced some weird package that
// extracted itself to tmp, and deleted itself on exit... ?
// So I use zip instead, and portability logic is implemented internally
// through checking if userData folder exists under app root.
if (target === 'portable') {
if (os !== 'win') throw new Error(`Portable target is only supported on windows.`);
const keepMePath = Path.join('userData', 'KEEPME');
await FSP.mkdir('userData', {recursive: true});
await FSP.writeFile(
keepMePath,
'This file ensures the `userData` directory is not deleted during packaging, as its existence switches Drovp into portable mode.'
);
extraFiles = {
from: 'userData',
to: 'userData',
filter: ['KEEPME'],
};
target = 'zip';
}
// Ensure binaries are marked as executable
for (const file of await FSP.readdir(binariesRoot)) {
await FSP.chmod(Path.join(binariesRoot, file), '777');
}
const result = await builder.build({
targets: Platform.createTarget(target),
config: {
appId: `app.${manifest.name}`,
productName: manifest.productName,
artifactName: `${manifest.productName}-${arch}.\${ext}`,
copyright: `Copyright © ${new Date().getFullYear()} ${manifest.productName}`,
directories: {
app: PATHS.build,
output: PATHS.out,
},
extraResources: {
from: binariesRoot,
to: 'bin',
filter: ['**/*'],
},
extraFiles,
publish: [], // disables auto-publishing in github actions
nsis: {
uninstallDisplayName: manifest.productName,
},
win: {
publish: [], // disables auto-publishing in github actions
target: [{target, arch}],
icon: 'src/assets/logo.png',
},
linux: {
publish: [], // disables auto-publishing in github actions
target: [{target, arch}],
icon: 'src/assets/logo.png',
},
mac: {
publish: [], // disables auto-publishing in github actions
target: [
{target, arch},
{target: 'zip', arch},
],
category: 'public.app-category.developer-tools',
icon: 'src/assets/app.icns',
darkModeSupport: true,
},
asar: false,
protocols: [{name: manifest.productName, schemes: [manifest.name]}],
},
});
console.log('result', result);
}
/**
* Package binaries.
*/
async function binaries() {
await FSP.mkdir(PATHS.out, {recursive: true});
const combos = [
['win', 'x64'],
['mac', 'x64'],
['mac', 'arm64'],
];
for (const [os, arch] of combos) {
const outputPath = Path.join(PATHS.out, `${manifest.productName}-binaries-${os}-${arch}.7z`);
await archive(`bin/${os}/${arch}/*`, outputPath, '7z');
}
}
/**
* Check that all packages are built.
*/
async function check() {
const files = [
// Mac
`${manifest.productName}-arm64.dmg`,
`${manifest.productName}-x64.dmg`,
`${manifest.productName}-contents-mac-arm64.7z`,
`${manifest.productName}-contents-mac-x64.7z`,
// Linux
`${manifest.productName}-x64.AppImage`,
// Windows
`${manifest.productName}-x64.exe`,
`${manifest.productName}-x64.zip`,
// Can be build on any platform
`${manifest.productName}-core.7z`,
`${manifest.productName}-binaries-mac-arm64.7z`,
`${manifest.productName}-binaries-mac-x64.7z`,
`${manifest.productName}-binaries-win-x64.7z`,
];
// Check local
for (const file of files) {
try {
await FSP.access(Path.join('packages', file));
} catch {
console.error(`Missing: ${file}`);
}
}
}
const build = series(cleanBuild, parallel(assets, styles, scripts));
exports.clean = cleanBuild;
exports.scripts = scripts;
exports.styles = styles;
exports.assets = assets;
exports.watch = watch;
exports.build = build;
exports.check = check;
exports.binaries = binaries;
exports.package = package;
exports.default = series(build, cleanOut, packageDev, watch);
/**
* Helpers.
*/
/** @typedef {'win' | 'linux' | 'mac'} OS */
/** @typedef {'nsis' | 'zip' | 'AppImage' | 'dmg' | 'portable' | 'dir'} Target */
/** @typedef {'x64' | 'arm64'} Arch */
/**
* Run any gulp task manually.
*/
function runTask(task) {
return new Promise((resolve, reject) => {
series(task)((error, result) => {
if (error) reject(error);
else resolve(result);
});
});
}
/**
* @type {(name: string) => name is Target}
*/
function isValidTarget(name) {
switch (process.platform) {
case 'win32':
return ['nsis', 'portable', 'dir'].includes(name);
case 'linux':
return ['AppImage', 'dir'].includes(name);
case 'darwin':
return ['dmg', 'dir'].includes(name);
}
return false;
}
/**
* @type {(name: string) => name is Arch}
*/
function isValidArch(name) {
switch (process.platform) {
case 'win32':
return ['x64'].includes(name);
case 'linux':
return ['x64'].includes(name);
case 'darwin':
return ['x64', 'arm64'].includes(name);
}
return false;
}
/** @type {(platform?: string) => OS} */
const osFromPlatform = (platform = process.platform) => ({win32: 'win', linux: 'linux', darwin: 'mac'}[platform]);
/**
* List full paths to files inside a directory.
*/
async function listFiles(directoryPath) {
const files = await FSP.readdir(directoryPath);
return files.map((file) => Path.join(directoryPath, file));
}
/**
* Zip folder or a file;
*
* ```
* await archive('path/to/dir/or/file/to/zip', 'path/to/output/archive.zip');
* await archive('path/to/dir/contents/*', 'archive.zip');
* await archive('path/to/file.ext', 'archive.zip');
* await archive(['multiple.exe', 'files.png'], 'archive.zip');
* ```
*/
async function archive(input, outputPath, format = 'zip') {
await FSP.rm(outputPath, {recursive: true, force: true});
const inputs = (Array.isArray(input) ? input : [input]).map((path) => Path.resolve(path));
const binPath = `./bin/${osFromPlatform(process.platform)}/x64/7za${process.platform === 'win32' ? '.exe' : ''}`;
await exec(`"${binPath}" a -t${format} -mx9 -snl "${outputPath}" ${inputs.map((input) => `"${input}"`).join(' ')}`);
}