pokemon-showdown/tools/build-utils.js
2022-12-28 17:31:21 -06:00

62 lines
1.6 KiB
JavaScript

"use strict";
const fs = require("fs");
const child_process = require("child_process");
const esbuild = require('esbuild');
const copyOverDataJSON = (file = 'data') => {
const files = fs.readdirSync(file);
for (const f of files) {
if (fs.statSync(`${file}/${f}`).isDirectory()) {
copyOverDataJSON(`${file}/${f}`);
} else if (f.endsWith('.json')) {
fs.copyFileSync(`${file}/${f}`, require('path').resolve('dist', `${file}/${f}`));
}
}
};
const shouldBeCompiled = file => {
if (file.includes('node_modules/')) return false;
if (file.endsWith('.tsx')) return true;
if (file.endsWith('.ts')) return !(file.endsWith('.d.ts') || file.includes('global'));
return false;
};
const findFilesForPath = path => {
const out = [];
const files = fs.readdirSync(path);
for (const file of files) {
const cur = `${path}/${file}`;
if (cur.includes('node_modules')) continue;
if (fs.statSync(cur).isDirectory()) {
out.push(...findFilesForPath(cur));
} else if (shouldBeCompiled(cur)) {
out.push(cur);
}
}
return out;
};
exports.transpile = (decl) => {
esbuild.buildSync({
entryPoints: findFilesForPath('./'),
outdir: './dist',
outbase: '.',
format: 'cjs',
tsconfig: './tsconfig.json',
});
fs.copyFileSync('./config/config-example.js', './dist/config/config-example.js');
copyOverDataJSON();
// NOTE: replace is asynchronous - add additional replacements for the same path in one call instead of making multiple calls.
if (decl) {
exports.buildDecls();
}
};
exports.buildDecls = () => {
try {
child_process.execSync(`node ./node_modules/typescript/bin/tsc -p sim`, {stdio: 'inherit'});
} catch {}
};