Repository navigation
Expand file tree
/
Copy pathbuild.js
More file actions
92 lines (83 loc) · 2.55 KB
/
Copy pathbuild.js
File metadata and controls
92 lines (83 loc) · 2.55 KB
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
var fs = require("fs");
var babel = require("@babel/core");
function wrap(code) {
return `(() => {\n${code}\n})();`;
}
(async () => {
// Manifest
var manifest = null;
console.log("Loading manifest...");
if (fs.existsSync("src/manifest.json")) {
manifest = JSON.parse(fs.readFileSync("src/manifest.json").toString("utf-8"));
console.log(manifest);
} else {
console.warn("Manifest doesn't exist!");
}
if (!manifest.modid) {
return console.error("Manifest must have a mod ID!");
}
// Bundle all files together
var mainFile = (manifest ? manifest.main : undefined) || "mod.js";
var code = wrap(fs.readFileSync(`src/${mainFile}`).toString("utf-8"));
function addFile(path) {
if (path == `src/${mainFile}` || path == "src/manifest.json" || path == "src/icon.png") {
return;
}
if (fs.statSync(path).isDirectory()) {
for (var file of fs.readdirSync(path)) {
if (file.startsWith(".")) {
continue;
}
addFile(path + "/" + file);
}
} else if (path.startsWith("src/textures/")) {
code += `\n${wrap(`ShyFog.clientOnly(() => ShyFog.Client.saveTexture("/mods/${manifest.modid || "unknown-mod"}/${path.slice(13)}", "data:image/png;base64,${fs.readFileSync(path).toString("base64")}"));`)}`;
} else {
code += `\n${wrap(fs.readFileSync(path).toString("utf-8"))}`;
}
}
addFile("src");
// Minify
if (manifest && manifest.minify) {
code = babel.transformSync(code, {
"presets": [
["minify", {
"keepClassName": true
}]
],
"comments": false
}).code.split("\n").join("\\n");
}
if (!manifest) {
// Save raw code without .sfm format
return fs.writeFileSync("unknown-mod.js", code);
}
// Default manifest values
if (!manifest.name) {
manifest.name = manifest.modid;
}
if (!manifest.description) {
manifest.description = "";
}
if (!manifest.version) {
manifest.version = "v0.0.1";
}
if (!manifest.output) {
manifest.output = `${manifest.name}-${manifest.version}.sfm`;
}
var outputFile = manifest.output;
delete manifest.main;
delete manifest.minify;
delete manifest.output;
// Icon
if (fs.existsSync("src/icon.png")) {
manifest.icon = "data:image/png;base64," + fs.readFileSync("src/icon.png").toString("base64");
}
// Package the mod into SFM format
code = Object.assign(manifest, { code });
code = JSON.stringify(code);
code = Buffer.from(code).toString("base64");
// Save the mod
fs.writeFileSync(outputFile, code);
console.log(`Saved ${outputFile}`);
})();