1
1
import convertPathToPosix from "./convert-path-to-posix" ;
2
- import path from "path" ;
2
+ import path , { win32 } from "path" ;
3
+
3
4
const forwardSlashPattern = / \/ / g;
4
5
const protocolPattern = / ^ ( \w { 2 , } ) : \/ \/ / i;
5
6
const jsonPointerSlash = / ~ 1 / g;
@@ -9,7 +10,10 @@ import { join } from "path";
9
10
import { isWindows } from "./is-windows" ;
10
11
11
12
// 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 ] [ ] ;
13
17
14
18
// RegExp patterns to URL-decode special characters for local filesystem paths
15
19
const 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) {
177
181
* @param path
178
182
* @returns
179
183
*/
180
- export function fromFileSystemPath ( path : any ) {
184
+ export function fromFileSystemPath ( path : string ) {
181
185
// Step 1: On Windows, replace backslashes with forward slashes,
182
186
// rather than encoding them as "%5C"
183
187
if ( isWindows ( ) ) {
184
- const projectDir = join ( __dirname , ".." , ".." ) ;
188
+ const projectDir = cwd ( ) ;
185
189
const upperPath = path . toUpperCase ( ) ;
186
190
const projectDirPosixPath = convertPathToPosix ( projectDir ) ;
187
191
const posixUpper = projectDirPosixPath . toUpperCase ( ) ;
188
192
const hasProjectDir = upperPath . includes ( posixUpper ) ;
189
193
const hasProjectUri = upperPath . includes ( posixUpper ) ;
190
- const isAbsolutePath = path ?. win32 ?. isAbsolute ( path ) ;
194
+ const isAbsolutePath = win32 ?. isAbsolute ( path ) ;
191
195
192
196
if ( ! ( hasProjectDir || hasProjectUri || isAbsolutePath ) ) {
193
197
path = join ( projectDir , path ) ;
@@ -201,8 +205,8 @@ export function fromFileSystemPath(path: any) {
201
205
// Step 3: Manually encode characters that are not encoded by `encodeURI`.
202
206
// This includes characters such as "#" and "?", which have special meaning in URLs,
203
207
// 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 ] ) ;
206
210
}
207
211
208
212
return path ;
0 commit comments