import * as http from "http" ; import * as path from "path" ; import * as fs from "fs" ; import { CancellationToken } from "vscode" ; import { CANCELLATION_ERROR_NAME } from "../debugger/cordovaDebugSession" ; export function generateRandomPortNumber ( ) { return Math . round ( Math . random ( ) * 40000 + 3000 ) ; } export function retryAsync < T > ( func , condition , maxRetries , iteration , delayTime , failure , cancellationToken ? ) { const retry = ( ) => { if ( cancellationToken && cancellationToken . isCancellationRequested ) { let cancelError = new Error ( CANCELLATION_ERROR_NAME ) ; cancelError . name = CANCELLATION_ERROR_NAME ; throw cancelError ; } if ( iteration < maxRetries ) { return delay ( delayTime ) . then ( ( ) => retryAsync ( func , condition , maxRetries , iteration + 1 , delayTime , failure , cancellationToken ) ) ; } throw new Error ( failure ) ; } ; return func ( ) . then ( result => { if ( condition ( result ) ) { return result ; } return retry ( ) ; } , retry ) ; } export function delay ( duration ) { return new Promise < void > ( resolve => setTimeout ( resolve , duration ) ) ; } export function promiseGet ( url , reqErrMessage ) { return new Promise ( ( resolve , reject ) => { let req = http . get ( url , function ( res ) { let responseString = "" ; res . on ( "data" , ( data ) => { responseString += data . toString ( ) ; } ) ; res . on ( "end" , ( ) => { resolve ( responseString ) ; } ) ; } ) ; req . on ( "error" , ( err ) => { this . outputLogger ( reqErrMessage ) ; reject ( err ) ; } ) ; } ) ; } export function findFileInFolderHierarchy ( dir , filename ) { let parentPath ; let projectRoot = dir ; let atFsRoot = false ; while ( ! fs . existsSync ( path . join ( projectRoot , filename ) ) ) { parentPath = path . resolve ( projectRoot , ".." ) ; if ( parentPath !== projectRoot ) { projectRoot = parentPath ; } else { atFsRoot = true ; break ; } } if ( atFsRoot ) { return null ; } return path . join ( projectRoot , filename ) ; } export function isNullOrUndefined ( value ) { return typeof value === "undefined" || value === null ; }