11import path from 'path' ;
22import express from 'express' ;
33import { onExit } from 'signal-exit' ;
4- import BluebirdPromise from 'bluebird' ;
54import bodyParser from 'body-parser' ;
65import { INTERNAL_SERVER_ERROR , OK } from 'http-codes' ;
76import type { Config } from 'testplane' ;
7+ import { listenWithFallback } from './listen-with-fallback' ;
88
99import { App } from './app' ;
10- import { MAX_REQUEST_SIZE , KEEP_ALIVE_TIMEOUT , HEADERS_TIMEOUT } from './constants' ;
10+ import { MAX_REQUEST_SIZE } from './constants' ;
1111import { logger } from '../common-utils' ;
1212import { initPluginsRoutes } from './routes/plugins' ;
1313import { BrowserFeature , Feature , ToolName } from '../constants' ;
@@ -19,6 +19,7 @@ import type {TestplaneToolAdapter} from '../adapters/tool/testplane';
1919import type { ToolRunnerTree } from './tool-runner' ;
2020import type { TestplaneConfigAdapter } from '../adapters/config/testplane' ;
2121import type { UpdateTimeTravelSettingsRequest , UpdateTimeTravelSettingsResponse } from '../types' ;
22+ import chalk from 'chalk' ;
2223
2324interface CustomGuiError {
2425 response : {
@@ -275,14 +276,23 @@ export const start = async (args: ServerArgs): Promise<ServerReadyData> => {
275276
276277 await app . initialize ( ) ;
277278
278- const { port, hostname} = args . cli . options ;
279- await BluebirdPromise . fromCallback ( ( callback ) => {
280- const httpServer = server . listen ( port , hostname , callback as ( ) => void ) ;
281- httpServer . keepAliveTimeout = KEEP_ALIVE_TIMEOUT ;
282- httpServer . headersTimeout = HEADERS_TIMEOUT ;
279+ const { port : requestedPort , hostname} = args . cli . options ;
280+
281+ const { actualPort, hostnameForUrl} = await listenWithFallback ( {
282+ server,
283+ hostname,
284+ requestedPort
283285 } ) ;
284286
285- const data = { url : `http://${ hostname } :${ port } ` } ;
287+ const listeningMessage = `GUI is running at ${ chalk . cyan ( `http://${ hostnameForUrl } :${ actualPort } ` ) } ` ;
288+
289+ if ( requestedPort !== undefined && actualPort !== requestedPort ) {
290+ logger . log ( `${ listeningMessage } . This port was chosen because ${ hostnameForUrl } :${ requestedPort } is busy.` ) ;
291+ } else {
292+ logger . log ( listeningMessage ) ;
293+ }
294+
295+ const data = { url : `http://${ hostnameForUrl } :${ actualPort } ` } ;
286296
287297 await guiApi . serverReady ( data ) ;
288298
0 commit comments