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

Commit

Permalink
fix(serve): assign all ports dynamically (#727)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
huerlisi authored and danbucholtz committed Feb 9, 2017
1 parent 55fb91a commit 6b4115c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
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

0 comments on commit 6b4115c

Please sign in to comment.