UNPKG

dependency-context

Version:

MCP server for providing dependency documentation context to AI assistants

137 lines 4.86 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.parseDependencies = parseDependencies; const fs_extra_1 = __importDefault(require("fs-extra")); const path_1 = __importDefault(require("path")); /** * Parse dependencies from project files */ async function parseDependencies(projectPath) { const dependencies = []; // Check for custom dependency file const customDependencyPath = path_1.default.join(projectPath, "dependency-context.json"); if (await fs_extra_1.default.pathExists(customDependencyPath)) { const customDeps = await parseDependencyContextJson(customDependencyPath); dependencies.push(...customDeps); return dependencies; } // Check for package.json (Node.js projects) const packageJsonPath = path_1.default.join(projectPath, "package.json"); if (await fs_extra_1.default.pathExists(packageJsonPath)) { const npmDeps = await parsePackageJson(packageJsonPath); dependencies.push(...npmDeps); return dependencies; } // Check for requirements.txt (Python projects) const requirementsPath = path_1.default.join(projectPath, "requirements.txt"); if (await fs_extra_1.default.pathExists(requirementsPath)) { const pythonDeps = await parseRequirementsTxt(requirementsPath); dependencies.push(...pythonDeps); return dependencies; } return dependencies; } /** * Parse dependencies from dependency-context.json */ async function parseDependencyContextJson(filePath) { try { const content = await fs_extra_1.default.readJson(filePath); const dependencies = []; // Process regular dependencies if (content) { Object.entries(content).forEach(([name, version]) => { dependencies.push({ name, version: String(version).replace(/[^0-9.]/g, ""), type: "npm", }); }); } return dependencies; } catch (error) { console.error("Error parsing dependency-context.json:", error); return []; } } /** * Parse dependencies from package.json */ async function parsePackageJson(filePath) { try { const content = await fs_extra_1.default.readJson(filePath); const dependencies = []; // Process regular dependencies if (content.dependencies) { Object.entries(content.dependencies).forEach(([name, version]) => { dependencies.push({ name, version: String(version).replace(/[^0-9.]/g, ""), type: "npm", }); }); } // Process dev dependencies if (content.devDependencies) { Object.entries(content.devDependencies).forEach(([name, version]) => { dependencies.push({ name, version: String(version).replace(/[^0-9.]/g, ""), type: "npm", }); }); } return dependencies; } catch (error) { console.error("Error parsing package.json:", error); return []; } } /** * Parse dependencies from requirements.txt */ async function parseRequirementsTxt(filePath) { try { const content = await fs_extra_1.default.readFile(filePath, "utf-8"); const dependencies = []; // Split by lines and process each line const lines = content.split("\n"); for (const line of lines) { // Skip empty lines and comments const trimmedLine = line.trim(); if (!trimmedLine || trimmedLine.startsWith("#")) { continue; } // Basic parsing for package==version or package>=version formats const equalMatch = trimmedLine.match(/^([\w.-]+)[=<>~!]=+([\w.-]+)/); if (equalMatch) { dependencies.push({ name: equalMatch[1], version: equalMatch[2], type: "python", }); continue; } // Handle simple package names without version specifiers const packageMatch = trimmedLine.match(/^([\w.-]+)/); if (packageMatch) { dependencies.push({ name: packageMatch[1], version: "latest", type: "python", }); } } return dependencies; } catch (error) { console.error("Error parsing requirements.txt:", error); return []; } } //# sourceMappingURL=index.js.map