Skip to content

Commit

Permalink
feat: add latest version information of a package
Browse files Browse the repository at this point in the history
Fixes #12
  • Loading branch information
mainawycliffe committed Jan 13, 2024
1 parent 9bbc784 commit 4fa1170
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/utils/composeHoverMarkdownContent.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import * as vscode from "vscode";
import fetchPackageInfoFromNPM from "./fetchPackageInfoFromNPM";
import fetchPackageInfoFromNPM, {
fetchLatestPackageVersionFromNPM,
} from "./fetchPackageInfoFromNPM";

function determineRepositoryNameFromGitURL(gitURL: string): string {
if (gitURL.toLowerCase().includes("gitlab")) {
Expand All @@ -25,6 +27,10 @@ export default async function composeHoverMarkdownContent(
if (!packageDetails) {
return null;
}
// fetch info about the latest version of the package from npm
const latestPackageVersion =
await fetchLatestPackageVersionFromNPM(packageName);
console.log(latestPackageVersion);
const gitRepositoryURL = packageDetails.repository?.url
.replace("git+", "")
.replace(".git", "");
Expand All @@ -42,7 +48,7 @@ export default async function composeHoverMarkdownContent(
${packageDescription ? packageDescription : ""}
**Version**: ${packageDetails.version}
**Version**: ${packageDetails.version} (${latestPackageVersion && latestPackageVersion !== packageDetails.version ? `_**⭐️ Latest version**: ${latestPackageVersion}_` : ""})
**License**: ${packageDetails.license}
Expand Down
13 changes: 13 additions & 0 deletions src/utils/fetchPackageInfoFromNPM.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,16 @@ export default async function fetchPackageInfoFromNPM(
}
throw new Error(`Failed to fetch package info for ${packageName}`);
}

export async function fetchLatestPackageVersionFromNPM(
packageName: string,
): Promise<string | undefined> {
const res = await got.get(`https://registry.npmjs.com/${packageName}/latest`);
if (res.statusCode === 200) {
return registryPackageAPIResponseSchema.parse(JSON.parse(res.body)).version;
}
if (res.statusCode === 404) {
return undefined;
}
throw new Error(`Failed to fetch package info for ${packageName}`);
}

0 comments on commit 4fa1170

Please sign in to comment.