2021-10-07 18:21:01 +02:00
|
|
|
const { spawn } = require("child_process");
|
|
|
|
const path = require("path");
|
2021-10-08 10:43:43 +02:00
|
|
|
const fs = require("fs");
|
2021-10-09 01:44:44 +02:00
|
|
|
const { performance } = require("perf_hooks");
|
2021-10-07 18:21:01 +02:00
|
|
|
|
2021-10-08 10:43:43 +02:00
|
|
|
let parts = "api,cdn,gateway,bundle".split(",");
|
|
|
|
const tscBin = path.join(__dirname, "..", "..", "util", "node_modules", "typescript", "bin", "tsc");
|
|
|
|
const swcBin = path.join(__dirname, "..", "..", "util", "node_modules", "@swc", "cli", "bin", "swc");
|
2021-10-07 18:21:01 +02:00
|
|
|
|
|
|
|
// because npm run is slow we directly get the build script of the package.json script
|
|
|
|
|
|
|
|
function buildPackage(dir) {
|
|
|
|
const element = path.basename(dir);
|
|
|
|
|
2021-10-09 01:44:44 +02:00
|
|
|
return require("esbuild").build({
|
2021-10-08 10:43:43 +02:00
|
|
|
entryPoints: walk(path.join(dir, "src")),
|
|
|
|
bundle: false,
|
|
|
|
outdir: path.join(dir, "dist"),
|
|
|
|
target: "es2021",
|
2021-10-09 01:44:44 +02:00
|
|
|
// plugins don't really work because bundle is false
|
|
|
|
keepNames: false,
|
|
|
|
tsconfig: path.join(dir, "tsconfig.json"),
|
2021-10-08 10:43:43 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-10-09 01:44:44 +02:00
|
|
|
const importPart = /import (\* as )?(({[^}]+})|(\w+)) from ("[.\w-/@q]+")/g;
|
|
|
|
const importMod = /import ("[\w-/@q.]+")/g;
|
|
|
|
const exportDefault = /export default/g;
|
|
|
|
const exportAllAs = /export \* from (".+")/g;
|
|
|
|
const exportMod = /export ({[\w, ]+})/g;
|
|
|
|
const exportConst = /export (const|var|let) (\w+)/g;
|
|
|
|
const exportPart = /export ((async )?\w+) (\w+)/g;
|
|
|
|
|
|
|
|
// resolves tsconfig paths + rewrites es6 imports/exports to require (because esbuild/swc doesn't work properly)
|
|
|
|
function transpileFiles() {
|
|
|
|
for (const part of ["gateway", "api", "cdn", "bundle"]) {
|
|
|
|
const files = walk(path.join(__dirname, "..", "..", part, "dist"));
|
|
|
|
for (const file of files) {
|
|
|
|
let content = fs.readFileSync(file, { encoding: "utf8" });
|
|
|
|
content = content
|
2021-10-09 01:53:41 +02:00
|
|
|
.replace(
|
|
|
|
new RegExp(`@fosscord/${part}`),
|
2021-10-09 01:44:44 +02:00
|
|
|
path.relative(file, path.join(__dirname, "..", "..", part, "dist")).slice(3)
|
|
|
|
)
|
|
|
|
.replace(importPart, `const $2 = require($5)`)
|
|
|
|
.replace(importMod, `require($1)`)
|
|
|
|
.replace(exportDefault, `module.exports =`)
|
|
|
|
.replace(exportAllAs, `module.exports = {...(module.exports)||{}, ...require($1)}`)
|
|
|
|
.replace(exportMod, "module.exports = $1")
|
|
|
|
.replace(exportConst, `let $2 = {};\nmodule.exports.$2 = $2`)
|
|
|
|
.replace(exportPart, `module.exports.$3 = $1 $3`);
|
|
|
|
fs.writeFileSync(file, content);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-08 10:43:43 +02:00
|
|
|
function util() {
|
|
|
|
// const child = spawn("node", `${swcBin} src --out-dir dist --sync`.split(" "), {
|
|
|
|
const child = spawn("node", `${tscBin} -b .`.split(" "), {
|
|
|
|
cwd: path.join(__dirname, "..", "..", "util"),
|
2021-10-07 18:21:01 +02:00
|
|
|
env: process.env,
|
|
|
|
shell: true,
|
|
|
|
});
|
|
|
|
function log(data) {
|
2021-10-08 10:43:43 +02:00
|
|
|
console.log(`[util] ` + data.toString().slice(0, -1));
|
2021-10-07 18:21:01 +02:00
|
|
|
}
|
|
|
|
child.stdout.on("data", log);
|
|
|
|
child.stderr.on("data", log);
|
2021-10-08 10:43:43 +02:00
|
|
|
child.on("error", (err) => console.error("util", err));
|
2021-10-07 18:21:01 +02:00
|
|
|
return child;
|
|
|
|
}
|
|
|
|
|
|
|
|
const start = performance.now();
|
|
|
|
|
2021-10-09 01:44:44 +02:00
|
|
|
async function main() {
|
|
|
|
console.log("[Build] starting ...");
|
|
|
|
util();
|
|
|
|
await Promise.all(parts.map((part) => buildPackage(path.join(__dirname, "..", "..", part))));
|
|
|
|
transpileFiles();
|
2021-10-07 18:21:01 +02:00
|
|
|
}
|
|
|
|
|
2021-10-09 01:44:44 +02:00
|
|
|
main();
|
|
|
|
|
2021-10-07 18:21:01 +02:00
|
|
|
process.on("exit", () => {
|
2021-10-08 10:43:43 +02:00
|
|
|
console.log("[Build] took " + Math.round(performance.now() - start) + "ms");
|
2021-10-07 18:21:01 +02:00
|
|
|
});
|
2021-10-08 10:43:43 +02:00
|
|
|
|
|
|
|
function walk(dir) {
|
|
|
|
var results = [];
|
|
|
|
var list = fs.readdirSync(dir);
|
|
|
|
list.forEach(function (file) {
|
2021-10-09 18:06:02 +02:00
|
|
|
file = path.join(dir, file);
|
2021-10-08 10:43:43 +02:00
|
|
|
var stat = fs.statSync(file);
|
|
|
|
if (stat && stat.isDirectory()) {
|
|
|
|
/* Recurse into a subdirectory */
|
|
|
|
results = results.concat(walk(file));
|
2021-10-09 01:44:44 +02:00
|
|
|
} else if (file.endsWith(".ts") || file.endsWith(".js")) {
|
2021-10-08 10:43:43 +02:00
|
|
|
/* Is a file */
|
|
|
|
results.push(file);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return results;
|
|
|
|
}
|