-
Notifications
You must be signed in to change notification settings - Fork 4
/
build.js
79 lines (70 loc) · 1.82 KB
/
build.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
const fs = require("fs");
const path = require("path");
const esbuild = require("esbuild");
// build user game
function buildGame() {
const template = fs.readFileSync("template.html", "utf-8");
let code = "";
code += `<script src="/dist/helper.js"></script>\n`;
code += `<script src="/dist/game.js"></script>\n`;
try {
// build user code
esbuild.buildSync({
bundle: true,
sourcemap: true,
target: "es6",
keepNames: true,
logLevel: "silent",
entryPoints: ["code/main.js"],
outfile: "public/dist/game.js",
});
esbuild.buildSync({
bundle: true,
sourcemap: true,
target: "es6",
keepNames: true,
entryPoints: ["helper.ts"],
outfile: "public/dist/helper.js",
});
} catch (e) {
const loc = e.errors[0].location;
err = {
msg: e.errors[0].text,
stack: [
{
line: loc.line,
col: loc.column,
file: loc.file,
},
],
};
let msg = "";
msg += "<pre>";
msg += `ERROR: ${err.msg}\n`;
if (err.stack) {
err.stack.forEach((trace) => {
msg += ` -> ${trace.file}:${trace.line}:${trace.col}\n`;
});
}
msg += "</pre>";
fs.writeFileSync("public/index.html", msg);
return;
}
fs.writeFileSync("public/index.html", template.replace("{{kaboom}}", code));
}
function copyDir(src, dest) {
fs.mkdirSync(dest, { recursive: true });
const entries = fs.readdirSync(src, { withFileTypes: true });
for (let entry of entries) {
const srcPath = path.join(src, entry.name);
const destPath = path.join(dest, entry.name);
if (entry.isDirectory()) {
copyDir(srcPath, destPath);
} else {
fs.copyFileSync(srcPath, destPath);
}
}
}
buildGame();
copyDir("sprites", "public/sprites");
copyDir("sounds", "public/sounds");