1
+ import * as express from 'express' ;
1
2
import { BuildContext } from './util/interfaces' ;
2
3
import { getConfigValue , hasConfigValue } from './util/config' ;
3
4
import { BuildError } from './util/errors' ;
@@ -20,11 +21,24 @@ export function serve(context: BuildContext) {
20
21
setContext ( context ) ;
21
22
22
23
let config : ServeConfig ;
24
+ let httpServer : express . Application ;
23
25
const host = getHttpServerHost ( context ) ;
24
26
const notificationPort = getNotificationPort ( context ) ;
25
27
const liveReloadServerPort = getLiveReloadServerPort ( context ) ;
26
28
const hostPort = getHttpServerPort ( context ) ;
27
29
30
+ function finish ( ) {
31
+ if ( config ) {
32
+ if ( httpServer ) {
33
+ httpServer . listen ( config . httpPort , config . host , function ( ) {
34
+ Logger . debug ( `listening on ${ config . httpPort } ` ) ;
35
+ } ) ;
36
+ }
37
+
38
+ onReady ( config , context ) ;
39
+ }
40
+ }
41
+
28
42
return findClosestOpenPorts ( host , [ notificationPort , liveReloadServerPort , hostPort ] )
29
43
. then ( ( [ notificationPortFound , liveReloadServerPortFound , hostPortFound ] ) => {
30
44
const hostLocation = ( host === '0.0.0.0' ) ? 'localhost' : host ;
@@ -51,12 +65,12 @@ export function serve(context: BuildContext) {
51
65
52
66
createNotificationServer ( config ) ;
53
67
createLiveReloadServer ( config ) ;
54
- createHttpServer ( config ) ;
68
+ httpServer = createHttpServer ( config ) ;
55
69
56
70
return watch ( context ) ;
57
71
} )
58
72
. then ( ( ) => {
59
- onReady ( config , context ) ;
73
+ finish ( ) ;
60
74
return config ;
61
75
} , ( err : BuildError ) => {
62
76
throw err ;
@@ -65,10 +79,7 @@ export function serve(context: BuildContext) {
65
79
if ( err && err . isFatal ) {
66
80
throw err ;
67
81
} else {
68
- if ( config ) {
69
- onReady ( config , context ) ;
70
- }
71
-
82
+ finish ( ) ;
72
83
return config ;
73
84
}
74
85
} ) ;
0 commit comments