import * as fs from 'fs' ; import * as path from 'path' ; /**
 * A collection of functions to perform path operations
 */ export class PathUtil { private static _pathSepRegex = /(?:\\|\/)+/ ; description param { string } file returns { string } public static EscapePathParts ( file ) { file = unescape ( file ) ; const parts = file . split ( '/' ) ; const newParts = [ ] ; for ( const i in parts ) { if ( parts [ i ] . length > 0 ) { newParts . push ( escape ( parts [ i ] ) ) ; } } return newParts . join ( '/' ) ; } description param { string } file returns { string } public static UnescapePathParts ( file ) { const parts = file . split ( '/' ) ; const newParts = [ ] ; for ( const i in parts ) { if ( parts [ i ] . length > 0 ) { newParts . push ( unescape ( parts [ i ] ) ) ; } } return newParts . join ( '/' ) ; } param { string } file returns { string } public static GetParentDir ( file ) { if ( fs . existsSync ( file ) && fs . statSync ( file ) . isDirectory ( ) ) { return file ; } return path . dirname ( file ) ; } param { string } file returns { string } public static GetImmediateParentDir ( file ) { return PathUtil . GetParentDir ( file ) . split ( this . _pathSepRegex ) . pop ( ) ; } param { string } file param { boolean } returnEmptyOnDir returns { string } public static GetFileName ( file , returnEmptyOnDir = false ) { if ( returnEmptyOnDir && fs . existsSync ( file ) && fs . statSync ( file ) . isDirectory ( ) ) { return '' ; } return path . basename ( file ) ; } param { string } file1 param { string } file2 returns { boolean } public static PathEquals ( file1 , file2 ) { return path . normalize ( file1 ) == path . normalize ( file2 ) ; } param { string } file1 param { string } file2 returns { boolean } public static PathBeginsWith ( file1 , file2 ) { return path . normalize ( file1 ) . startsWith ( path . normalize ( file2 ) ) ; } param { string } file returns { string } public static ConvertToUnixPath ( file ) { return file . replace ( /\\/g , '/' ) ; } param { string } file returns { string } public static GetUserDataDirFromStorageUri ( file ) { if ( ! file ) { return file ; } file = PathUtil . ConvertToUnixPath ( file ) ; const parts = file . split ( '/' ) ; const newParts = [ ] ; for ( const i in parts ) { if ( parts [ i ] . length > 0 ) { newParts . push ( parts [ i ] ) ; } if ( parts [ i ] == 'User' ) { break ; } } return newParts . join ( '/' ) ; } }