-
Notifications
You must be signed in to change notification settings - Fork 56
/
gulpfile.js
428 lines (385 loc) · 11.2 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
/* eslint-disable @typescript-eslint/no-var-requires */
const fs = require("fs-extra");
const gulp = require("gulp");
const builder = require("electron-builder");
const tar = require("tar-fs");
const zlib = require("zlib");
const { parallel, series } = require("gulp");
const { installExtensions, buildExtensions, watchExtensions } = require("./gulpfile.extensions");
const { execute } = require("./gulpfile.util");
const { execSync } = require('child_process');
const { promisify } = require("util");
const esbuild = require("esbuild");
// Promisify the pipeline function
const pipeline = promisify(require("stream").pipeline);
const packageJson = JSON.parse(fs.readFileSync("./package.json", { encoding: 'utf-8' }));
const config = {
buildVersion: Date.now().toString(),
publish: !!process.env.PUBLISH,
isRelease: process.env.NODE_ENV === "production",
isStaticInstall: packageJson.config.installed,
static: {
src: "./static",
dest: "./build",
},
main: {
src: "./src/main",
},
sevenZip: "./extern/7zip-bin",
back: {
src: "./src/back",
},
};
// Copy extensions after packing
const extraResources = [
{
from: "./extensions",
to: "./extensions",
filter: ["!**/node_modules/**", "!**/.git/**"],
},
];
// Files to copy after packing
const copyFiles = [
{
from: "./extern/utils",
to: "./extern/utils",
filter: ["**"],
},
{
from: "./extern/bluezip",
to: "./extern/bluezip",
filter: ["**"],
},
{
// Only copy 7zip execs for packed platform
from: "./extern/7zip-bin",
to: "./extern/7zip-bin",
filter: ["${os}/**/*"],
},
{
from: "./extern/elevate",
to: "./extern/elevate",
filter: ["**"],
},
"./lang",
"./licenses",
"ormconfig.json",
{
from: "./LICENSE",
to: "./licenses/LICENSE",
},
{
// Copy the OS specific upgrade file
from: "./upgrade/${os}.json",
to: "./upgrade.json",
},
];
// Options to append when releasing
const extraOptions = {
win: {
target: [
{
target: "nsis-web",
arch: ["x64", "ia32"],
},
{
target: "7z",
arch: ["x64", "ia32"],
},
],
icon: "./icons/icon.ico",
},
mac: {
target: ["dmg", "7z"],
icon: "./icons/icon.icns",
protocols: {
name: "flashpoint-protocol",
schemes: ["flashpoint"],
},
},
linux: {
target: ["deb", "7z"],
category: "games",
},
};
// Publish info for electron builder
const publishInfo = [
{
provider: "github",
vPrefixedTagName: false,
},
];
/* - Cross Arch Deps - */
function installCrossDeps(done) {
console.log('Checking for installed cross-platform packages...');
// Get existing version of FP Archive
const packageLock = JSON.parse(fs.readFileSync('./package-lock.json', { encoding: 'utf-8' }));
const fpa = packageLock.packages['node_modules/@fparchive/flashpoint-archive'];
const platform = process.env.PACK_PLATFORM || process.platform;
const arch = process.env.PACK_ARCH || process.arch;
console.log(`Platform: ${platform} - Arch: ${arch}`);
const packageName = Object.keys(fpa.optionalDependencies).find(p => p.includes(`${platform}-${arch}`));
if (!packageName) {
console.log('No package found for this platform and arch combination, skipping...');
done();
return;
}
// List installed deps for fparchive
const packageLocation = 'node_modules/' + packageName;
const badPackages = fs.readdirSync('./node_modules/@fparchive/', { withFileTypes: true }).filter(m => m.isDirectory() && m.name !== 'flashpoint-archive' && ('@fparchive/' + m.name) !== packageName);
// Remove old packages
for (const bp of badPackages) {
console.log(`Removing: ${bp.path + bp.name}`);
fs.removeSync(bp.path + bp.name);
}
try {
// Check if required version already exists and exit early if matches version needed
const existingInfo = JSON.parse(fs.readFileSync(packageLocation + '/package.json', { encoding: 'utf-8' }));
if (existingInfo.version === fpa.version) {
// Already exists, up to date
done();
return;
} else {
console.log(`Removed old version (${existingInfo.version})`);
// Wrong version, delete and replace
fs.removeSync(packageLocation);
}
} catch {
// Pacakge not installed, carry on
}
const packageFilename = `fparchive-${packageName.split('/')[1]}-${fpa.version}.tgz`;
execSync(`npm pack ${packageName}@${fpa.version}`);
console.log('Unpacking ' + packageFilename);
// Extract and move all files to new folder
extractTarball(packageFilename, "./package-extract")
.then(() => {
fs.removeSync(packageFilename);
fs.mkdirSync(`./node_modules/${packageName}`, { recursive: true });
for (const file of fs.readdirSync('./package-extract/package/')) {
fs.moveSync('./package-extract/package/' + file, packageLocation + '/' + file);
}
fs.removeSync('./package-extract');
console.log(`Installed ${packageName}@${fpa.version}`);
done();
});
}
/* ------ Watch ------ */
function watchBack(done) {
execute("npx swc --strip-leading-paths --no-swcrc --config-file swcrc.back.dev.json --source-maps true -d build src --watch", done);
}
async function watchRenderer() {
const ctx = await esbuild.context({
bundle: true,
entryPoints: ['./src/renderer/index.tsx'],
outdir: './build/window',
minify: true,
outExtension: {
'.js': '.bundle.js'
},
external: ['electron', ...require('module').builtinModules],
});
return ctx.watch();
}
function watchStatic() {
gulp.watch(config.static.src + "/**/*", buildStatic);
}
/* ------ Build ------ */
function buildBack(done) {
execute("npx swc --strip-leading-paths --no-swcrc --config-file swcrc.back.prod.json -d build src", done);
}
async function buildRenderer() {
return esbuild.build({
bundle: true,
entryPoints: ['./src/renderer/index.tsx'],
outdir: './build/window',
minify: true,
outExtension: {
'.js': '.bundle.js'
},
external: ['electron', ...require('module').builtinModules],
});
}
function buildStatic() {
return gulp
.src(config.static.src + "/**/*")
.pipe(gulp.dest(config.static.dest));
}
function configVersion(done) {
fs.writeFile(".version", config.buildVersion, done);
}
/* ----- Version ---- */
function createVersionFile(done) {
// Get the current date
const currentDate = new Date();
// Get the year, month, and day components
const year = currentDate.getFullYear();
// JavaScript months are 0-indexed, so we add 1 to get the actual month
const month = (currentDate.getMonth() + 1).toString().padStart(2, '0');
const day = currentDate.getDate().toString().padStart(2, '0');
// Create the formatted date string in "YYYY-MM-DD" format
const formattedDate = `${year}-${month}-${day}`;
// Get Git Commit Hash
const gitCommitHash = execSync('git rev-parse --short HEAD', { encoding: 'utf-8' }).trim();
const data = `export const VERSION = '${formattedDate} (${gitCommitHash})';\n`
// Write to src/shared/version.ts
fs.writeFile('src/shared/version.ts', data, (err) => {
if (err) {
throw `Error writing to version.ts: ${err}`;
} else {
console.log(`Build ver: "${formattedDate} (${gitCommitHash})"`);
done();
}
});
}
/* ------ Pack ------ */
function nexusPack(done) {
const files = ["./build"];
// Forcefully include ia32 library since nexus builds ia32
if (process.platform === 'win32') {
files.push({
from: '../../node_modules/@fparchive/flashpoint-archive-win32-ia32-msvc',
to: './node_modules/@fparchive/flashpoint-archive-win32-ia32-msvc',
filter: ["**/*"]
});
}
builder
.build({
targets: builder.Platform.WINDOWS.createTarget(),
config: Object.assign(
{
appId: "com.bluemaxima.flashpoint-launcher",
productName: "Flashpoint",
directories: {
buildResources: "./static/",
output: "./dist/",
},
files: files,
extraFiles: copyFiles, // Files to copy to the build folder
extraResources: extraResources, // Copy System Extensions
compression: "maximum", // Only used if a compressed target (like 7z, nsis, dmg etc.)
asar: true,
artifactName: "${productName}.${ext}",
win: {
target: [
{
target: "zip",
arch: "ia32",
}
],
icon: "./icons/icon.ico",
}
}
),
})
.then(() => {
console.log("Pack - Done!");
})
.catch((error) => {
console.log("Pack - Error!", error);
})
.finally(done);
}
function pack(done) {
const publish = config.publish ? publishInfo : []; // Uses Git repo for unpublished builds
const extraOpts = config.publish ? extraOptions : {};
builder
.build({
ia32: process.env.PACK_ARCH === "ia32" || undefined,
x64: process.env.PACK_ARCH === "x64" || undefined,
config: Object.assign(
{
appId: "com.bluemaxima.flashpoint-launcher",
productName: "Flashpoint",
directories: {
buildResources: "./static/",
output: "./dist/",
},
files: ["./build"],
extraFiles: copyFiles, // Files to copy to the build folder
extraResources: extraResources, // Copy System Extensions
compression: "maximum", // Only used if a compressed target (like 7z, nsis, dmg etc.)
target: "dir",
asar: true,
publish: publish,
artifactName: "${productName}-${version}_${os}-${arch}.${ext}",
win: {
target: "dir",
icon: "./icons/icon.ico",
},
mac: {
target: "dir",
icon: "./icons/icon.icns",
},
linux: {
target: "dir",
},
},
extraOpts
),
})
.then(() => {
console.log("Pack - Done!");
})
.catch((error) => {
console.log("Pack - Error!", error);
})
.finally(done);
}
/* ------ Clean ------ */
function clean(done) {
fs.remove("./dist", () => {
fs.remove("./build", done);
});
}
/* ------ Util ------ */
async function extractTarball(inputFilePath, outputDirectory) {
try {
// Create a readable stream from the input file
const readStream = fs.createReadStream(inputFilePath);
// Pipe the readable stream through zlib.createGunzip() and then through tar.extract()
await pipeline(
readStream,
zlib.createGunzip(),
tar.extract(outputDirectory)
);
console.log('Extraction complete.');
} catch (error) {
console.error('Extraction failed:', error);
}
}
/* ------ Meta Tasks ------*/
exports.clean = series(clean);
exports.build = series(
clean,
createVersionFile,
installCrossDeps,
parallel(
buildBack,
buildRenderer,
buildExtensions,
buildStatic,
configVersion
)
);
exports.watch = series(
clean,
createVersionFile,
installCrossDeps,
parallel(
watchBack,
watchRenderer,
watchExtensions,
buildStatic,
watchStatic
)
);
exports.pack = series(
pack
);
exports.nexusPack = series(
installExtensions,
buildExtensions,
nexusPack
);
exports.extInstall = series(installExtensions);
exports.esbuildTest = series(buildRenderer);