pokemon-showdown/build
Kirk Scheibelhut 3624863551 Fix build script on Windows. (#5241)
On Windows,	`shell` doesn't glob for us and the literal
`.sim-dist/*` is passed to `replace` and it doesn't get expanded
into the list of paths.
2019-03-03 21:00:56 -05:00

52 lines
1.5 KiB
JavaScript
Executable File

#!/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');
var replace = require('replace');
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 rewrite(src, out, dist) {
replace({
regex: `(require\\\(.*?)(${src})(.*?\\\))`,
replacement: `$1${out}$3`,
paths: fs.readdirSync(dist).map(f => path.join(__dirname, dist, f)),
silent: true,
});
}
try {
require.resolve('replace');
} catch (e) {
console.log('Installing dependencies...');
shell('npm install');
}
sucrase('./sim', './.sim-dist');
sucrase('./lib', './.lib-dist');
rewrite('lib', '.lib-dist', '.sim-dist');
// 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'))
);
}