Skip to content
This repository was archived by the owner on May 1, 2020. It is now read-only.

Commit 6b4115c

Browse files
huerlisidanbucholtz
authored andcommitted
fix(serve): assign all ports dynamically (#727)
Up to now only the logger port was dynamically increased if another app is running already. This meant that manually specifying port arguments for server and live reload was needed when directly using the `ionic-app-scripts server` command. The `ionic` CLI command does already take care of this. This patch ensures all the ports are chosen dynamically, so it's now possible to run two applications without any manual intervention.
1 parent 55fb91a commit 6b4115c

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

src/serve.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { createNotificationServer } from './dev-server/notification-server';
99
import { createHttpServer } from './dev-server/http-server';
1010
import { createLiveReloadServer } from './dev-server/live-reload';
1111
import { ServeConfig, IONIC_LAB_URL } from './dev-server/serve-config';
12-
import { findClosestOpenPort } from './util/network';
12+
import { findClosestOpenPorts } from './util/network';
1313

1414
const DEV_LOGGER_DEFAULT_PORT = 53703;
1515
const LIVE_RELOAD_DEFAULT_PORT = 35729;
@@ -20,18 +20,19 @@ export function serve(context: BuildContext) {
2020
setContext(context);
2121

2222
let config: ServeConfig;
23-
const notificationPort = getNotificationPort(context);
2423
const host = getHttpServerHost(context);
24+
const notificationPort = getNotificationPort(context);
25+
const liveReloadServerPort = getLiveReloadServerPort(context);
26+
const hostPort = getHttpServerPort(context);
2527

26-
return findClosestOpenPort(host, notificationPort)
27-
.then((notificationPortFound) => {
28-
const hostPort = getHttpServerPort(context);
28+
return findClosestOpenPorts(host, [notificationPort, liveReloadServerPort, hostPort])
29+
.then(([notificationPortFound, liveReloadServerPortFound, hostPortFound]) => {
2930
const hostLocation = (host === '0.0.0.0') ? 'localhost' : host;
3031

3132
config = {
32-
httpPort: hostPort,
33+
httpPort: hostPortFound,
3334
host: host,
34-
hostBaseUrl: `http://${hostLocation}:${hostPort}`,
35+
hostBaseUrl: `http://${hostLocation}:${hostPortFound}`,
3536
rootDir: context.rootDir,
3637
wwwDir: context.wwwDir,
3738
buildDir: context.buildDir,
@@ -40,7 +41,7 @@ export function serve(context: BuildContext) {
4041
launchLab: launchLab(context),
4142
browserToLaunch: browserToLaunch(context),
4243
useLiveReload: useLiveReload(context),
43-
liveReloadPort: getLiveReloadServerPort(context),
44+
liveReloadPort: liveReloadServerPortFound,
4445
notificationPort: notificationPortFound,
4546
useServerLogs: useServerLogs(context),
4647
useProxy: useProxy(context),

src/util/network.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import * as net from 'net';
22

3+
export function findClosestOpenPorts(host: string, ports: number[]): Promise<number[]> {
4+
const promises = ports.map(port => findClosestOpenPort(host, port));
5+
return Promise.all(promises);
6+
}
7+
38
export function findClosestOpenPort(host: string, port: number): Promise<number> {
49
function t(portToCheck: number): Promise<number> {
510
return isPortTaken(host, portToCheck).then(isTaken => {

0 commit comments

Comments
 (0)