'use strict' ; import * as path from 'path' ; import { Uri } from 'vscode' ; import { IConfig } from '../spgo' ; export class UrlHelper { public static ensureLeadingSlash ( urlPath ) { if ( ! urlPath . startsWith ( path . sep ) ) { urlPath = path . sep + urlPath ; } return urlPath ; } public static ensureLeadingWebSlash ( urlPath ) { if ( ! urlPath . startsWith ( '/' ) ) { urlPath = '/' + urlPath ; } return urlPath ; } public static ensureTailingWebSlash ( urlPath ) { if ( ! urlPath . endsWith ( '/' ) ) { urlPath = urlPath + '/' ; } return urlPath ; } public static formatUriAsFileName ( uri ) { return uri . replace ( /[^a-zA-Z0-9]/g , '_' ) ; } public static formatWebFolder ( urlPath ) { if ( ! urlPath . startsWith ( '/' ) ) { urlPath = '/' + urlPath ; } if ( ! urlPath . endsWith ( '/' ) ) { urlPath = urlPath + '/' ; } return urlPath ; } public static getFileName ( urlPath ) { return urlPath . split ( '\\' ) . pop ( ) . split ( '/' ) . pop ( ) ; } public static getSiteRelativeFilePath ( fileName , config ) { return fileName . split ( config . sourceRoot + path . sep ) [ 1 ] . toString ( ) ; } public static getSiteRelativeFileUri ( fileName , config ) { return Uri . parse ( this . getSiteRelativeFilePath ( fileName , config ) ) ; } public static getServerRelativeFilePath ( fileName , config ) { let relativeFilePath = this . getSiteRelativeFilePath ( fileName , config ) ; let remoteFolder = relativeFilePath . substring ( 0 , relativeFilePath . lastIndexOf ( path . sep ) ) ; let remoteFileName = this . getFileName ( relativeFilePath ) ; let remoteFileUrl = this . normalizeSlashes ( UrlHelper . formatWebFolder ( remoteFolder ) + remoteFileName ) ; return this . removeTrailingSlash ( config . sharePointSiteUrl ) + remoteFileUrl ; } public static getServerRelativeFileUri ( fileName , config ) { return Uri . parse ( this . getServerRelativeFilePath ( fileName , config ) ) ; } public static isFile ( uri ) { const fileName = this . getFileName ( uri ) ; return fileName . includes ( '.' ) && fileName != '.' && fileName . indexOf ( '*' ) < 0 ; } static normalizeSlashes ( urlPath ) { return urlPath . replace ( /\\/g , "/" ) ; } public static osAwareGlobStar ( ) { return path . sep + '**' + path . sep + '*.*' ; } public static removeTrailingSlash ( url ) { return url . replace ( /(\/$)|(\\$)/ , '' ) ; } public static removeLeadingSlash ( url ) { return url . replace ( /(^\/)|(^\\)/ , '' ) ; } public static trimSlashes ( url ) { return url . replace ( /(^\/)|(^\\)|(\/$)|(\\$)/g , '' ) ; } }