pokemon-showdown/build
Kirk Scheibelhut b9e5fcb387
Introduce set importer logic (#5749)
`node tools/set-import [version]` can be run to create a
`@pokemon-showdown/sets` package containing sets from Smogon, usage
stats and third party sources. Some notes:

- The build is set up so that `tools/set-import/importer.ts` is
  compiled to `tools/set-import/importer.js` - creating a
  `.tools-dist/set-import` directory for the sole artifact was
  deemed to be overkill
- The sets package is generated such that it can be used easily on
  Node or in a browser (where the large set JSON can be loaded
  asynchronously)
- Supported tiers/formats are mostly arbitrary - those popular
  enough to have a signficant playerbase or analyses on Smogon have
  been included, but additional formats can be added based on demand
- Some set information is redundant for ease of use by downstream
  users: certain formes are split out and information that could
  theoretically be inferred like level/ability/HP IVs are included
  to simplify parsing logic and make the sets more immediately
  available. This results in what should mostly be negligible
  size overhead.
- In a similar vein, display versions of effect names instead of IDs
  are used (name -> ID is trivial, ID -> name requires data lookup)
- All sets pass validation, provided certain simple transformations
  are applied (eg. reverting `battleOnly` formes like Megas)

This tool has primarily been tested to run on Linux - running on
other platforms is not guaranteed to result in error-free output.
2019-09-10 21:15:05 -04:00

86 lines
2.7 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');
function shell(cmd) {
child_process.execSync(cmd, {stdio: 'inherit', cwd: __dirname});
}
function sucrase(src, out, opts) {
shell(`npx sucrase ${opts || ''} -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);
}
});
}
});
}
try {
require.resolve('sucrase');
} catch (e) {
console.log('Installing dependencies...');
shell('npm install --production');
}
sucrase('./sim', './.sim-dist');
sucrase('./lib', './.lib-dist');
sucrase('./server', './.server-dist');
sucrase('./tools/set-import', './tools/set-import', '--exclude-dirs=sets');
// 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)(.*?\\\))`, 'g'), replace: `$1.lib-dist$3`},
]);
replace(path.join(__dirname, '.server-dist'), [
{regex: new RegExp(`(require\\\(.*?)(lib|sim)(.*?\\\))`, 'g'), replace: `$1.$2-dist$3`},
]);
replace(path.join(__dirname, './tools/set-import'), [
{regex: new RegExp(`(require\\\(.*?)(lib|sim)(.*?\\\))`, 'g'), replace: `$1.$2-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'))
);
}