-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.js
160 lines (134 loc) · 4.77 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
/* eslint-disable */
const settings = require('./settings.json');
const fs = require('fs-extra');
const typescriptToLua = require('typescript-to-lua');
const ts = require('typescript');
const { execSync } = require('child_process');
class Builder {
constructor(buildSettings) {
this._buildSettings = { preCleanup: true, ...buildSettings };
}
build() {
if (this.buildSettings.preCleanup) {
console.log('Running pre-cleanup...');
this.cleanup();
}
console.log('Creating directories...');
fs.mkdirSync('target');
fs.mkdirSync('target/map.dir');
console.log('Extracting war3map.lua...');
new Runner('"tools/mpqtool.exe"', ['extract', '--output', 'target/map.dir2', `${settings.map.dir}/${settings.map.filename}`]).run();
fs.copySync(`target/map.dir2`, `target/map.dir`);
fs.copySync(`target/map.dir/war3map.lua`, `${settings.map.dir}/map/war3map.lua`);
console.log('Transpiling project...');
const { emitResult, diagnostics } = typescriptToLua.transpileProject('tsconfig.json');
diagnostics.forEach((diagnostic) => {
console.log(diagnostic.messageText);
if (diagnostic.code !== 2306) {
console.error('FATAL: Error in typescript');
throw diagnostic;
}
});
emitResult.forEach(({ name, text }) => ts.sys.writeFile(name, text));
console.log('Building lua map...');
new Runner('"tools/ceres/ceres.exe"', ['build', '--', '--map', 'map', '--output', 'dir']).run();
console.log('Copying war3map files...');
const regex = new RegExp(`.+war3map\.w3.`);
fs.copySync(`target/map.dir2`, `target/map.dir`, {
filter: (src, dest) => {
if (src === 'target\\map.dir2' || src === 'target/map.dir2') {
return true;
}
return src.match(regex);
},
});
console.log('Adding compiled files...');
new Runner('"tools/mpqtool.exe"', ['new', 'target/map.dir', 'target/map.w3x']).run();
}
cleanup() {
if (fs.existsSync(`./lua`)) {
fs.removeSync('./lua');
}
if (fs.existsSync(`./target`)) {
fs.removeSync('./target');
}
}
get buildSettings() {
return this._buildSettings;
}
set buildSettings(buildSettings) {
this._buildSettings = buildSettings;
}
}
class Runner {
constructor(file, args) {
this._file = file;
this._arguments = args;
}
run() {
const stdout = execSync(`${this.file} ${this.arguments.join(' ')}`).toString('utf8');
if (stdout) {
console.log(stdout);
}
}
get file() {
return this._file;
}
get arguments() {
return this._arguments;
}
}
class CommandHandler {
static get helpText() {
return `
Usage: build.js [options]
options:
-b, --build Build the project
-r, --run Run the map in Warcraft 3
-t, --test Run the tests in busted (currently not implemented)
--buildnumber <number> Set the ingame buildnumber for the map
--release <num.num.num> Set The full release version
-h, --help Shows this help menu
`;
}
constructor(args) {
if (args.length === 1 && args[0] === '--help') {
console.log(this.helpText);
return process.exit(0);
}
args.forEach((argument) => {
if (argument === 'build') {
console.log('Building...');
try {
new Builder().build();
} catch (err) {
if (err.stdout && err.stdout.toString) {
console.log(err.stdout.toString());
}
if (err.stderr && err.stderr.toString) {
console.error(err.stderr.toString());
}
console.error(err);
process.exit(1);
}
}
if (argument === 'run') {
console.log('Starting Warcraft III...');
try {
new Runner(`"${settings.game.path}"`, [
...settings.game.arguments,
'-loadfile',
`"${process.cwd()}/target/map.w3x"`,
]).run();
} catch (err) {
console.error(err);
process.exit(1);
}
}
});
}
get helpText() {
return this._helpText;
}
}
new CommandHandler(process.argv.slice(2));