import * as path from 'path' ; import * as fs from 'fs' ; export type ParsedPath = path . ParsedPath ; export class Path { remarks see https public static usingCaseSensitive : boolean = Path . _detectCaseSensitive ( ) ; private static _detectCaseSensitive ( ) { return ! fs . existsSync ( __filename . toUpperCase ( ) ) ; } private static _trimTrailingSlashes ( inputPath ) { return inputPath . replace ( /(?<=[^\/\\])[\/\\]+$/ , '' ) ; } private static _relativeCaseInsensitive ( from , to ) { const normalizedTo = Path . _trimTrailingSlashes ( path . normalize ( to ) ) ; const uppercasedFrom = from . toUpperCase ( ) ; const uppercasedTo = normalizedTo . toUpperCase ( ) ; const uppercasedResult = path . relative ( uppercasedFrom , uppercasedTo ) ; if ( uppercasedResult . toLowerCase ( ) === uppercasedResult ) { return uppercasedResult ; } let resultIndex = uppercasedResult . length ; let toIndex = normalizedTo . length ; for ( ; ; ) { if ( resultIndex === 0 || toIndex === 0 ) { break ; } if ( uppercasedResult . charCodeAt ( resultIndex - 1 ) !== uppercasedTo . charCodeAt ( toIndex - 1 ) ) { break ; } -- resultIndex ; -- toIndex ; } return uppercasedResult . substring ( 0 , resultIndex ) + normalizedTo . substring ( toIndex ) ; } public static relative ( from , to ) { if ( ! Path . usingCaseSensitive ) { return Path . _relativeCaseInsensitive ( from , to ) ; } return path . relative ( from , to ) ; } public static dirname ( p ) { return path . dirname ( p ) ; } public static join ( ... paths ) { return path . join ( ... paths ) ; } public static resolve ( ... pathSegments ) { return path . resolve ( ... pathSegments ) ; } public static parse ( pathString ) { return path . parse ( pathString ) ; } private static _relativePathRegex : RegExp = /^[.\/\\]+$/ ; remarks public static isUnder ( childPath , parentFolderPath ) { const relativePath = Path . relative ( childPath , parentFolderPath ) ; return Path . _relativePathRegex . test ( relativePath ) ; } remarks public static isEqual ( path1 , path2 ) { return Path . relative ( path1 , path2 ) === '' ; } remarks public static convertToSlashes ( inputPath ) { return inputPath . split ( '\\' ) . join ( '/' ) ; } }