1+ import * as os from 'os' ;
2+ import * as _ from 'lodash' ;
13import * as express from 'express' ;
24import * as http from 'http' ;
35
4- import { appendToFile , writeMultipleFiles } from '../../utils/fs' ;
6+ import { appendToFile , writeMultipleFiles , writeFile } from '../../utils/fs' ;
57import {
68 killAllProcesses ,
79 silentExecAndWaitForOutputToMatch ,
@@ -18,21 +20,32 @@ export default function () {
1820 const app = express ( ) ;
1921 const server = http . createServer ( app ) ;
2022 let liveReloadCount = 0 ;
21- let liveReloadClientCalled = false ;
2223 function resetApiVars ( ) {
2324 liveReloadCount = 0 ;
24- liveReloadClientCalled = false ;
2525 }
2626
2727 server . listen ( 0 ) ;
2828 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+
2938 const apiUrl = `http://localhost:${ server . address ( ) . port } ` ;
3039
3140 // This endpoint will be pinged by the main app on each reload.
3241 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 ) ;
3542
43+ const proxyConfigFile = 'proxy.config.json' ;
44+ const proxyConfig = {
45+ '/live-reload-count' : {
46+ target : apiUrl
47+ }
48+ } ;
3649
3750 return Promise . resolve ( )
3851 . then ( _ => writeMultipleFiles ( {
@@ -122,15 +135,35 @@ export default function () {
122135 . then ( _ => killAllProcesses ( ) , ( err ) => { killAllProcesses ( ) ; throw err ; } )
123136 . then ( _ => resetApiVars ( ) )
124137 // 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+ }` ) )
125153 . then ( _ => silentExecAndWaitForOutputToMatch (
126154 'ng' ,
127- [ 'e2e' , '--watch' , `--public-host=${ apiUrl } ` ] ,
155+ [ 'e2e' , '--watch' , '--host=0.0.0.0' , '--port=4200' , `--public-host=${ publicHost } ` , '--proxy' , proxyConfigFile ] ,
128156 protractorGoodRegEx
129157 ) )
130158 . then ( _ => wait ( 2000 ) )
159+ . then ( _ => appendToFile ( 'src/main.ts' , 'console.log(1);' ) )
160+ . then ( _ => waitForAnyProcessOutputToMatch ( webpackGoodRegEx , 5000 ) )
161+ . then ( _ => wait ( 2000 ) )
131162 . 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+ ) ;
134167 }
135168 } )
136169 . then ( _ => killAllProcesses ( ) , ( err ) => { killAllProcesses ( ) ; throw err ; } )
0 commit comments