Skip to content

Commit

Permalink
fix: add support for alternate port for web UI (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
saikatmitra91 authored Apr 4, 2024
1 parent 8f7c8ba commit 5ed6ca9
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 27 deletions.
5 changes: 5 additions & 0 deletions .changeset/neat-months-admire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@empiricalrun/cli": patch
---

fix: add support for alternate port for web UI
2 changes: 2 additions & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"devDependencies": {
"@empiricalrun/typescript-config": "workspace:*",
"@types/cli-progress": "^3.11.5",
"@types/detect-port": "^1.3.5",
"@types/node": "^20.11.24",
"@types/opener": "^1.4.3"
},
Expand All @@ -34,6 +35,7 @@
"@types/express": "^4.17.21",
"cli-progress": "^3.12.0",
"commander": "^12.0.0",
"detect-port": "^1.5.1",
"dotenv": "^16.4.5",
"express": "^4.19.2",
"opener": "^1.5.2",
Expand Down
24 changes: 20 additions & 4 deletions packages/cli/src/bin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
setRunSummary,
} from "../stats";
import { reportOnCI } from "../reporters/ci";
import detect from "detect-port";

const configFileName = "empiricalrc.json";
const cwd = process.cwd();
Expand Down Expand Up @@ -143,17 +144,32 @@ program
}
});

const defaultWebUIPort = 1337;
program
.command("ui")
.description("visualise the results of a run in your web browser")
.action(async () => {
.option(
"-p, --port <int>",
"port to run the empirical webapp on",
`${defaultWebUIPort}`,
)
.action(async (options) => {
console.log(yellow("Initiating webapp..."));
const app = express();
const port = 8000;
const port =
!options.port || isNaN(Number(options.port))
? defaultWebUIPort
: Number(options.port);
const availablePort = await detect(port);
if (availablePort !== port) {
console.log(
`${yellow("[Warning]")} Port ${port} is unavailable. Trying port ${availablePort}.`,
);
}
app.use(express.static(path.join(__dirname, "../webapp")));
app.get("/api/results", (req, res) => res.sendFile(outputFilePath));
const fullUrl = `http://localhost:${port}`;
app.listen(port, () => {
const fullUrl = `http://localhost:${availablePort}`;
app.listen(availablePort, () => {
console.log(`Empirical app running on ${fullUrl}`);
opener(fullUrl);
});
Expand Down
69 changes: 46 additions & 23 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5ed6ca9

Please sign in to comment.