Skip to content

Commit

Permalink
fix: 修复node_modules下package对应的package.json点击packageName无法跳转到对应package的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
zyrong committed Dec 14, 2021
1 parent fd53e24 commit 79f6077
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 25 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
},
"icon": "logo.png",
"galleryBanner": {
"color": "#2f54eb"
"color": "#2f54eb",
"theme": "light"
},
"categories": [
"Other"
Expand Down
39 changes: 15 additions & 24 deletions src/pkgjson-dep-jump-nm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { basename, dirname, join } from "path";
import { existsSync } from "fs";
import {
getFileInProjectRootDir,
isMonorepoProject,
genFileLocation,
error,
} from "./utils/index";
Expand All @@ -25,7 +24,7 @@ async function provideDefinition(
position: Position,
token: CancellationToken
): Promise<Definition | LocationLink[] | null | undefined> {
const filepath = document.uri.path;
const filepath = document.uri.fsPath;
const fileName = basename(filepath);
if (/package\.json$/.test(fileName)) {
const range = document.getWordRangeAtPosition(position);
Expand All @@ -48,30 +47,22 @@ async function provideDefinition(
const pkgName = word.replace(/"/g, "");
const isOrganizePkg = pkgName.startsWith("@");
const pkgNamePath = isOrganizePkg ? pkgName.split("/") : [pkgName];
const isMonorepo = isMonorepoProject(rootDir);
if (!isMonorepo) {
const nodeModulesPath = join(rootDir, NODE_MODULES);
destPath = join(nodeModulesPath, ...pkgNamePath, PACKAGE_JSON);
// 从package.json所在目录的node_modules寻找,直到根目录的node_modules停止。
let isRootDir = false;
let currentDirPath = filepath;
do {
currentDirPath = dirname(currentDirPath);
destPath = join(
currentDirPath,
NODE_MODULES,
...pkgNamePath,
PACKAGE_JSON
);
if (existsSync(destPath)) {
return genFileLocation(destPath);
return genFileLocation(destPath); // return location,字符串就会变成一个可以点击的链接
}
} else {
let isRootFile = false;
let currentDirPath = filepath;
do {
currentDirPath = dirname(currentDirPath);
destPath = join(
currentDirPath,
NODE_MODULES,
...pkgNamePath,
PACKAGE_JSON
);
if (existsSync(destPath)) {
return genFileLocation(destPath); // return location,字符串就会变成一个可以点击的链接
}
isRootFile = rootDir === currentDirPath;
} while (!isRootFile);
}
isRootDir = rootDir === currentDirPath;
} while (!isRootDir);

window.showWarningMessage(t("tip.notFoundPackage"));
}
Expand Down

0 comments on commit 79f6077

Please sign in to comment.