This repository has been archived by the owner on May 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake.js
75 lines (69 loc) · 1.94 KB
/
make.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
const fs = require("fs");
const readline = require("readline");
const luamin = require("luamin");
const logger = require("./logger");
const child = require("child_process");
const args = process.argv.slice(2);
let mode = "-d";
if (args[0] === "-p") {
mode = "-p";
}
const startMark = "--nef-inject";
const endMark = "--nef-inject-end";
function injectWC3(outLua, wc3path, iMode) {
if (!fs.existsSync(wc3path) || !fs.statSync(wc3path).isFile()) {
logger.error(`File not found ${wc3path}`);
return;
}
let file = fs.readFileSync(outLua).toString();
if (iMode === "-p") {
file = luamin.minify(file);
}
const ri = readline.createInterface({
input: fs.createReadStream(wc3path),
});
let state = 0;
let outFile = "";
let changed = false;
ri.on("line", function (line) {
if (line.trim() === endMark) {
outFile += file + "\n";
state = 0;
}
if (state === 2) {
return;
}
if (state === 0 && line === "function main()") {
state = 1;
changed = true;
}
if (state === 1 && line === "end") {
outFile += startMark + "\n";
outFile += file + "\n";
outFile += endMark + "\n";
state = 0;
}
if (line.trim() === startMark) {
state = 2;
changed = true;
}
outFile += line + "\n";
});
ri.on("close", function () {
fs.writeFileSync(wc3path, outFile);
if (changed) {
logger.success("Write to war3map.lua success");
} else {
logger.error("Target file is not war3map.lua");
}
});
}
// ts to lua
try {
child.execSync(".\\node_modules\\.bin\\tstl -p .\\tsconfig.json", { stdio: "inherit" });
} catch (error) {
process.exit(1);
}
logger.success("Ts to Lua success");
// inject
injectWC3("./out.lua", "./EndlessFarm.w3x/war3map.lua", mode);