@@ -9,11 +9,13 @@ import {
9
9
replaceInFile ,
10
10
writeFile ,
11
11
} from '../../utils/fs' ;
12
+ import { findFreePort } from '../../utils/network' ;
12
13
import { installPackage } from '../../utils/packages' ;
13
14
import { ng } from '../../utils/process' ;
14
15
import { updateJsonFile } from '../../utils/project' ;
15
- import { expectToFail } from '../../utils/utils' ;
16
16
import { readNgVersion } from '../../utils/version' ;
17
+ import { Server } from 'http' ;
18
+ import { AddressInfo } from 'net' ;
17
19
18
20
// Configurations for each locale.
19
21
export const baseDir = 'dist/test-project' ;
@@ -67,13 +69,33 @@ export const langTranslations = [
67
69
] ;
68
70
export const sourceLocale = langTranslations [ 0 ] . lang ;
69
71
70
- export const externalServer = ( outputPath : string , baseUrl = '/' ) => {
72
+ export interface ExternalServer {
73
+ readonly server : Server ;
74
+ readonly port : number ;
75
+ readonly url : string ;
76
+ }
77
+
78
+ /**
79
+ * Create an `express` `http.Server` listening on a random port.
80
+ *
81
+ * Call .close() on the server return value to close the server.
82
+ */
83
+ export async function externalServer ( outputPath : string , baseUrl = '/' ) : Promise < ExternalServer > {
71
84
const app = express ( ) ;
72
85
app . use ( baseUrl , express . static ( resolve ( outputPath ) ) ) ;
73
86
74
- // call .close() on the return value to close the server.
75
- return app . listen ( 4200 , 'localhost' ) ;
76
- } ;
87
+ return new Promise ( ( resolve ) => {
88
+ const server = app . listen ( 0 , 'localhost' , ( ) => {
89
+ const { port } = server . address ( ) as AddressInfo ;
90
+
91
+ resolve ( {
92
+ server,
93
+ port,
94
+ url : `http://localhost:${ port } ${ baseUrl } ` ,
95
+ } ) ;
96
+ } ) ;
97
+ } ) ;
98
+ }
77
99
78
100
export const formats = {
79
101
'xlf' : {
0 commit comments