forked from scoopapa/DH
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build
186 lines (165 loc) · 5.56 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
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
#!/usr/bin/env node
try {
// technically this was introduced in Node 11, but we'll ask for the most recent LTS with it to be safe
[].flatMap(x => x);
} catch (e) {
console.log("We require Node.js version 12 or later; you're using " + process.version);
process.exit(1);
}
var child_process = require('child_process');
var fs = require('fs');
var path = require('path');
var force = process.argv[2] === '--force';
process.chdir(__dirname);
function shell(cmd) {
child_process.execSync(cmd, {stdio: 'inherit'});
}
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 copyOverDataJSON(file) {
var source = './data/' + file;
var dest = './.data-dist/' + file;
fs.readFile(source, function (err, text) {
if (err) throw err;
fs.writeFile(dest, text, function (err) {
if (err) throw err;
});
});
}
// Check to make sure the most recently added or updated dependency is installed at the correct version
try {
var sucraseVersion = require('sucrase').getVersion().split('.');
if (
parseInt(sucraseVersion[0]) < 3 ||
(parseInt(sucraseVersion[0]) === 3 && parseInt(sucraseVersion[1]) < 15)
) {
throw new Error("Sucrase version too old");
}
} catch (e) {
console.log('Installing dependencies...');
shell('npm install --production');
}
function needsSucrase(source, dest, path = '') {
if (path.endsWith('.ts')) {
if (path.endsWith('.d.ts')) return false;
const sourceStat = fs.lstatSync(source + path);
try {
const destStat = fs.lstatSync(dest + path.slice(0, -2) + 'js');
return (sourceStat.ctimeMs > destStat.ctimeMs);
} catch (e) {
// dest doesn't exist
return true;
}
}
if (!path.includes('.')) {
// probably dir
try {
const files = fs.readdirSync(source + path);
for (const file of files) {
if (needsSucrase(source, dest, path + '/' + file)) {
return true;
}
}
} catch (e) {
// not dir
}
return false;
}
return false;
}
function sucrase(src, out, opts) {
try {
if (!force && src !== './config' && !needsSucrase(src, out)) {
return false;
}
} catch (e) {}
shell(`npx sucrase ${opts || ''} -q ${src} -d ${out} --transforms typescript,imports --enable-legacy-typescript-module-interop`);
return true;
}
if (sucrase('./config', './.config-dist')) {
replace('.config-dist', [
{regex: /(require\(.*?)(lib|sim)/g, replace: `$1.$2-dist`},
]);
}
if (sucrase('./data', './.data-dist')) {
replace('.data-dist', [
{regex: /(require\(.*?)(lib|sim)/g, replace: `$1.$2-dist`},
]);
}
if (sucrase('./sim', './.sim-dist')) {
replace('.sim-dist', [
{regex: /(require\(.*?)(lib)/g, replace: `$1.lib-dist`},
]);
}
sucrase('./lib', './.lib-dist');
if (sucrase('./server', './.server-dist')) {
replace('.server-dist', [
{regex: /(require\(.*?)(lib|sim)/g, replace: `$1.$2-dist`},
]);
}
sucrase('./translations', './.translations-dist');
if (sucrase('./tools/set-import', './tools/set-import', '--exclude-dirs=sets')) {
replace('./tools/set-import/importer.js', [
{regex: /(require\(.*?)(lib|sim)/g, replace: `$1.$2-dist`},
]);
}
if (sucrase('./tools/modlog', './tools/modlog')) {
replace('./tools/modlog/converter.js', [
{regex: /(require\(.*?)(server|lib)/g, replace: `$1.$2-dist`},
]);
}
if (!fs.existsSync('./.data-dist/README.md')) {
const text = '**NOTE**: This folder contains the compiled output of the `data/` directory.\n' +
'You should be editing the `.ts` files there and then running `npm run build` or\n' +
'`./pokemon-showdown` to force these `.js` files to be recreated.\n';
fs.writeFile('./.config-dist/README.md', text.replace('data/', 'config/'), function () {});
fs.writeFile('./.data-dist/README.md', text, function () {});
fs.writeFile('./.sim-dist/README.md', text.replace('data/', 'sim/'), function () {});
fs.writeFile('./.server-dist/README.md', text.replace('data/', 'server/'), function () {});
fs.writeFile('./.translations-dist/README.md', text.replace('data/', 'translations/'), function () {});
fs.writeFile('./.lib-dist/README.md', text.replace('data/', 'lib/'), function () {});
}
// sucrase doesn't copy JSON over, so we'll have to do it ourselves
copyOverDataJSON('cap-1v1-sets.json');
copyOverDataJSON('mods/gen7/factory-sets.json');
copyOverDataJSON('mods/gen7/bss-factory-sets.json');
copyOverDataJSON('mods/gen6/factory-sets.json');
// NOTE: replace is asynchronous - add additional replacements for the same path in one call instead of making multiple calls.
// 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(
'config/config.js',
fs.readFileSync('config/config-example.js')
);
}