'use strict' ; import * as vscode from 'vscode' ; import { IConfig } from '../spgo' ; import { Uri , WorkspaceFolder } from 'vscode' ; import { FileHelper } from '../util/fileHelper' ; export class WorkspaceHelper { static getActiveFile ( ) { let selectedResource = Uri . parse ( '' ) ; if ( vscode . window . activeTextEditor && vscode . window . activeTextEditor . document && vscode . window . activeTextEditor . document . uri . scheme !== 'output' ) { selectedResource = vscode . window . activeTextEditor . document ? vscode . window . activeTextEditor . document . uri : Uri . parse ( '' ) ; } return selectedResource ; } static getActiveWorkspaceUri ( ) { return new Promise ( ( resolve , reject ) => { let activeWorkspace = vscode . workspace . workspaceFolders [ 0 ] . uri ; if ( vscode . workspace . workspaceFolders . length > 1 ) { if ( vscode . window . activeTextEditor && vscode . window . activeTextEditor . document && vscode . window . activeTextEditor . document . uri . scheme !== 'output' ) { const selectedResource = vscode . window . activeTextEditor . document ? vscode . window . activeTextEditor . document . uri : Uri . parse ( '' ) ; activeWorkspace = vscode . workspace . getWorkspaceFolder ( selectedResource ) . uri ; } else { let options = { canPickMany : false , ignoreFocusOut : true } ; return vscode . window . showQuickPick ( vscode . workspace . workspaceFolders . map ( ( folder ) => { return folder . uri . fsPath ; } ) , options ) . then ( ( result ) => { if ( ! result ) { reject ( "No workspace selected." ) ; } activeWorkspace = FileHelper . convertToUri ( result ) ; resolve ( activeWorkspace ) ; } ) ; } } resolve ( activeWorkspace ) ; } ) ; } static getSiteUriForActiveWorkspace ( fileUri , config ) { let matchCount = fileUri . replace ( config . sharePointSiteUrl , '' ) . length ; let subSiteMatchCount = 0 ; let siteUri = Uri . parse ( config . sharePointSiteUrl ) ; if ( config . subSites ) { for ( let subSite of config . subSites ) { subSiteMatchCount = fileUri . replace ( subSite . sharePointSiteUrl , '' ) . length ; if ( subSiteMatchCount < matchCount ) { siteUri = Uri . parse ( subSite . sharePointSiteUrl ) ; } } } return siteUri ; } }