Skip to content

Commit 28c9361

Browse files
committed
Move address output to the listen callback
Since listening is asynchronous, this guarantees the address will be filled out.
1 parent a9d1788 commit 28c9361

File tree

1 file changed

+24
-22
lines changed

1 file changed

+24
-22
lines changed

packages/server/src/cli.ts

+24-22
Original file line numberDiff line numberDiff line change
@@ -281,10 +281,32 @@ const bold = (text: string | number): string | number => {
281281

282282
if (options.socket) {
283283
logger.info("Starting webserver via socket...", field("socket", options.socket));
284-
app.server.listen(options.socket);
284+
app.server.listen(options.socket, () => {
285+
logger.info(" ");
286+
logger.info("Started on socket address:");
287+
logger.info(options.socket!);
288+
logger.info(" ");
289+
});
285290
} else {
286291
logger.info("Starting webserver...", field("host", options.host), field("port", options.port));
287-
app.server.listen(options.port, options.host);
292+
app.server.listen(options.port, options.host, async () => {
293+
const protocol = options.allowHttp ? "http" : "https";
294+
const address = app.server.address();
295+
const port = typeof address === "string" ? options.port : address.port;
296+
const url = `${protocol}://localhost:${port}/`;
297+
logger.info(" ");
298+
logger.info("Started (click the link below to open):");
299+
logger.info(url);
300+
logger.info(" ");
301+
302+
if (options.open) {
303+
try {
304+
await opn(url);
305+
} catch (e) {
306+
logger.warn("Url couldn't be opened automatically.", field("url", url), field("error", e.message));
307+
}
308+
}
309+
});
288310
}
289311
let clientId = 1;
290312
app.wss.on("connection", (ws, req) => {
@@ -327,26 +349,6 @@ const bold = (text: string | number): string | number => {
327349
logger.info(" ");
328350
logger.info("Telemetry is disabled.");
329351
}
330-
331-
logger.info(" ");
332-
if (options.socket) {
333-
logger.info("Started on socket address:");
334-
logger.info(options.socket);
335-
} else {
336-
const protocol = options.allowHttp ? "http" : "https";
337-
const url = `${protocol}://localhost:${app.server.address().port}/`;
338-
logger.info("Started (click the link below to open):");
339-
logger.info(url);
340-
}
341-
logger.info(" ");
342-
343-
if (options.open) {
344-
try {
345-
await opn(url);
346-
} catch (e) {
347-
logger.warn("Url couldn't be opened automatically.", field("url", url), field("exception", e));
348-
}
349-
}
350352
})().catch((ex) => {
351353
logger.error(ex);
352354
});

0 commit comments

Comments
 (0)