Skip to content

Commit

Permalink
refactor to helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Apr 23, 2020
1 parent 03533a7 commit 2e2b183
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions packages/jest-resolve/src/defaultResolver.ts
Expand Up @@ -22,22 +22,6 @@ type ResolverOptions = {
rootDir?: Config.Path;
};

function tolerantRealpath(path: Config.Path): Config.Path {
try {
const resolved = realpath(path);

if (resolved) {
return resolved;
}
} catch (error) {
if (error.code !== 'ENOENT') {
throw error;
}
}

return path;
}

export default function defaultResolver(
path: Config.Path,
options: ResolverOptions,
Expand All @@ -58,12 +42,12 @@ export default function defaultResolver(
paths: options.paths,
preserveSymlinks: false,
// @ts-ignore: https://github.com/DefinitelyTyped/DefinitelyTyped/pull/44137
realpathSync: tolerantRealpath,
realpathSync,
});

// Dereference symlinks to ensure we don't create a separate
// module instance depending on how it was referenced.
return tolerantRealpath(result);
return realpathSync(result);
}

export function clearDefaultResolverCache(): void {
Expand Down Expand Up @@ -105,6 +89,22 @@ function statSyncCached(path: string): IPathType {
return IPathType.OTHER;
}

function tolerantRealpath(path: Config.Path): Config.Path {
try {
const resolved = realpath(path);

if (resolved) {
return resolved;
}
} catch (error) {
if (error.code !== 'ENOENT') {
throw error;
}
}

return path;
}

/*
* helper functions
*/
Expand All @@ -115,3 +115,7 @@ function isFile(file: Config.Path): boolean {
function isDirectory(dir: Config.Path): boolean {
return statSyncCached(dir) === IPathType.DIRECTORY;
}

function realpathSync(file: Config.Path): Config.Path {
return tolerantRealpath(file);
}

0 comments on commit 2e2b183

Please sign in to comment.