'use strict' ; import * as vscode from 'vscode' ; import { Uri } from 'vscode' ; import { Logger } from '../util/logger' ; import { UiHelper } from '../util/uiHelper' ; import { UrlHelper } from '../util/urlHelper' ; import { ErrorHelper } from '../util/errorHelper' ; import { IError , IConfig , ICommand } from '../spgo' ; import { SPFileService } from '../service/spFileService' ; import { WorkspaceHelper } from '../util/workspaceHelper' ; import { AuthenticationService } from '../service/authenticationService' ; export class PopulateWorkspaceCommand implements ICommand { param { vscode . Uri } resourcePath param { boolean } absolute execute ( _fileUri , _props ? ) { try { return UiHelper . showStatusBarProgress ( 'Populating Workspace' , WorkspaceHelper . getActiveWorkspaceUri ( ) . then ( ( activeWorkspace ) => vscode . window . spgo . initialize ( activeWorkspace ) ) . then ( ( config ) => { return AuthenticationService . verifyCredentials ( vscode . window . spgo , config ) . then ( ( ) => ( this . downloadFiles ( config ) ) . then ( ( ) => Logger . updateStatusBar ( 'File Download Complete.' , 5 ) ) ) ; } ) . catch ( err => ErrorHelper . handleError ( err ) ) ) ; } catch ( err ) { ErrorHelper . handleError ( err , true ) ; } } public downloadFiles ( config ) { return new Promise ( ( resolve , reject ) => { let fileService = new SPFileService ( config ) ; Logger . outputMessage ( 'Starting File Synchronization...' , vscode . window . spgo . outputChannel ) ; if ( config . remoteFolders ) { let downloads = [ ] ; for ( let path of config . remoteFolders ) { if ( UrlHelper . isFile ( path ) ) { downloads . push ( fileService . downloadFileMajorVersion ( Uri . file ( config . sourceRoot + UrlHelper . normalizeSlashes ( path ) ) , config . sourceRoot ) ) ; } else { downloads . push ( fileService . downloadFiles ( Uri . parse ( config . sharePointSiteUrl ) , decodeURI ( path ) ) ) ; } } if ( config . subSites && config . subSites . length > 0 ) { for ( let subSite of config . subSites ) { for ( let folder of subSite . remoteFolders ) { downloads . push ( fileService . downloadFiles ( Uri . parse ( subSite . sharePointSiteUrl ) , decodeURI ( folder ) ) ) ; } } } Promise . all ( downloads ) . then ( ( ) => { Logger . outputMessage ( `file synchronization complete.` , vscode . window . spgo . outputChannel ) ; resolve ( `file synchronization complete.` ) ; } ) ; } else { let error = { message : '"remoteFolders":[string] property not configured in workspace configuration file.' } ; Logger . showError ( error . message , error ) ; reject ( error ) ; } } ) ; } }