22
33const { readdir : _readdir , readdirSync } = require ( 'fs' )
44const { platform } = require ( 'os' )
5- const { isAbsolute } = require ( 'path' )
5+ const { isAbsolute, normalize } = require ( 'path' )
66const { promisify : pify } = require ( 'util' )
77
88const readdir = pify ( _readdir )
@@ -22,10 +22,6 @@ function escapeString(str) {
2222 return str . replace ( / [ . * + ? ^ $ { } ( ) | [ \] \\ ] / g, '\\$&' )
2323}
2424
25- function isDriveLetter ( str ) {
26- return / [ a - z A - Z ] : / . test ( str )
27- }
28-
2925function matchCaseInsensitive ( fileOrDirectory , directoryContents , filePath ) {
3026 const caseInsensitiveRegex = new RegExp (
3127 `^${ escapeString ( fileOrDirectory ) } $` ,
@@ -41,14 +37,28 @@ function matchCaseInsensitive(fileOrDirectory, directoryContents, filePath) {
4137
4238function _trueCasePath ( { sync } ) {
4339 return ( filePath , basePath ) => {
44- if ( basePath && ! isAbsolute ( basePath ) ) {
45- throw new Error (
46- `[true-case-path]: basePath argument must be absolute. Received "${ basePath } "`
47- )
40+ if ( basePath ) {
41+ if ( ! isAbsolute ( basePath ) ) {
42+ throw new Error (
43+ `[true-case-path]: basePath argument must be absolute. Received "${ basePath } "`
44+ )
45+ }
46+ basePath = normalize ( basePath )
4847 }
48+ filePath = normalize ( filePath )
4949 const segments = getRelevantFilePathSegments ( filePath )
50- if ( ! basePath ) basePath = isAbsolute ( filePath ) ? '' : process . cwd ( )
51- if ( isDriveLetter ( segments [ 0 ] ) ) segments [ 0 ] = segments [ 0 ] . toUpperCase ( )
50+ if ( isAbsolute ( filePath ) ) {
51+ if ( basePath ) {
52+ throw new Error (
53+ '[true-case-path]: filePath must be relative when used with basePath'
54+ )
55+ }
56+ basePath = isWindows
57+ ? segments . shift ( ) . toUpperCase ( ) // drive letter
58+ : ''
59+ } else if ( ! basePath ) {
60+ basePath = process . cwd ( )
61+ }
5262 return sync
5363 ? iterateSync ( basePath , filePath , segments )
5464 : iterateAsync ( basePath , filePath , segments )
0 commit comments