import * as fs from 'fs' ; import * as path from 'path' ; import * as vscode from 'vscode' ; export class iOSTeamService { public selectTeam ( ) { return new Promise ( ( resolve , reject ) => { const workspaceTeamId = vscode . workspace . getConfiguration ( ) . get < string > ( 'nativescript.iosTeamId' ) ; if ( workspaceTeamId ) { resolve ( { id : workspaceTeamId , name : undefined , } ) ; return ; } const developmentTeams = this . getDevelopmentTeams ( ) ; if ( developmentTeams . length > 1 ) { const quickPickItems = developmentTeams . map ( ( team ) => { return { description : team . id , label : team . name , } ; } ) ; vscode . window . showQuickPick ( quickPickItems , { placeHolder : 'Select your development team' , } ) . then ( ( val ) => { vscode . workspace . getConfiguration ( ) . update ( 'nativescript.iosTeamId' , val . description ) ; resolve ( { id : val . description , name : val . label , } ) ; } ) ; } else { resolve ( ) ; } } ) ; } private getDevelopmentTeams ( ) { try { const dir = path . join ( process . env . HOME , 'Library/MobileDevice/Provisioning Profiles/' ) ; const files = fs . readdirSync ( dir ) ; const teamIds = { } ; for ( const file of files ) { const filePath = path . join ( dir , file ) ; const data = fs . readFileSync ( filePath , { encoding : 'utf8' } ) ; const teamId = this . getProvisioningProfileValue ( 'TeamIdentifier' , data ) ; const teamName = this . getProvisioningProfileValue ( 'TeamName' , data ) ; if ( teamId ) { teamIds [ teamId ] = teamName ; } } const teamIdsArray = new Array < { id : string ; name : string ; } > ( ) ; for ( const teamId in teamIds ) { teamIdsArray . push ( { id : teamId , name : teamIds [ teamId ] } ) ; } return teamIdsArray ; } catch ( e ) { return new Array < { id : string ; name : string ; } > ( ) ; } } private getProvisioningProfileValue ( name , text ) { const findStr = '<key>' + name + '</key>' ; let index = text . indexOf ( findStr ) ; if ( index > 0 ) { index = text . indexOf ( '<string>' , index + findStr . length ) ; if ( index > 0 ) { index += '<string>' . length ; const endIndex = text . indexOf ( '</string>' , index ) ; const result = text . substring ( index , endIndex ) ; return result ; } } return null ; } }