zed.mcp
Version:
MCP server for project analysis, AI rules reading, and dependency checking
95 lines (80 loc) • 2.6 kB
text/typescript
import type { PackageJson } from "../types/index";
/**
* NPM Registry API base URL
*/
const NPM_REGISTRY_URL = "https://registry.npmjs.org";
/**
* Fetches the latest version of a package from npm registry
*/
export async function getLatestVersion(packageName: string): Promise<string | null> {
try {
const response = await fetch(`${NPM_REGISTRY_URL}/${packageName}/latest`, {
headers: {
"Accept": "application/json",
},
});
if (!response.ok) {
console.warn(`Failed to fetch version for ${packageName}: ${response.status}`);
return null;
}
const data = await response.json() as { version?: string };
return data.version || null;
} catch (error) {
console.warn(`Error fetching version for ${packageName}:`, error);
return null;
}
}
/**
* Fetches latest versions for multiple packages in parallel
*/
export async function getLatestVersions(
packageNames: string[],
): Promise<Map<string, string | null>> {
const results = await Promise.all(
packageNames.map(async (name) => ({
name,
version: await getLatestVersion(name),
})),
);
return new Map(results.map((r) => [r.name, r.version]));
}
/**
* Cleans a version string by removing common prefixes and ranges
*/
export function cleanVersionString(version: string): string {
// Remove common prefixes: ^, ~, >=, <=, >, <, =
return version.replace(/^[\^~>=<]+/, "").trim();
}
/**
* Compares two version strings to determine if an update is available
*/
export function isVersionOutdated(current: string, latest: string): boolean {
const cleanCurrent = cleanVersionString(current);
const cleanLatest = cleanVersionString(latest);
if (cleanCurrent === cleanLatest) {
return false;
}
// Simple comparison - a more robust solution would use semver package
const currentParts = cleanCurrent.split(".").map(Number);
const latestParts = cleanLatest.split(".").map(Number);
for (let i = 0; i < Math.max(currentParts.length, latestParts.length); i++) {
const currentPart = currentParts[i] || 0;
const latestPart = latestParts[i] || 0;
if (latestPart > currentPart) {
return true;
} else if (latestPart < currentPart) {
return false;
}
}
return false;
}
/**
* Formats dependency information for display
*/
export function formatDependencyUpdate(
name: string,
currentVersion: string,
latestVersion: string,
): string {
return `${name}: ${currentVersion} → ${latestVersion}`;
}