Skip to content

Commit 510112e

Browse files
committed
fix(windows): fix windows paths not passed in as absolute by using cwd - #303
1 parent c9da8a4 commit 510112e

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

lib/util/url.ts

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import convertPathToPosix from "./convert-path-to-posix";
2-
import path from "path";
2+
import path, { win32 } from "path";
3+
34
const forwardSlashPattern = /\//g;
45
const protocolPattern = /^(\w{2,}):\/\//i;
56
const jsonPointerSlash = /~1/g;
@@ -9,7 +10,10 @@ import { join } from "path";
910
import { 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
1519
const urlDecodePatterns = [/%23/g, "#", /%24/g, "$", /%26/g, "&", /%2C/g, ",", /%40/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;

test/utils/path.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@ function filesystemPathHelpers() {
3838
/**
3939
* Returns the path with normalized, UNIX-like, slashes. Disk letter is lower-cased, if present.
4040
*/
41-
unixify(file: any) {
41+
unixify(file: string) {
4242
return convertPathToPosix(file).replace(/^[A-Z](?=:\/)/, (letter: any) => letter.toLowerCase());
4343
},
4444

4545
/**
4646
* Returns the path of a file in the "test" directory as a URL.
4747
* (e.g. "file://path/to/json-schema-ref-parser/test/files...")
4848
*/
49-
url(file: any) {
49+
url(file: string) {
5050
let pathname = path.abs(file);
5151

5252
if (isWindows()) {

0 commit comments

Comments
 (0)