-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
186 lines (151 loc) · 5.23 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
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
const fs = require('fs-extra');
const jassToTs = require('./node_modules/convertjasstots/dist/jassParser');
const typescriptToLua = require('typescript-to-lua');
const ts = require("typescript");
const {execSync} = require('child_process');
const minimist = require('minimist');
let reportDiagnostic = typescriptToLua.createDiagnosticReporter(true);
const helptext = `
Usage: build.js [options]
options:
-b, --build Build the project
-j, --jass Generate typescript declarations
-r, --run Run the map in Warcraft 3
-h, --help Shows this help menu
`;
class Build {
constructor(args) {
this.os = process.platform;
if (args['help'] || args['h']) {
console.log(helptext);
process.exit(1)
}
this.doTasks(args)
}
async doTasks(args) {
if (args['jass'] || args['j']) {
await this.generateTs()
}
if (args['build'] || args['b']) {
await this.build()
}
if (args['run'] || args['r']) {
this.run()
}
}
env() {
const dir = './config';
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
if (!fs.existsSync(`${dir}/warcraft.json`)) {
console.log('missing file');
fs.writeFileSync(`${dir}/warcraft.json`, JSON.stringify({
path: '',
}))
}
this.settings = JSON.parse(fs.readFileSync(`${dir}/warcraft.json`));
if (!this.settings.path.length > 0) {
console.error('Path to wc3 is not setup, please set it in ./config/warcraft.json');
process.exit(1)
}
}
nativeExecute(cmdline) {
execSync(cmdline, (err, stdout, stderr) => {
if (err) {
throw err;
}
return false;
});
return true;
}
async build() {
this.cleanup();
var map = 'map.w3x';
fs.mkdirSync('target');
fs.copySync(`maps/${map}`, `target/${map}`);
let sharedArgs = `extract "target/${map}" "war3map.lua" "maps/map"`;
let mpqEditor = '';
if (this.os === "win32") {
mpqEditor = 'tools/MPQEditor/x64/MPQEditor.exe';
} else {
mpqEditor = "WINEDEBUG=-all wine64 tools/MPQEditor/x64/MPQEditor.exe";
}
this.nativeExecute(`${mpqEditor} ${sharedArgs}`);
const {emitResult, diagnostics} = typescriptToLua.transpileProject('tsconfig.json');
for (let diag of diagnostics) {
console.log(diag.messageText);
if (diag.code != 2306) {
console.error('FATAL ERROR IN TYPESCRIPT');
console.error(diag);
throw diag;
}
}
emitResult.forEach(({name, text}) => ts.sys.writeFile(name, text));
fs.copySync(`src/app/src/main.lua`, `src/main.lua`);
sharedArgs = `build "map"`;
let ceres = '';
switch (this.os) {
case "win32":
ceres = 'tools/ceres/ceres.exe';
break;
case "darwin":
ceres = "tools/ceres/ceres";
break;
default:
ceres = "tools/ceres/ceres-linux";
break;
}
//
this.nativeExecute(`${ceres} ${sharedArgs}`);
let sed = '';
switch (this.os) {
case "win32":
sed = 'tools/sed.exe';
break;
default:
sed = "LC_ALL=C sed";
break;
}
this.nativeExecute(`${sed} -i "s/local function __module_/function __module_/g" "target/map/war3map.lua"`);
sharedArgs = `add "target/map.w3x" "target/map/*" "/c" "/auto" "/r"`;
//
this.nativeExecute(`${mpqEditor} ${sharedArgs}`);
}
run() {
this.env();
let suffix = '';
let sharedArgs = `-windowmode windowed -nowfpause -loadfile `;
let currentDir = String(__dirname);
switch (this.os) {
case "linux":
suffix = "WINEDEBUG=-all wine64 ";
currentDir = String(currentDir).replace('/', '\\');
sharedArgs += '"Z:' + currentDir + '\\target\\map.w3x"';
break;
case "win32":
sharedArgs += currentDir + '\\target\\map.w3x"';
break;
default:
suffix = "";
break;
}
console.log(`${suffix}"${this.settings.path}" ${sharedArgs}`);
this.nativeExecute(`${suffix}"${this.settings.path}" ${sharedArgs}`);
}
cleanup() {
if (fs.existsSync(`./src`)) {
fs.removeSync('./src');
}
if (fs.existsSync(`./target`)) {
fs.removeSync('./target');
}
}
async generateTs() {
const parser = new jassToTs.JassParser();
await parser.main(['', '', "app/src/lib/core/blizzard.j", "app/src/lib/core/blizzard.d.ts"]);
await parser.main(['', '', "app/src/lib/core/common.j", "app/src/lib/core/common.d.ts"]);
await parser.main(['', '', "app/src/lib/core/common.ai", "app/src/lib/core/commonai.d.ts"]);
}
}
new Build(minimist(process.argv));