From 06bbd060c2458c26a2be29adbb4f9b77c2a4a8ac Mon Sep 17 00:00:00 2001 From: Daniel Imhoff Date: Wed, 24 Jan 2018 17:26:31 -0600 Subject: [PATCH] fix(serve): start listening when watch is ready --- src/dev-server/http-server.ts | 3 --- src/serve.ts | 23 +++++++++++++++++------ 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/dev-server/http-server.ts b/src/dev-server/http-server.ts index 17853903..c518a34f 100644 --- a/src/dev-server/http-server.ts +++ b/src/dev-server/http-server.ts @@ -28,9 +28,6 @@ export function createHttpServer(config: ServeConfig): express.Application { const app = express(); app.set('serveConfig', config); - app.listen(config.httpPort, config.host, function() { - Logger.debug(`listening on ${config.httpPort}`); - }); app.get('/', serveIndex); app.use('/', express.static(config.wwwDir)); diff --git a/src/serve.ts b/src/serve.ts index 25eacf4f..043d87c6 100644 --- a/src/serve.ts +++ b/src/serve.ts @@ -1,3 +1,4 @@ +import * as express from 'express'; import { BuildContext } from './util/interfaces'; import { getConfigValue, hasConfigValue } from './util/config'; import { BuildError } from './util/errors'; @@ -20,11 +21,24 @@ export function serve(context: BuildContext) { setContext(context); let config: ServeConfig; + let httpServer: express.Application; const host = getHttpServerHost(context); const notificationPort = getNotificationPort(context); const liveReloadServerPort = getLiveReloadServerPort(context); const hostPort = getHttpServerPort(context); + function finish() { + if (config) { + if (httpServer) { + httpServer.listen(config.httpPort, config.host, function() { + Logger.debug(`listening on ${config.httpPort}`); + }); + } + + onReady(config, context); + } + } + return findClosestOpenPorts(host, [notificationPort, liveReloadServerPort, hostPort]) .then(([notificationPortFound, liveReloadServerPortFound, hostPortFound]) => { const hostLocation = (host === '0.0.0.0') ? 'localhost' : host; @@ -51,12 +65,12 @@ export function serve(context: BuildContext) { createNotificationServer(config); createLiveReloadServer(config); - createHttpServer(config); + httpServer = createHttpServer(config); return watch(context); }) .then(() => { - onReady(config, context); + finish(); return config; }, (err: BuildError) => { throw err; @@ -65,10 +79,7 @@ export function serve(context: BuildContext) { if (err && err.isFatal) { throw err; } else { - if (config) { - onReady(config, context); - } - + finish(); return config; } });