1
0
mirror of https://github.com/spacebarchat/server.git synced 2024-11-06 19:02:33 +01:00
server/bundle/scripts/build.js

71 lines
1.9 KiB
JavaScript
Raw Normal View History

2021-10-07 18:21:01 +02:00
const { spawn } = require("child_process");
const path = require("path");
const { performance } = require("perf_hooks");
2021-10-08 10:43:43 +02:00
const fs = require("fs");
const esbuildPluginTsc = require("esbuild-plugin-tsc");
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-08 10:43:43 +02:00
require("esbuild").build({
entryPoints: walk(path.join(dir, "src")),
bundle: false,
outdir: path.join(dir, "dist"),
target: "es2021",
format: "cjs",
plugins: [esbuildPluginTsc({})],
keepNames: true,
});
}
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-08 10:43:43 +02:00
console.log("[Build] starting ...");
2021-10-07 18:21:01 +02:00
2021-10-08 10:43:43 +02:00
util();
2021-10-07 18:21:01 +02:00
for (const part of parts) {
buildPackage(path.join(__dirname, "..", "..", part));
}
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) {
file = dir + "/" + file;
var stat = fs.statSync(file);
if (stat && stat.isDirectory()) {
/* Recurse into a subdirectory */
results = results.concat(walk(file));
} else if (file.endsWith(".ts")) {
/* Is a file */
results.push(file);
}
});
return results;
}