File tree Expand file tree Collapse file tree 1 file changed +19
-1
lines changed
Expand file tree Collapse file tree 1 file changed +19
-1
lines changed Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ import * as path from 'path';
33import * as fs from "fs" ;
44import * as os from "os" ;
55import { exec , execSync } from "child_process" ;
6+ import * as net from 'net' ;
67
78export function stripAnsi ( input : string ) : string {
89 const esc = String . fromCharCode ( 27 ) ;
@@ -137,4 +138,21 @@ export function execWithProgress(command: string, progressMessage: string): Then
137138 } ) ;
138139 } )
139140 ) ;
140- }
141+ }
142+
143+ export async function getFreePort ( ) : Promise < number > {
144+ return new Promise ( ( resolve , reject ) => {
145+ const server = net . createServer ( ) ;
146+ server . listen ( 0 , '127.0.0.1' ) ;
147+ server . on ( 'listening' , ( ) => {
148+ const address = server . address ( ) ;
149+ server . close ( ) ;
150+ if ( typeof address === 'object' && address ?. port ) {
151+ resolve ( address . port ) ;
152+ } else {
153+ reject ( new Error ( 'Could not get free port' ) ) ;
154+ }
155+ } ) ;
156+ server . on ( 'error' , reject ) ;
157+ } ) ;
158+ }
You can’t perform that action at this time.
0 commit comments