import fs from 'fs/promises';
import path from 'path';
import { getSignatures } from './language.js';
/**
* Detect entry points for a project
* @param {string} projectPath - Path to the project directory
* @param {string[]} files - List of files in the project
* @param {Array<{name: string, key: string}>} detectedLanguages - Languages detected
* @returns {Promise<string[]>}
*/
export async function detectEntryPoints(projectPath, files, detectedLanguages) {
const sigs = await getSignatures();
const entryPoints = [];
const fileSet = new Set(files);
// Check package.json main field for Node.js
if (detectedLanguages.some(l => l.key === 'nodejs' || l.key === 'typescript')) {
try {
const pkgPath = path.join(projectPath, 'package.json');
const pkgData = await fs.readFile(pkgPath, 'utf-8');
const pkg = JSON.parse(pkgData);
if (pkg.main) {
entryPoints.push(pkg.main);
}
if (pkg.module) {
entryPoints.push(pkg.module);
}
if (pkg.bin) {
if (typeof pkg.bin === 'string') {
entryPoints.push(pkg.bin);
} else if (typeof pkg.bin === 'object') {
entryPoints.push(...Object.values(pkg.bin));
}
}
} catch {
// package.json not found or invalid
}
}
// Check signature-based entry points
for (const lang of detectedLanguages) {
const langEntryPoints = sigs.entryPoints[lang.key] || [];
for (const ep of langEntryPoints) {
// Handle glob-like patterns simply
if (ep.includes('*')) {
// For patterns like "src/main/java/**/*Application.java"
// Just check if any file matches the pattern suffix
const suffix = ep.split('*').pop();
const matchingFiles = files.filter(f => f.endsWith(suffix));
entryPoints.push(...matchingFiles);
} else if (fileSet.has(ep) || files.some(f => f === ep || f.endsWith('/' + ep) || f.endsWith('\\' + ep))) {
entryPoints.push(ep);
}
}
}
// Deduplicate
return [...new Set(entryPoints)];
}