From 59f4877ee551e1c57c13b087c32cbef44c93fa67 Mon Sep 17 00:00:00 2001 From: Axorax <78349410+Axorax@users.noreply.github.com> Date: Thu, 14 Mar 2024 12:23:53 +0600 Subject: [PATCH] update: use any port if all attempts fail --- dev-server.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/dev-server.js b/dev-server.js index 30f149d1..2add5966 100644 --- a/dev-server.js +++ b/dev-server.js @@ -10,15 +10,14 @@ let port = process.env.PORT ?? 4000; // Starting port const maxAttempts = 10; // Maximum number of ports to try const env = argv[2] ?? "dev"; -const startServer = (attempt) => { +const startServer = (attempt, useAnyFreePort = false) => { if (attempt > maxAttempts) { - console.error(chalk.red(`ERROR: Unable to find an available port after ${maxAttempts} attempts.`)); - return; + useAnyFreePort = true; // Use any port that is free } - app.listen(port, () => { + const server = app.listen(useAnyFreePort ? 0 : port, () => { console.log("\n-----------------------------------------------------------\n"); - console.log(`Puter is now live at: `, chalk.underline.blue(`http://localhost:${port}`)); + console.log(`Puter is now live at: `, chalk.underline.blue(`http://localhost:${server.address().port}`)); console.log("\n-----------------------------------------------------------\n"); }).on('error', (err) => { if (err.code === 'EADDRINUSE') { // Check if the error is because the port is already in use