refactor(dx/server): extract basic build commands to separate file

This commit is contained in:
Elian Doran 2025-09-01 19:36:14 +03:00
parent 978e6b9dde
commit 1e991c0526
No known key found for this signature in database
2 changed files with 38 additions and 21 deletions

View File

@ -1,12 +1,13 @@
import * as esbuild from "esbuild";
import { join } from "path";
import * as fs from "fs-extra";
import * as child_process from "child_process";
import BuildHelper from "../../../scripts/build-utils";
const projectDir = __dirname + "/..";
const outDir = join(projectDir, "dist");
const build = new BuildHelper("apps/server");
async function build() {
async function runBuild() {
esbuild.build({
entryPoints: [
join(projectDir, "src/main.ts"),
@ -41,18 +42,18 @@ async function build() {
function copyAssets() {
// Copy server assets
copy("src/assets", "assets/");
build.copy("src/assets", "assets/");
// Copy node modules
for (const module of [ "better-sqlite3", "bindings", "file-uri-to-path" ]) {
copy(`node_modules/${module}`, `node_modules/${module}/`);
build.copy(`node_modules/${module}`, `node_modules/${module}/`);
}
// Copy sync worker.
copy("node_modules/jsdom/lib/jsdom/living/xhr/xhr-sync-worker.js", "xhr-sync-worker.js");
build.copy("node_modules/jsdom/lib/jsdom/living/xhr/xhr-sync-worker.js", "xhr-sync-worker.js");
// Copy share templates.
copy("../../packages/share-theme/src/templates", "share-theme/templates/");
build.copy("../../packages/share-theme/src/templates", "share-theme/templates/");
}
function buildAndCopyClient() {
@ -60,26 +61,14 @@ function buildAndCopyClient() {
child_process.execSync("pnpm build", { cwd: join(projectDir, "../client"), stdio: "inherit" });
// Copy the artifacts.
copy("../client/dist", "public/");
build.copy("../client/dist", "public/");
// Remove unnecessary files.
deleteFromOutput("public/webpack-stats.json");
}
function copy(projectDirPath: string, outDirPath: string) {
if (outDirPath.endsWith("/")) {
fs.mkdirpSync(join(outDirPath));
}
fs.copySync(join(projectDir, projectDirPath), join(outDir, outDirPath), { dereference: true });
}
function deleteFromOutput(path: string) {
fs.rmSync(join(outDir, path), { recursive: true });
build.deleteFromOutput("public/webpack-stats.json");
}
async function main() {
fs.emptyDirSync(outDir);
await build();
await runBuild();
copyAssets();
buildAndCopyClient();
}

28
scripts/build-utils.ts Normal file
View File

@ -0,0 +1,28 @@
import { rmSync } from "fs";
import { copySync, emptyDirSync, mkdirpSync } from "fs-extra";
import { join } from "path";
export default class BuildHelper {
private projectDir: string;
private outDir: string;
constructor(projectPath: string) {
this.projectDir = join(__dirname, "..", projectPath);
this.outDir = join(this.projectDir, "dist");
emptyDirSync(this.outDir);
}
copy(projectDirPath: string, outDirPath: string) {
if (outDirPath.endsWith("/")) {
mkdirpSync(join(outDirPath));
}
copySync(join(this.projectDir, projectDirPath), join(this.outDir, outDirPath), { dereference: true });
}
deleteFromOutput(path: string) {
rmSync(join(this.outDir, path), { recursive: true });
}
}