Sv443 пре 5 месеци
родитељ
комит
e520bfb606
2 измењених фајлова са 44 додато и 26 уклоњено
  1. 10 6
      src/tools/post-build.ts
  2. 34 20
      src/tools/serve.ts

+ 10 - 6
src/tools/post-build.ts

@@ -164,7 +164,9 @@ I welcome every contribution on GitHub!
       try {
         buildStats = JSON.parse(String(await readFile(".build.json"))) as BuildStats;
       }
-      catch(e) { void e; }
+      catch {
+        void 0;
+      }
     }
 
     let sizeIndicator = "";
@@ -175,11 +177,13 @@ I welcome every contribution on GitHub!
         sizeIndicator = " \x1b[2m(\x1b[0m\x1b[1m" + (sizeDiff > 0 ? "\x1b[33m+" : (sizeDiff !== 0 ? "\x1b[32m-" : "\x1b[32m")) + Math.abs(sizeDiffTrunc) + "\x1b[0m\x1b[2m)\x1b[0m";
     }
 
-    console.info();
-    console.info(`Successfully built for ${envText}\x1b[0m - build number (last commit SHA): ${buildNbr}`);
-    console.info(`Outputted file '${relative("./", scriptPath)}' with a size of \x1b[32m${sizeKiB} KiB\x1b[0m${sizeIndicator}`);
-    console.info(`Userscript URL: \x1b[34m\x1b[4m${devServerUserscriptUrl}\x1b[0m`);
-    console.info();
+    console.info([
+      "",
+      `Successfully built for ${envText}\x1b[0m - build number (last commit SHA): ${buildNbr}`,
+      `Outputted file '${relative("./", scriptPath)}' with a size of \x1b[32m${sizeKiB} KiB\x1b[0m${sizeIndicator}`,
+      `Userscript URL: \x1b[34m\x1b[4m${devServerUserscriptUrl}\x1b[0m`,
+      "",
+    ].join("\n"));
 
     const buildStatsNew: BuildStats = {
       sizeKiB,

+ 34 - 20
src/tools/serve.ts

@@ -3,6 +3,7 @@ import { fileURLToPath } from "node:url";
 import express, { NextFunction, Request, Response } from "express";
 import "dotenv/config";
 import { outputDir } from "../../rollup.config.mjs";
+import type { Server } from "node:http";
 
 const { argv, env, exit, stdout } = process;
 
@@ -18,12 +19,12 @@ const autoExitRaw = argv.find(arg => arg.startsWith("--auto-exit-time="))?.split
 const autoExitTime: number | undefined = !isNaN(Number(autoExitRaw)) ? Number(autoExitRaw) * 1000 : undefined;
 
 const app = express();
+let server: Server;
 
-enableLogging &&
-  app.use((_req, _res, next) => {
-    stdout.write("*");
-    next();
-  });
+enableLogging && app.use((_req, _res, next) => {
+  stdout.write("*");
+  next();
+});
 
 app.use((err: unknown, _req: Request, _res: Response, _next: NextFunction) => {
   if(typeof err === "string" || err instanceof Error)
@@ -42,18 +43,31 @@ app.use("/assets", express.static(
   resolve(fileURLToPath(import.meta.url), "../../../assets/")
 ));
 
-const server = app.listen(devServerPort, "0.0.0.0", () => {
-  console.log(`Dev server is running on port ${devServerPort}`);
-  if(enableLogging)
-    stdout.write("\nRequests: ");
-  else
-    console.log("\x1b[2m(request logging is disabled)\x1b[0m");
-  console.log();
-
-  if(autoExitTime) {
-    console.log(`Exiting in ${autoExitTime / 1000}s...`);
-    setTimeout(() => {
-      server.close(() => setImmediate(() => exit(0)));
-    }, autoExitTime);
-  }
-});
+function closeAndExit(code: number) {
+  !server && setImmediate(() => exit(code));
+  server?.close(() =>
+    setImmediate(() =>
+      exit(code)
+    )
+  );
+}
+
+try {
+  server = app.listen(devServerPort, "0.0.0.0", () => {
+    console.log(`Dev server is running on port ${devServerPort}`);
+    if(enableLogging)
+      stdout.write("\nRequests: ");
+    else
+      console.log("\x1b[2m(request logging is disabled)\x1b[0m");
+    console.log();
+
+    if(autoExitTime) {
+      console.log(`Exiting in ${autoExitTime / 1000}s...`);
+      setTimeout(() => closeAndExit(0), autoExitTime);
+    }
+  });
+}
+catch(err) {
+  console.error("\x1b[31mError starting dev server:\x1b[0m", err);
+  closeAndExit(1);
+}