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

feat: use async resolve #29

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
12 changes: 12 additions & 0 deletions lib/common.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import assert from 'assert';
import fs from 'fs';
import { pathExists } from 'fs-extra';
import { realpath } from 'fs/promises';
import path from 'path';

export const STORE_BLOB = 0;
Expand Down Expand Up @@ -264,3 +266,13 @@ export function toNormalizedRealPath(requestPath: string) {

return file;
}

export async function toNormalizedRealPathAsync(requestPath: string) {
const file = normalizePath(requestPath);

if (await pathExists(requestPath)) {
return realpath(file);
}

return file;
}
37 changes: 23 additions & 14 deletions lib/follow.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { sync, SyncOpts } from 'resolve';
import async, { AsyncOpts } from 'resolve';
import fs from 'fs';
import path from 'path';
import { toNormalizedRealPath } from './common';
import { toNormalizedRealPathAsync } from './common';

import type { PackageJson } from './types';

Expand All @@ -25,20 +25,20 @@ function parentDirectoriesContain(parent: string, directory: string) {
}
}

interface FollowOptions extends Pick<SyncOpts, 'basedir' | 'extensions'> {
interface FollowOptions extends Pick<AsyncOpts, 'basedir' | 'extensions'> {
ignoreFile?: string;
catchReadFile?: (file: string) => void;
catchPackageFilter?: (config: PackageJson, base: string, dir: string) => void;
}

export function follow(x: string, opts: FollowOptions) {
// TODO async version
return new Promise<string>((resolve) => {
resolve(
sync(x, {
return new Promise<string>((resolve, reject) => {
async(
x,
{
basedir: opts.basedir,
extensions: opts.extensions,
isFile: (file) => {
isFile: (file: string) => {
if (
opts.ignoreFile &&
path.join(path.dirname(opts.ignoreFile), PROOF) === file
Expand Down Expand Up @@ -85,16 +85,16 @@ export function follow(x: string, opts: FollowOptions) {

return stat.isDirectory();
},
readFileSync: (file) => {
readFile: (file) => {
if (opts.ignoreFile && opts.ignoreFile === file) {
return Buffer.from(`{"main":"${PROOF}"}`);
return Promise.resolve(Buffer.from(`{"main":"${PROOF}"}`));
}

if (opts.catchReadFile) {
opts.catchReadFile(file);
}

return fs.readFileSync(file);
return fs.promises.readFile(file);
},
packageFilter: (config, base, dir) => {
if (opts.catchPackageFilter) {
Expand All @@ -106,11 +106,20 @@ export function follow(x: string, opts: FollowOptions) {

/** function to synchronously resolve a potential symlink to its real path */
// realpathSync?: (file: string) => string;
realpathSync: (file) => {
const file2 = toNormalizedRealPath(file);
realpath: async (file) => {
const file2 = await toNormalizedRealPathAsync(file);
return file2;
},
}),
},
(err, res) => {
if (err) {
reject(err);
} else if (res === undefined) {
reject(new Error(`Cannot find module '${x}'`));
} else {
resolve(res);
}
},
);
});
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"minimist": "^1.2.6",
"multistream": "^4.1.0",
"prebuild-install": "7.1.1",
"resolve": "^1.22.0",
"resolve": "2.0.0-next.5",
"stream-meter": "^1.0.4"
},
"devDependencies": {
Expand All @@ -46,7 +46,7 @@
"@types/minimist": "1.2.2",
"@types/multistream": "4.1.0",
"@types/node": "14.18.20",
"@types/resolve": "1.20.2",
"@types/resolve": "1.20.6",
"@types/stream-meter": "0.0.22",
"@typescript-eslint/eslint-plugin": "6.7.4",
"@typescript-eslint/parser": "6.7.4",
Expand Down
19 changes: 14 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -538,10 +538,10 @@
resolved "https://registry.npmjs.org/@types/parse-json/-/parse-json-4.0.0.tgz"
integrity sha512-//oorEZjL6sbPcKUaCdIGlIUeH26mgzimjBB77G6XRgnDl/L5wOnpyBGRe/Mmf5CVW3PwEBE1NjiMZ/ssFh4wA==

"@types/[email protected].2":
version "1.20.2"
resolved "https://registry.npmjs.org/@types/resolve/-/resolve-1.20.2.tgz"
integrity sha512-60BCwRFOZCQhDncwQdxxeOEEkbc5dIMccYLwbxsS4TUNeVECQ/pBJ0j09mrHOl/JJvpRPGwO9SvE4nR2Nb/a4Q==
"@types/[email protected].6":
version "1.20.6"
resolved "https://registry.yarnpkg.com/@types/resolve/-/resolve-1.20.6.tgz#e6e60dad29c2c8c206c026e6dd8d6d1bdda850b8"
integrity sha512-A4STmOXPhMUtHH+S6ymgE2GiBSMqf4oTvcQZMcHzokuTLVYzXTB8ttjcgxOVaAp2lGwEdzZ0J+cRbbeevQj1UQ==

"@types/semver@^7.5.0":
version "7.5.3"
Expand Down Expand Up @@ -3891,7 +3891,16 @@ resolve-from@^4.0.0:
resolved "https://registry.npmjs.org/resolve-from/-/resolve-from-4.0.0.tgz"
integrity sha512-pb/MYmXstAkysRFx8piNI1tGFNQIFA3vkE3Gq4EuA1dF6gHp/+vgZqsCGJapvy8N3Q+4o7FwvquPJcnZ7RYy4g==

resolve@^1.1.6, resolve@^1.22.0, resolve@^1.22.4:
[email protected]:
version "2.0.0-next.5"
resolved "https://registry.yarnpkg.com/resolve/-/resolve-2.0.0-next.5.tgz#6b0ec3107e671e52b68cd068ef327173b90dc03c"
integrity sha512-U7WjGVG9sH8tvjW5SmGbQuui75FiyjAX72HX15DwBBwF9dNiQZRQAg9nnPhYy+TUnE0+VcrttuvNI8oSxZcocA==
dependencies:
is-core-module "^2.13.0"
path-parse "^1.0.7"
supports-preserve-symlinks-flag "^1.0.0"

resolve@^1.1.6, resolve@^1.22.4:
version "1.22.6"
resolved "https://registry.npmjs.org/resolve/-/resolve-1.22.6.tgz"
integrity sha512-njhxM7mV12JfufShqGy3Rz8j11RPdLy4xi15UurGJeoHLfJpVXKdh3ueuOqbYUcDZnffr6X739JBo5LzyahEsw==
Expand Down
Loading