11import convertPathToPosix from "./convert-path-to-posix" ;
2- import path from "path" ;
2+ import path , { win32 } from "path" ;
3+
34const forwardSlashPattern = / \/ / g;
45const protocolPattern = / ^ ( \w { 2 , } ) : \/ \/ / i;
56const jsonPointerSlash = / ~ 1 / g;
@@ -9,7 +10,10 @@ import { join } from "path";
910import { isWindows } from "./is-windows" ;
1011
1112// RegExp patterns to URL-encode special characters in local filesystem paths
12- const urlEncodePatterns = [ / \? / g, "%3F" , / # / g, "%23" ] ;
13+ const urlEncodePatterns = [
14+ [ / \? / g, "%3F" ] ,
15+ [ / # / g, "%23" ] ,
16+ ] as [ RegExp , string ] [ ] ;
1317
1418// RegExp patterns to URL-decode special characters for local filesystem paths
1519const urlDecodePatterns = [ / % 2 3 / g, "#" , / % 2 4 / g, "$" , / % 2 6 / g, "&" , / % 2 C / g, "," , / % 4 0 / g, "@" ] ;
@@ -177,17 +181,17 @@ export function isFileSystemPath(path: string | undefined) {
177181 * @param path
178182 * @returns
179183 */
180- export function fromFileSystemPath ( path : any ) {
184+ export function fromFileSystemPath ( path : string ) {
181185 // Step 1: On Windows, replace backslashes with forward slashes,
182186 // rather than encoding them as "%5C"
183187 if ( isWindows ( ) ) {
184- const projectDir = join ( __dirname , ".." , ".." ) ;
188+ const projectDir = cwd ( ) ;
185189 const upperPath = path . toUpperCase ( ) ;
186190 const projectDirPosixPath = convertPathToPosix ( projectDir ) ;
187191 const posixUpper = projectDirPosixPath . toUpperCase ( ) ;
188192 const hasProjectDir = upperPath . includes ( posixUpper ) ;
189193 const hasProjectUri = upperPath . includes ( posixUpper ) ;
190- const isAbsolutePath = path ?. win32 ?. isAbsolute ( path ) ;
194+ const isAbsolutePath = win32 ?. isAbsolute ( path ) ;
191195
192196 if ( ! ( hasProjectDir || hasProjectUri || isAbsolutePath ) ) {
193197 path = join ( projectDir , path ) ;
@@ -201,8 +205,8 @@ export function fromFileSystemPath(path: any) {
201205 // Step 3: Manually encode characters that are not encoded by `encodeURI`.
202206 // This includes characters such as "#" and "?", which have special meaning in URLs,
203207 // but are just normal characters in a filesystem path.
204- for ( let i = 0 ; i < urlEncodePatterns . length ; i += 2 ) {
205- path = path . replace ( urlEncodePatterns [ i ] , urlEncodePatterns [ i + 1 ] ) ;
208+ for ( const pattern of urlEncodePatterns ) {
209+ path = path . replace ( pattern [ 0 ] , pattern [ 1 ] ) ;
206210 }
207211
208212 return path ;
0 commit comments