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

fix(serve): start listening when watch is ready #1372

Merged
merged 1 commit into from
Jan 25, 2018
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
3 changes: 0 additions & 3 deletions src/dev-server/http-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
23 changes: 17 additions & 6 deletions src/serve.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -65,10 +79,7 @@ export function serve(context: BuildContext) {
if (err && err.isFatal) {
throw err;
} else {
if (config) {
onReady(config, context);
}

finish();
return config;
}
});
Expand Down