forked from smogon/pokemon-showdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build
executable file
·81 lines (71 loc) · 2.42 KB
/
build
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
#!/usr/bin/env node
try {
RegExp("\\p{Emoji}", "u");
} catch (e) {
console.log("We require Node.js version 10 or later; you're using " + process.version);
process.exit(1);
}
var child_process = require('child_process');
var fs = require('fs');
var path = require('path');
function shell(cmd) {
child_process.execSync(cmd, {stdio: 'inherit', cwd: __dirname});
}
function sucrase(src, out) {
shell(`npx sucrase -q ${src} -d ${out} --transforms typescript,imports --enable-legacy-typescript-module-interop`);
}
function replace(file, replacements) {
fs.lstat(file, function (err, stats) {
if (err) throw err;
if (stats.isSymbolicLink()) return;
if (stats.isFile()) {
if (!file.endsWith('.js')) return;
fs.readFile(file, "utf-8", function (err, text) {
if (err) throw err;
var anyMatch = false;
for (var i = 0; i < replacements.length; i++) {
anyMatch = anyMatch || text.match(replacements[i].regex);
if (anyMatch) text = text.replace(replacements[i].regex, replacements[i].replace);
}
if (!anyMatch) return;
fs.writeFile(file, text, function (err) {
if (err) throw err;
});
});
} else if (stats.isDirectory()) {
fs.readdir(file, function (err, files) {
if (err) throw err;
for (var i = 0; i < files.length; i++) {
replace(path.join(file, files[i]), replacements);
}
});
}
});
}
function rewrite(src, out, dist) {
replace(new RegExp(`(require\\\(.*?)(${src})(.*?\\\))`), `$1${out}$3`, path.join(__dirname, dist));
}
try {
require.resolve('sucrase');
} catch (e) {
console.log('Installing dependencies...');
shell('npm install --production');
}
sucrase('./sim', './.sim-dist');
sucrase('./lib', './.lib-dist');
// NOTE: replace is asynchronous - add additional replacements for the same path in one call instead of making multiple calls.
replace(path.join(__dirname, '.sim-dist'), [
{regex: new RegExp(`(require\\\(.*?)(lib)(.*?\\\))`), replace: `$1.lib-dist$3`},
]);
// Make sure config.js exists. If not, copy it over synchronously from
// config-example.js, since it's needed before we can start the server
try {
require.resolve('./config/config');
} catch (err) {
if (err.code !== 'MODULE_NOT_FOUND') throw err; // should never happen
console.log('config.js does not exist. Creating one with default settings...');
fs.writeFileSync(
path.resolve(__dirname, 'config/config.js'),
fs.readFileSync(path.resolve(__dirname, 'config/config-example.js'))
);
}