import net from 'net' ; import { chalk } from './chalk' ; import { logger } from './logger' ; param port param tryLimits returns export const getPort = async ( port , tryLimits = 20 ) : Promise < number > => { if ( typeof port === 'string' ) { port = parseInt ( port , 10 ) ; } const original = port ; let found = false ; let attempts = 0 ; while ( ! found && attempts <= tryLimits ) { try { await new Promise ( ( resolve , reject ) => { const server = net . createServer ( ) ; server . unref ( ) ; server . on ( 'error' , reject ) ; server . listen ( { port , host : '0.0.0.0' , } , ( ) => { found = true ; server . close ( resolve ) ; } ) ; } ) ; } catch ( e ) { if ( e . code !== 'EADDRINUSE' ) { throw e ; } port ++ ; attempts ++ ; } } if ( port !== original ) { logger . info ( chalk . red ( `Something is already running on port ${ original }. ${ chalk . yellow ( `Use port ${ port } instead.` ) }` ) ) ; } return port ; } ;