From 1e991c0526037c1d788a80c3a95bc498f9c6f7a8 Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Mon, 1 Sep 2025 19:36:14 +0300 Subject: [PATCH] refactor(dx/server): extract basic build commands to separate file --- apps/server/scripts/build.ts | 31 ++++++++++--------------------- scripts/build-utils.ts | 28 ++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 21 deletions(-) create mode 100644 scripts/build-utils.ts diff --git a/apps/server/scripts/build.ts b/apps/server/scripts/build.ts index 29147ec5b..b02969722 100644 --- a/apps/server/scripts/build.ts +++ b/apps/server/scripts/build.ts @@ -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(); } diff --git a/scripts/build-utils.ts b/scripts/build-utils.ts new file mode 100644 index 000000000..27790488b --- /dev/null +++ b/scripts/build-utils.ts @@ -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 }); + } + +}