forked from Milkshiift/GoofCord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.mjs
76 lines (63 loc) · 2.13 KB
/
build.mjs
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
import { context } from "esbuild";
import fs from "fs";
import path from "path";
import * as readline from "readline";
await fs.promises.rm("ts-out", { recursive: true, force: true });
const NodeCommonOpts = {
minify: true,
bundle: true,
sourcemap: "external",
logLevel: "info",
format: "cjs",
platform: "node",
external: ["electron"],
target: ["esnext"],
entryPoints: await searchPreloadFiles("src", ["src/main.ts"]),
outdir: "ts-out"
};
const ctx = await context(NodeCommonOpts)
await ctx.rebuild()
await ctx.dispose()
deleteSourceMaps("./ts-out");
await fs.promises.cp('./assets/', './ts-out/assets', {recursive: true});
// Every preload file should be marked with "// RENDERER" on the first line so it's included
async function searchPreloadFiles(directory, result = []) {
const files = await fs.promises.readdir(directory);
for (const file of files) {
const filePath = path.join(directory, file);
const stats = await fs.promises.stat(filePath);
if (stats.isDirectory()) {
// Recursively search subdirectories
searchPreloadFiles(filePath, result);
} else {
if (await getFirstLine(filePath) === "// RENDERER") {
result.push(filePath);
}
}
}
return result;
}
async function deleteSourceMaps(directoryPath) {
const files = await fs.promises.readdir(directoryPath);
for (const file of files) {
const filePath = path.join(directoryPath, file);
const stats = await fs.promises.stat(filePath);
if (stats.isDirectory()) {
deleteSourceMaps(filePath); // Recursively delete files in subdirectory
} else if (file.endsWith('.map')) {
fs.promises.unlink(filePath);
}
}
}
async function getFirstLine(pathToFile) {
const readable = fs.createReadStream(pathToFile);
const reader = readline.createInterface({ input: readable });
const line = await new Promise((resolve) => {
reader.on('line', (line) => {
reader.close();
resolve(line);
});
});
readable.close();
return line;
}