Skip to content

Commit

Permalink
fix: resolve aliased packages and packages that don't resolve their m…
Browse files Browse the repository at this point in the history
…ain files
  • Loading branch information
rigor789 committed Sep 11, 2023
1 parent 2ab2684 commit c4b85cf
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 6 deletions.
30 changes: 28 additions & 2 deletions index.js
Expand Up @@ -16,22 +16,47 @@ function resolvePackagePath(packageName, options) {
.replace(trailingPathSepRegExp, "");
}

function tryFindPackagePath(packageName, options) {
try {
// this can throw if the package doesn't have an exported main field
return require.resolve(packageName, options);
} catch (ignore) {}

const paths = options?.paths ?? [process.cwd()];
for (const dir of paths) {
try {
const pathToCheck = path.resolve(dir, "node_modules", packageName);
if (fs.existsSync(pathToCheck)) {
return pathToCheck;
}
} catch (ignore) {
// ignore
}
}
}

function resolvePackageJSONPath(packageName, options) {
try {
return require.resolve(`${packageName}/package.json`, options);
} catch (ignore) {}

try {
// try find last index of node_modules/<packageName>
const packageMainPath = require.resolve(packageName, options);
const packageMainPath = tryFindPackagePath(packageName, options);

if (!packageMainPath) {
// we couldn't find the main path, so we likely won't find the package.json either.
return undefined;
}

const searchWord = `node_modules${path.sep}${packageName.replace(
"/",
path.sep
)}`;
const foundIndex = packageMainPath.lastIndexOf(searchWord);

if (foundIndex > -1) {
const packagePath = packageMainPath.substr(
const packagePath = packageMainPath.slice(
0,
foundIndex + searchWord.length
);
Expand Down Expand Up @@ -75,6 +100,7 @@ function resolvePackageJSONPath(packageName, options) {
// this is a last-resort package.json path that we found near the resolved package,
// but did not match the packageName or was in an invalid json file
if (possiblePackageJSONPath) {
console.log("using possible package.json path", possiblePackageJSONPath)
return possiblePackageJSONPath;
}
} catch (ignore) {}
Expand Down
4 changes: 3 additions & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "@rigor789/resolve-package-path",
"version": "1.0.5",
"version": "1.0.6",
"main": "index.js",
"types": "index.d.ts",
"files": [
Expand All @@ -14,8 +14,10 @@
},
"devDependencies": {
"package-a": "./stubs/package-a",
"package-alias": "npm:@rigor789/resolve-package-path@latest",
"package-b": "./stubs/package-b",
"package-c": "./stubs/package-c",
"package-no-main-exports": "./stubs/package-no-main-exports",
"@scope/package-a": "./stubs/@scope/package-a",
"@scope/package-b": "./stubs/@scope/package-b",
"@scope/package-c": "./stubs/@scope/package-c"
Expand Down
6 changes: 6 additions & 0 deletions stubs/package-no-main-exports/package.json
@@ -0,0 +1,6 @@
{
"name": "package-no-main-exports",
"version": "1.0.0",
"type": "module",
"exports": {}
}
19 changes: 16 additions & 3 deletions test.js
Expand Up @@ -2,7 +2,10 @@ const { resolvePackagePath } = require("./");

const path = require("path");

const expectedPath = (packageName) => {
const expectedPath = (packageName, dir) => {
if (dir) {
return path.resolve(__dirname, dir, packageName);
}
switch (process.env.PACKAGE_MANAGER) {
case "yarn":
return path.resolve(__dirname, "node_modules", packageName);
Expand All @@ -18,6 +21,10 @@ const tests = [
packageName: "package-a",
expectedPath: expectedPath("package-a"),
},
{
packageName: "package-alias",
expectedPath: expectedPath("package-alias", "node_modules"),
},
{
packageName: "package-b",
expectedPath: expectedPath("package-b"),
Expand All @@ -26,6 +33,10 @@ const tests = [
packageName: "package-c",
expectedPath: expectedPath("package-c"),
},
{
packageName: "package-no-main-exports",
expectedPath: expectedPath("package-no-main-exports", "node_modules"),
},
{
packageName: "@scope/package-a",
expectedPath: expectedPath("@scope/package-a"),
Expand Down Expand Up @@ -54,11 +65,13 @@ tests.map((test) => {
console.log("OK");
} else {
console.error("FAIL");
console.error(`\nExpected \n\t"${packagePath}" \nto equal \n\t"${test.expectedPath}"`);
console.error(
`\nExpected \n\t"${packagePath}" \nto equal \n\t"${test.expectedPath}"`
);

// set process to a failed exit code
process.exitCode = 1;
}

console.log('\n')
console.log("\n");
});

0 comments on commit c4b85cf

Please sign in to comment.