Explorar o código

feat: auto-exit arg for serve script

Sv443 hai 10 meses
pai
achega
1761c83dd7
Modificáronse 1 ficheiros con 12 adicións e 1 borrados
  1. 12 1
      src/tools/serve.ts

+ 12 - 1
src/tools/serve.ts

@@ -11,6 +11,10 @@ const devServerPort = isNaN(envPort) || envPort === 0 ? 8710 : envPort;
 /** Whether to log requests to the console */
 const enableLogging = false;
 
+const autoExitRaw = process.argv.find(arg => arg.startsWith("--auto-exit="))?.split("=")[1];
+/** Time in milliseconds after which the process should automatically exit */
+const autoExitTime: number | undefined = !isNaN(Number(autoExitRaw)) ? Number(autoExitRaw) * 1000 : undefined;
+
 const app = express();
 
 enableLogging &&
@@ -36,11 +40,18 @@ app.use("/assets", express.static(
   resolve(fileURLToPath(import.meta.url), "../../../assets/")
 ));
 
-app.listen(devServerPort, "0.0.0.0", () => {
+const server = app.listen(devServerPort, "0.0.0.0", () => {
   console.log(`Dev server is running on port ${devServerPort}`);
   if(enableLogging)
     process.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(() => process.exit(0)));
+    }, autoExitTime);
+  }
 });