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

fix(serve): assign all ports dynamically #727

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions src/serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { createNotificationServer } from './dev-server/notification-server';
import { createHttpServer } from './dev-server/http-server';
import { createLiveReloadServer } from './dev-server/live-reload';
import { ServeConfig, IONIC_LAB_URL } from './dev-server/serve-config';
import { findClosestOpenPort } from './util/network';
import { findClosestOpenPorts } from './util/network';

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

let config: ServeConfig;
const notificationPort = getNotificationPort(context);
const host = getHttpServerHost(context);
const notificationPort = getNotificationPort(context);
const liveReloadServerPort = getLiveReloadServerPort(context);
const hostPort = getHttpServerPort(context);

return findClosestOpenPort(host, notificationPort)
.then((notificationPortFound) => {
const hostPort = getHttpServerPort(context);
return findClosestOpenPorts(host, [notificationPort, liveReloadServerPort, hostPort])
.then(([notificationPortFound, liveReloadServerPortFound, hostPortFound]) => {
const hostLocation = (host === '0.0.0.0') ? 'localhost' : host;

config = {
httpPort: hostPort,
httpPort: hostPortFound,
host: host,
hostBaseUrl: `http://${hostLocation}:${hostPort}`,
hostBaseUrl: `http://${hostLocation}:${hostPortFound}`,
rootDir: context.rootDir,
wwwDir: context.wwwDir,
buildDir: context.buildDir,
Expand All @@ -40,7 +41,7 @@ export function serve(context: BuildContext) {
launchLab: launchLab(context),
browserToLaunch: browserToLaunch(context),
useLiveReload: useLiveReload(context),
liveReloadPort: getLiveReloadServerPort(context),
liveReloadPort: liveReloadServerPortFound,
notificationPort: notificationPortFound,
useServerLogs: useServerLogs(context),
useProxy: useProxy(context),
Expand Down
5 changes: 5 additions & 0 deletions src/util/network.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import * as net from 'net';

export function findClosestOpenPorts(host: string, ports: number[]): Promise<number[]> {
const promises = ports.map(port => findClosestOpenPort(host, port));
return Promise.all(promises);
}

export function findClosestOpenPort(host: string, port: number): Promise<number> {
function t(portToCheck: number): Promise<number> {
return isPortTaken(host, portToCheck).then(isTaken => {
Expand Down