From 510112ef04a9efdfa5afcba741fff9ec9c9ee043 Mon Sep 17 00:00:00 2001 From: JonLuca DeCaro Date: Tue, 5 Mar 2024 18:18:16 -0800 Subject: [PATCH] fix(windows): fix windows paths not passed in as absolute by using cwd - #303 --- lib/util/url.ts | 18 +++++++++++------- test/utils/path.ts | 4 ++-- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/lib/util/url.ts b/lib/util/url.ts index f692198d..1e392fef 100644 --- a/lib/util/url.ts +++ b/lib/util/url.ts @@ -1,5 +1,6 @@ import convertPathToPosix from "./convert-path-to-posix"; -import path from "path"; +import path, { win32 } from "path"; + const forwardSlashPattern = /\//g; const protocolPattern = /^(\w{2,}):\/\//i; const jsonPointerSlash = /~1/g; @@ -9,7 +10,10 @@ import { join } from "path"; import { isWindows } from "./is-windows"; // RegExp patterns to URL-encode special characters in local filesystem paths -const urlEncodePatterns = [/\?/g, "%3F", /#/g, "%23"]; +const urlEncodePatterns = [ + [/\?/g, "%3F"], + [/#/g, "%23"], +] as [RegExp, string][]; // RegExp patterns to URL-decode special characters for local filesystem paths const urlDecodePatterns = [/%23/g, "#", /%24/g, "$", /%26/g, "&", /%2C/g, ",", /%40/g, "@"]; @@ -177,17 +181,17 @@ export function isFileSystemPath(path: string | undefined) { * @param path * @returns */ -export function fromFileSystemPath(path: any) { +export function fromFileSystemPath(path: string) { // Step 1: On Windows, replace backslashes with forward slashes, // rather than encoding them as "%5C" if (isWindows()) { - const projectDir = join(__dirname, "..", ".."); + const projectDir = cwd(); const upperPath = path.toUpperCase(); const projectDirPosixPath = convertPathToPosix(projectDir); const posixUpper = projectDirPosixPath.toUpperCase(); const hasProjectDir = upperPath.includes(posixUpper); const hasProjectUri = upperPath.includes(posixUpper); - const isAbsolutePath = path?.win32?.isAbsolute(path); + const isAbsolutePath = win32?.isAbsolute(path); if (!(hasProjectDir || hasProjectUri || isAbsolutePath)) { path = join(projectDir, path); @@ -201,8 +205,8 @@ export function fromFileSystemPath(path: any) { // Step 3: Manually encode characters that are not encoded by `encodeURI`. // This includes characters such as "#" and "?", which have special meaning in URLs, // but are just normal characters in a filesystem path. - for (let i = 0; i < urlEncodePatterns.length; i += 2) { - path = path.replace(urlEncodePatterns[i], urlEncodePatterns[i + 1]); + for (const pattern of urlEncodePatterns) { + path = path.replace(pattern[0], pattern[1]); } return path; diff --git a/test/utils/path.ts b/test/utils/path.ts index a9b71b6f..2d678d82 100644 --- a/test/utils/path.ts +++ b/test/utils/path.ts @@ -38,7 +38,7 @@ function filesystemPathHelpers() { /** * Returns the path with normalized, UNIX-like, slashes. Disk letter is lower-cased, if present. */ - unixify(file: any) { + unixify(file: string) { return convertPathToPosix(file).replace(/^[A-Z](?=:\/)/, (letter: any) => letter.toLowerCase()); }, @@ -46,7 +46,7 @@ function filesystemPathHelpers() { * Returns the path of a file in the "test" directory as a URL. * (e.g. "file://path/to/json-schema-ref-parser/test/files...") */ - url(file: any) { + url(file: string) { let pathname = path.abs(file); if (isWindows()) {