Skip to content

Commit

Permalink
fix(utils): handle absolute windows paths correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
ardatan committed Oct 29, 2022
1 parent a3d5020 commit d5f2d95
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 16 deletions.
6 changes: 6 additions & 0 deletions .changeset/slow-hornets-think.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'json-machete': patch
'@graphql-mesh/utils': patch
---

Fix Windows path issues
14 changes: 5 additions & 9 deletions packages/json-machete/src/dereferenceObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import JsonPointer from 'json-pointer';
import { path as pathModule, process } from '@graphql-mesh/cross-helpers';
import urlJoin from 'url-join';
import { fetch as crossUndiciFetch } from '@whatwg-node/fetch';
import { defaultImportFn, DefaultLogger, readFileOrUrl } from '@graphql-mesh/utils';
import { defaultImportFn, DefaultLogger, isUrl, readFileOrUrl } from '@graphql-mesh/utils';
import { handleUntitledDefinitions } from './healUntitledDefinitions';

export const resolvePath = (path: string, root: any): any => {
Expand All @@ -19,14 +19,10 @@ function isRefObject(obj: any): obj is { $ref: string } {
return typeof obj === 'object' && typeof obj.$ref === 'string';
}

function isURL(str: string) {
return /^https?:\/\//.test(str);
}

const getAbsolute$Ref = (given$ref: string, baseFilePath: string) => {
const [givenExternalFileRelativePath, givenRefPath] = given$ref.split('#');
if (givenExternalFileRelativePath) {
const cwd = isURL(baseFilePath) ? getCwdForUrl(baseFilePath) : pathModule.dirname(baseFilePath);
const cwd = isUrl(baseFilePath) ? getCwdForUrl(baseFilePath) : pathModule.dirname(baseFilePath);
const givenExternalFilePath = getAbsolutePath(givenExternalFileRelativePath, cwd);
if (givenRefPath) {
return `${givenExternalFilePath}#${givenRefPath}`;
Expand All @@ -47,10 +43,10 @@ function normalizeUrl(url: string) {
}

export function getAbsolutePath(path: string, cwd: string) {
if (isURL(path)) {
if (isUrl(path)) {
return path;
}
if (isURL(cwd)) {
if (isUrl(cwd)) {
return normalizeUrl(urlJoin(cwd, path));
}
if (pathModule.isAbsolute(path)) {
Expand All @@ -60,7 +56,7 @@ export function getAbsolutePath(path: string, cwd: string) {
}

export function getCwd(path: string) {
return isURL(path) ? getCwdForUrl(path) : pathModule.dirname(path);
return isUrl(path) ? getCwdForUrl(path) : pathModule.dirname(path);
}

// eslint-disable-next-line @typescript-eslint/ban-types
Expand Down
8 changes: 1 addition & 7 deletions packages/utils/src/read-file-or-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,7 @@ export interface ReadFileOrUrlOptions extends RequestInit {
}

export function isUrl(str: string): boolean {
try {
// eslint-disable-next-line no-new
new URL(str);
return true;
} catch {
return false;
}
return /^https?:\/\//.test(str);
}

export async function readFileOrUrl<T>(filePathOrUrl: string, config: ReadFileOrUrlOptions): Promise<T> {
Expand Down

0 comments on commit d5f2d95

Please sign in to comment.