-
Notifications
You must be signed in to change notification settings - Fork 28
/
tools.ts
276 lines (227 loc) · 8.63 KB
/
tools.ts
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import child_process = require('child_process');
import esbuild = require('esbuild');
import fs = require('fs');
import path = require('path');
import { buildSrc } from './build-src';
import type { IInputMetadata, InputFolderNames, InputFolders, RunOptionNames, RunOptions } from './src/types/root';
if (!global._outputFilepaths) global._outputFilepaths = [];
const folderNames: InputFolderNames[] = ['private', 'src', 'web', 'Lanette-private'];
const optionNames: RunOptionNames[] = ['offline', 'incrementalBuild', 'modules', 'categories', 'games', 'gameSeed', 'mochaRuns', 'script',
'grep', 'noBuild', 'noRemote', 'noSha', 'regression'];
const optionAliases: Dict<RunOptionNames> = {
'local': 'offline',
'incremental': 'incrementalBuild',
'module': 'modules',
'game': 'games',
'category': 'categories',
};
export function getInputFolders(): InputFolders {
if (global._inputFolders) return global._inputFolders;
// __dirname from build script is root/build
const rootFolder: IInputMetadata = {
buildPath: __dirname,
inputPath: path.join(__dirname, ".."),
};
const inputFolders: PartialKeyedDict<InputFolderNames, IInputMetadata> = {};
for (const folderName of folderNames) {
inputFolders[folderName] = {
buildPath: path.join(rootFolder.buildPath, folderName),
inputPath: path.join(rootFolder.inputPath, folderName),
};
if (folderName === 'web') {
inputFolders[folderName].tsConfig = path.join(inputFolders[folderName].inputPath, "tsconfig.json");
}
}
global._inputFolders = {
root: rootFolder,
private: inputFolders.private!,
src: inputFolders.src!,
web: inputFolders.web!,
'Lanette-private': inputFolders['Lanette-private']!,
};
return global._inputFolders;
}
export function getRunOptions(filename?: string): RunOptions {
if (!global._runOptions) {
global._runOptions = {};
const startIndex = filename ? process.argv.indexOf(filename) + 1 : 0;
for (let i = startIndex; i < process.argv.length; i++) {
if (!process.argv[i].startsWith('--')) continue;
const arg = process.argv[i].substr(2);
if (!arg) continue;
const equalsIndex = arg.indexOf('=');
let optionName = arg;
let value;
if (equalsIndex === -1) {
value = 'true';
} else {
optionName = arg.substr(0, equalsIndex);
value = arg.substr(equalsIndex + 1).trim();
}
if (optionName in optionAliases) optionName = optionAliases[optionName];
if (!optionNames.includes(optionName as RunOptionNames)) throw new Error("Unknown test option '" + optionName + "'");
global._runOptions[optionName as RunOptionNames] = value;
}
}
return global._runOptions;
}
export async function initializeSrc(options?: RunOptions): Promise<void> {
await buildSrc(options).catch(e => {
console.log(e);
process.exit(1);
});
// tools.ts is required by build-src so src files cannot be imported directly
const appPath = path.join(getInputFolders().src.buildPath, 'app');
// eslint-disable-next-line @typescript-eslint/no-var-requires
(require(appPath) as typeof import("./src/app")).instantiate();
}
export function setExceptionHandler(): void {
process.on('uncaughtException', error => {
console.log(error);
Tools.logException(error, "process.on('uncaughtException')");
});
}
// Modified from https://stackoverflow.com/a/32197381
export function deleteFolderRecursive(folder: string): void {
folder = folder.trim();
if (!folder || folder === '/' || folder === '.') return;
let exists = false;
try {
fs.accessSync(folder);
exists = true;
} catch (e) {} // eslint-disable-line no-empty
if (exists) {
const files = fs.readdirSync(folder);
for (const file of files) {
const curPath = path.join(folder, file);
if (fs.lstatSync(curPath).isDirectory()) {
deleteFolderRecursive(curPath);
} else {
fs.unlinkSync(curPath);
}
}
if (folder !== getInputFolders().root.buildPath) fs.rmdirSync(folder);
}
}
export function listFilesRecursive(folder: string): string[] {
folder = folder.trim();
if (!folder || folder === '/' || folder === '.') return [];
let fileList: string[] = [];
let exists = false;
try {
fs.accessSync(folder);
exists = true;
} catch (e) {} // eslint-disable-line no-empty
if (exists) {
const files = fs.readdirSync(folder);
for (const file of files) {
const curPath = path.join(folder, file);
if (fs.lstatSync(curPath).isDirectory()) {
fileList = fileList.concat(listFilesRecursive(curPath));
}
fileList.push(curPath);
}
}
return fileList;
}
export function deletePreviousBuild(): void {
const inputFolders = getInputFolders();
for (const folderName of folderNames) {
if (folderName === 'root') continue;
deleteFolderRecursive(inputFolders[folderName].buildPath);
}
}
export function exec(command: string): string | false {
try {
const result = child_process.execSync(command);
if (typeof result === 'string') return result;
return result.toString();
} catch (e: unknown) {
console.log("Failed to run command " + command + ": " + (e as Error).message);
return false;
}
}
export function setToSha(sha: string): string | false {
return exec('git reset --hard ' + sha);
}
export function copyPokemonShowdownShaBase(): void {
const inputFolders = getInputFolders();
fs.writeFileSync(path.join(inputFolders.root.inputPath, 'pokemon-showdown-sha.txt'),
fs.readFileSync(path.join(inputFolders.root.inputPath, 'pokemon-showdown-sha-base.txt')));
}
export function createUntrackedFiles(): void {
const inputFolders = getInputFolders();
const pokemonShowdownShaFile = path.join(inputFolders.root.inputPath, 'pokemon-showdown-sha.txt');
if (!fs.existsSync(pokemonShowdownShaFile)) {
copyPokemonShowdownShaBase();
}
const configFile = path.join(inputFolders.src.inputPath, 'config.ts');
if (!fs.existsSync(configFile)) {
console.log("Creating a default config.ts in the src folder (you need to edit this)...");
fs.writeFileSync(configFile, fs.readFileSync(path.join(inputFolders.src.inputPath, 'config-example.ts')));
}
const clientDataDirectory = path.join(inputFolders.root.inputPath, 'client-data');
const pokedexMiniFile = path.join(clientDataDirectory, 'pokedex-mini.js');
if (!fs.existsSync(pokedexMiniFile)) {
fs.writeFileSync(pokedexMiniFile, fs.readFileSync(path.join(clientDataDirectory, 'pokedex-mini-base.js')));
}
const pokedexMiniBWFile = path.join(clientDataDirectory, 'pokedex-mini-bw.js');
if (!fs.existsSync(pokedexMiniBWFile)) {
fs.writeFileSync(pokedexMiniBWFile, fs.readFileSync(path.join(clientDataDirectory, 'pokedex-mini-bw-base.js')));
}
}
export function transpile(): void {
const inputFolders = getInputFolders();
const tsConfig = path.join(inputFolders.root.inputPath, "tsconfig.json");
const currentOutputFilepaths: string[] = [];
for (const folderName of folderNames) {
if (folderName === 'root') continue;
const inputFolder = inputFolders[folderName];
const inputFiles = listFilesRecursive(inputFolder.inputPath);
const folderEntryPoints: string[] = [];
for (const filepath of inputFiles) {
if (!filepath.endsWith('.ts') || filepath.endsWith('.d.ts')) continue;
folderEntryPoints.push(filepath);
let outputDirectory = inputFolder.buildPath;
const relativeFilepath = filepath.substr(inputFolder.inputPath.length + 1);
let filename = relativeFilepath;
const index = relativeFilepath.lastIndexOf(path.sep);
if (index !== -1) {
outputDirectory = path.join(outputDirectory, relativeFilepath.substr(0, index));
filename = relativeFilepath.substr(index + 1);
}
const outputFilepath = path.join(outputDirectory, filename.substr(0, filename.length - 3) + ".js");
if (!global._outputFilepaths!.includes(outputFilepath)) global._outputFilepaths!.push(outputFilepath);
currentOutputFilepaths.push(outputFilepath);
}
const result = esbuild.buildSync({
entryPoints: folderEntryPoints,
format: 'cjs',
platform: 'node',
sourcemap: true,
tsconfig: inputFolder.tsConfig || tsConfig,
outdir: inputFolder.buildPath,
});
if (result.errors.length) {
console.log("Error building folder " + folderName + ": " + result.errors.map(x => x.text).join("\n"));
process.exit(1);
}
if (folderName === 'Lanette-private') {
try {
const postBuildPath = path.join(inputFolder.buildPath, 'post-build.js');
require(postBuildPath);
} catch (e) {} // eslint-disable-line no-empty
}
}
for (const filepath of global._outputFilepaths!) {
if (!currentOutputFilepaths.includes(filepath)) {
try {
fs.unlinkSync(filepath);
} catch (e) {} // eslint-disable-line no-empty
try {
fs.unlinkSync(filepath + ".map");
} catch (e) {} // eslint-disable-line no-empty
global._outputFilepaths!.splice(global._outputFilepaths!.indexOf(filepath), 1);
}
}
}