1
+ import * as os from 'os' ;
2
+ import * as _ from 'lodash' ;
1
3
import * as express from 'express' ;
2
4
import * as http from 'http' ;
3
5
4
- import { appendToFile , writeMultipleFiles } from '../../utils/fs' ;
6
+ import { appendToFile , writeMultipleFiles , writeFile } from '../../utils/fs' ;
5
7
import {
6
8
killAllProcesses ,
7
9
silentExecAndWaitForOutputToMatch ,
@@ -18,21 +20,32 @@ export default function () {
18
20
const app = express ( ) ;
19
21
const server = http . createServer ( app ) ;
20
22
let liveReloadCount = 0 ;
21
- let liveReloadClientCalled = false ;
22
23
function resetApiVars ( ) {
23
24
liveReloadCount = 0 ;
24
- liveReloadClientCalled = false ;
25
25
}
26
26
27
27
server . listen ( 0 ) ;
28
28
app . set ( 'port' , server . address ( ) . port ) ;
29
+
30
+ const firstLocalIp = _ ( os . networkInterfaces ( ) )
31
+ . values ( )
32
+ . flatten ( )
33
+ . filter ( { family : 'IPv4' , internal : false } )
34
+ . map ( 'address' )
35
+ . first ( ) ;
36
+ const publicHost = `${ firstLocalIp } :4200` ;
37
+
29
38
const apiUrl = `http://localhost:${ server . address ( ) . port } ` ;
30
39
31
40
// This endpoint will be pinged by the main app on each reload.
32
41
app . get ( '/live-reload-count' , _ => liveReloadCount ++ ) ;
33
- // This endpoint will be pinged by webpack to check for live reloads.
34
- app . get ( '/sockjs-node/info' , _ => liveReloadClientCalled = true ) ;
35
42
43
+ const proxyConfigFile = 'proxy.config.json' ;
44
+ const proxyConfig = {
45
+ '/live-reload-count' : {
46
+ target : apiUrl
47
+ }
48
+ } ;
36
49
37
50
return Promise . resolve ( )
38
51
. then ( _ => writeMultipleFiles ( {
@@ -122,15 +135,35 @@ export default function () {
122
135
. then ( _ => killAllProcesses ( ) , ( err ) => { killAllProcesses ( ) ; throw err ; } )
123
136
. then ( _ => resetApiVars ( ) )
124
137
// Serve with live reload client set to api should call api.
138
+ . then ( ( ) => writeFile ( proxyConfigFile , JSON . stringify ( proxyConfig , null , 2 ) ) )
139
+ // Update the component to call the webserver
140
+ . then ( ( ) => writeFile ( './src/app/app.component.ts' ,
141
+ `
142
+ import { Component } from '@angular/core';
143
+ import { Http } from '@angular/http';
144
+ @Component({
145
+ selector: 'app-root',
146
+ template: '<h1>Live reload test</h1>'
147
+ })
148
+ export class AppComponent {
149
+ constructor(private http: Http) {
150
+ http.get('http://${ publicHost + '/live-reload-count' } ').subscribe(res => null);
151
+ }
152
+ }` ) )
125
153
. then ( _ => silentExecAndWaitForOutputToMatch (
126
154
'ng' ,
127
- [ 'e2e' , '--watch' , `--public-host=${ apiUrl } ` ] ,
155
+ [ 'e2e' , '--watch' , '--host=0.0.0.0' , '--port=4200' , `--public-host=${ publicHost } ` , '--proxy' , proxyConfigFile ] ,
128
156
protractorGoodRegEx
129
157
) )
130
158
. then ( _ => wait ( 2000 ) )
159
+ . then ( _ => appendToFile ( 'src/main.ts' , 'console.log(1);' ) )
160
+ . then ( _ => waitForAnyProcessOutputToMatch ( webpackGoodRegEx , 5000 ) )
161
+ . then ( _ => wait ( 2000 ) )
131
162
. then ( _ => {
132
- if ( ! liveReloadClientCalled ) {
133
- throw new Error ( `Expected live-reload client to have been called but it was not.` ) ;
163
+ if ( liveReloadCount != 2 ) {
164
+ throw new Error (
165
+ `Expected API to have been called 2 times but it was called ${ liveReloadCount } times.`
166
+ ) ;
134
167
}
135
168
} )
136
169
. then ( _ => killAllProcesses ( ) , ( err ) => { killAllProcesses ( ) ; throw err ; } )
0 commit comments