import * as path from "path" ; import * as fs from "fs" ; import stripJsonComments = require ( "strip-json-comments" ) ; import { objectsContains , waitUntil } from "./utilities" ; export interface IConfiguration { name : string ; platform ? : string ; target ? : string ; type ? : string ; request ? : string ; useHermesEngine ? : boolean ; } export interface ILaunchScenarios { configurations ? : IConfiguration [ ] ; } export class LaunchConfigurationManager { private static LAUNCH_CONFIG_UPDATE_TIMEOUT = 30000 ; private pathToLaunchFile : string ; private launchScenarios : ILaunchScenarios ; constructor ( workspaceDirectory ) { this . pathToLaunchFile = path . resolve ( workspaceDirectory , ".vscode" , "launch.json" ) ; this . readLaunchScenarios ( ) ; } public getLaunchScenarios ( ) { return this . launchScenarios ; } public getConfigurationsCount ( ) { return this . launchScenarios . configurations ? this . launchScenarios . configurations . length : 0 ; } private getScenarioByName ( scenarioName ) { if ( this . launchScenarios . configurations ) { for ( let i = 0 ; i < this . launchScenarios . configurations . length ; i ++ ) { const config = this . launchScenarios . configurations [ i ] ; if ( scenarioName === config . name ) { return i ; } } } return null ; } private writeLaunchScenarios ( launch = this . launchScenarios ) { if ( fs . existsSync ( this . pathToLaunchFile ) ) { fs . writeFileSync ( this . pathToLaunchFile , JSON . stringify ( launch , null , 4 ) ) ; } } public readLaunchScenarios ( ) { if ( fs . existsSync ( this . pathToLaunchFile ) ) { const content = fs . readFileSync ( this . pathToLaunchFile , "utf8" ) ; this . launchScenarios = JSON . parse ( stripJsonComments ( content ) ) ; } return this . launchScenarios ; } public updateLaunchScenario ( configName , updates ) { this . readLaunchScenarios ( ) ; let launchConfigIndex = this . getScenarioByName ( configName ) ; const launchScenarios = this . getLaunchScenarios ( ) ; if ( launchConfigIndex !== null && launchScenarios . configurations ) { Object . assign ( launchScenarios . configurations [ launchConfigIndex ] , updates ) ; this . writeLaunchScenarios ( launchScenarios ) ; } } public waitUntilLaunchScenarioUpdate ( updates , configName ? ) { const condition = ( ) : boolean => { const configs = this . readLaunchScenarios ( ) . configurations ; if ( configs ) { if ( configName ) { const index = this . getScenarioByName ( configName ) ; if ( index !== null ) { return objectsContains ( configs [ index ] , updates ) ; } } else { configs . forEach ( config => { if ( objectsContains ( config , updates ) ) { return true ; } } ) ; } } return false ; } ; return waitUntil ( condition , LaunchConfigurationManager . LAUNCH_CONFIG_UPDATE_TIMEOUT ) ; } }