Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use node resolver by default #695

Merged
merged 1 commit into from
Nov 29, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions packages/cspell-lib/src/util/resolveFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import resolveGlobal from 'resolve-global';

export interface ResolveFileResult {
filename: string;
relativeTo: string | undefined;
found: boolean;
}

Expand All @@ -19,36 +20,45 @@ const testNodeModules = /^node_modules\//;
*/
export function resolveFile(filename: string, relativeTo: string): ResolveFileResult {
filename = filename.replace(/^~/, os.homedir());
const methodResolveFrom = (filename: string) => tryResolveFrom(filename, relativeTo);
const steps: { filename: string; fn: (f: string) => ResolveFileResult }[] = [
const steps: { filename: string; fn: (f: string, r: string) => ResolveFileResult }[] = [
{ filename: filename, fn: tryNodeResolve },
{ filename: path.resolve(relativeTo, filename), fn: tryResolveExists },
{ filename: path.resolve(filename), fn: tryResolveExists },
{ filename: filename, fn: methodResolveFrom },
{ filename: filename.replace(testNodeModules, ''), fn: methodResolveFrom },
{ filename: filename, fn: tryResolveFrom },
{ filename: filename.replace(testNodeModules, ''), fn: tryResolveFrom },
{ filename: filename, fn: tryResolveGlobal },
];

for (const step of steps) {
const r = step.fn(step.filename);
const r = step.fn(step.filename, relativeTo);
if (r.found) return r;
}
return { filename: path.resolve(relativeTo, filename), found: false };
return { filename: path.resolve(relativeTo, filename), relativeTo, found: false };
}

function tryNodeResolve(filename: string, relativeTo: string): ResolveFileResult {
try {
const r = require.resolve(filename, { paths: [path.resolve(relativeTo)] });
return { filename: r, relativeTo, found: true };
} catch (_) {
return { filename, relativeTo, found: false };
}
}

function tryResolveGlobal(filename: string): ResolveFileResult {
const r = resolveGlobal.silent(filename);
return { filename: r || filename, found: !!r };
return { filename: r || filename, relativeTo: undefined, found: !!r };
}

function tryResolveExists(filename: string): ResolveFileResult {
return { filename, found: fs.existsSync(filename) };
return { filename, relativeTo: undefined, found: fs.existsSync(filename) };
}

function tryResolveFrom(filename: string, relativeTo: string): ResolveFileResult {
try {
return { filename: resolveFrom(relativeTo, filename), found: true };
return { filename: resolveFrom(relativeTo, filename), relativeTo, found: true };
} catch (error) {
// Failed to resolve a relative module request
return { filename: filename, found: false };
return { filename: filename, relativeTo, found: false };
}
}