vibe-coder-mcp
Version:
Production-ready MCP server with complete agent integration, multi-transport support, and comprehensive development automation tools for AI-assisted workflows.
68 lines (67 loc) • 2.62 kB
JavaScript
import * as fs from 'fs';
import * as path from 'path';
import { extractJSImports } from './importExtractor.js';
import { resolveImport } from './importResolver.no-cache.js';
import { parseSourceCode } from '../parser.js';
import logger from '../../../logger.js';
export async function resolveImportsInFile(filePath, language, options = {}) {
try {
const fileContent = await fs.promises.readFile(filePath, 'utf-8');
const tree = await parseSourceCode(fileContent, `.${language}`);
const imports = [];
const rootNode = tree.ast;
if (language === 'javascript' || language === 'typescript') {
const extractedImports = extractJSImports(rootNode, fileContent);
imports.push(...extractedImports);
}
else {
const cursor = rootNode.walk();
let reachedEnd = false;
while (!reachedEnd) {
if (cursor.nodeType.includes('import')) {
const node = cursor.currentNode();
imports.push({
path: node.text,
type: 'unknown',
isExternalPackage: false,
startLine: node.startPosition.row + 1,
endLine: node.endPosition.row + 1
});
}
reachedEnd = !cursor.gotoNextSibling();
if (reachedEnd && cursor.gotoParent()) {
reachedEnd = !cursor.gotoNextSibling();
}
}
}
const projectRoot = options.projectRoot || path.dirname(filePath);
const expandSecurityBoundary = options.expandSecurityBoundary !== undefined
? options.expandSecurityBoundary
: true;
for (const importInfo of imports) {
try {
const resolvedPath = resolveImport(importInfo.path, {
projectRoot,
fromFile: filePath,
language,
expandSecurityBoundary
});
if (resolvedPath) {
importInfo.resolvedPath = resolvedPath;
}
}
catch (error) {
logger.debug({
err: error,
importPath: importInfo.path,
fromFile: filePath
}, 'Error resolving import');
}
}
return imports;
}
catch (error) {
logger.error({ err: error, filePath }, 'Error resolving imports in file');
return [];
}
}