Skip to content

Commit eed8aa8

Browse files
authored
Merge pull request #69 from webxdc/rtn/68
Use next available port
2 parents 3381a59 + e85725e commit eed8aa8

File tree

8 files changed

+73
-23
lines changed

8 files changed

+73
-23
lines changed

backend/app.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ export function createFrontend(
5959
app.get<{}, Instance[]>("/instances", (req, res) => {
6060
res.json(instances.list());
6161
});
62-
app.post<{}, Instance>("/instances", (req, res) => {
63-
const instance = instances.add();
62+
app.post<{}, Instance>("/instances", async (req, res) => {
63+
const instance = await instances.add();
6464
instance.start();
6565
res.json({
6666
id: instance.id,

backend/cli.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ const program = createProgram({
1818
},
1919
});
2020

21-
program.parse();
21+
program.parseAsync();

backend/dev-cli.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ const program = createProgram({
2424
},
2525
});
2626

27-
program.parse();
27+
program.parseAsync();

backend/instance.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import expressWs from "express-ws";
22
import { WebSocket, Server } from "ws";
3+
import detectPort from "detect-port";
34

45
import { ReceivedStatusUpdate } from "@webxdc/types";
56
import { createProcessor, IProcessor, WebXdcMulti, OnMessage } from "./message";
@@ -91,12 +92,12 @@ export class Instances {
9192
});
9293
}
9394

94-
add(): Instance {
95-
this.currentPort++;
96-
const port = this.currentPort;
95+
async add(): Promise<Instance> {
96+
const port = await detectPort(this.currentPort + 1);
9797
if (this.instances.has(port)) {
9898
throw new Error(`Already have Webxdc instance at port: ${port}`);
9999
}
100+
this.currentPort = port;
100101

101102
const instanceUrl = getInstanceUrl(port);
102103

backend/program.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ export function createProgram(inject: Inject): Command {
4343
.description(
4444
"Run webxdc-dev simulator with webxdc from dev server URL, .xdc file or dist directory",
4545
)
46-
.action((location, options) => {
47-
run(
46+
.action(async (location, options) => {
47+
await run(
4848
location,
4949
{ basePort: options.port, csp: options.csp, verbose: options.verbose },
5050
inject,

backend/run.ts

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import detectPort from "detect-port";
12
import process from "process";
23
import open from "open";
34

@@ -12,13 +13,18 @@ export type Inject = {
1213
getIndexHtml: () => string;
1314
};
1415

15-
function actualRun(appInfo: AppInfo, options: Options, inject: Inject): void {
16+
async function actualRun(
17+
appInfo: AppInfo,
18+
options: Options,
19+
inject: Inject,
20+
): Promise<void> {
1621
const { injectFrontend, injectSim, getIndexHtml } = inject;
22+
options.basePort = await detectPort(options.basePort);
1723
const instances = new Instances(appInfo, injectSim, options);
1824

1925
const numberOfInstances = 2;
2026
for (let i = 0; i < numberOfInstances; i++) {
21-
instances.add();
27+
await instances.add();
2228
}
2329

2430
const frontend = createFrontend(
@@ -37,7 +43,11 @@ function actualRun(appInfo: AppInfo, options: Options, inject: Inject): void {
3743
open("http://localhost:" + options.basePort);
3844
}
3945

40-
export function run(locationStr: string, options: Options, inject: Inject) {
46+
export async function run(
47+
locationStr: string,
48+
options: Options,
49+
inject: Inject,
50+
): Promise<void> {
4151
let location: Location;
4252
try {
4353
location = getLocation(locationStr);
@@ -57,15 +67,14 @@ export function run(locationStr: string, options: Options, inject: Inject) {
5767

5868
console.log("Starting webxdc project in:", locationStr);
5969

60-
getAppInfo(location)
61-
.then((appInfo) => {
62-
actualRun(appInfo, options, inject);
63-
})
64-
.catch((e) => {
65-
if (e instanceof AppInfoError) {
66-
console.error(e.message);
67-
return;
68-
}
69-
throw e;
70-
});
70+
try {
71+
const appInfo = await getAppInfo(location);
72+
await actualRun(appInfo, options, inject);
73+
} catch (e) {
74+
if (e instanceof AppInfoError) {
75+
console.error(e.message);
76+
return;
77+
}
78+
throw e;
79+
}
7180
}

package-lock.json

Lines changed: 39 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
"adm-zip": "^0.5.9",
8484
"body-parser": "^1.20.0",
8585
"commander": "^9.3.0",
86+
"detect-port": "^2.1.0",
8687
"express": "^4.18.1",
8788
"express-ws": "^5.0.2",
8889
"http-proxy-middleware": "^2.0.6",

0 commit comments

Comments
 (0)