1
0

Refactor file relevance scoring and sorting logic in main.js

This commit is contained in:
2025-06-01 07:08:18 +02:00
parent 8d8cf0ff11
commit cbd48f6710

34
main.js
View File

@@ -1961,11 +1961,22 @@ ipcMain.on('show-tree-context-menu', (event, { absPath, relPath, root, type }) =
const gitignorePath = path.join(folderPath, '.gitignore');
if (!fs.existsSync(gitignorePath)) return null;
const content = fs.readFileSync(gitignorePath, 'utf8');
const ig = ignore().add(content);
return ig;
return ignore().add(content);
}
function getRelevantFiles(dir, maxSize = MAX_TOTAL_SIZE, ig = null, base = null) {
function getFileRelevanceScore(filename, relPath) {
const base = path.basename(filename).toLowerCase();
let score = 0;
if (/main|index|app|server/.test(base)) score += 10;
if (/test|mock|example|spec/.test(base)) score -= 20;
if (/package\.json|requirements\.txt|pyproject\.toml|makefile|cargo\.toml/.test(base)) score += 10;
if (path.dirname(relPath) === '.') score += 5;
if (/\.(js|py|ts|rb|pl|php|java|c|cpp|h|cs|go|rs|json|yml|yaml|toml|md|html|css|txt)$/.test(base)) score += 5;
if (/\.min\./.test(base)) score -= 10;
return score;
}
function getRelevantFiles(dir, maxSize = 100*1024, ig = null, base = null) {
base = base || dir;
if (!ig) ig = getGitignoreFilter(base);
let files = [];
@@ -1978,28 +1989,31 @@ ipcMain.on('show-tree-context-menu', (event, { absPath, relPath, root, type }) =
if (f.startsWith('.')) continue;
walk(full);
} else if (isTextFile(full)) {
files.push(full);
files.push({ f: full, rel });
}
}
}
walk(dir);
// Sortiere nach Größe, nimm so viele wie ins Limit passen
files = files.map(f => ({f, s: fs.statSync(f).size}))
.sort((a,b)=>a.s-b.s);
// Score & sort
files = files.map(({f, rel}) => ({
f, rel, score: getFileRelevanceScore(f, rel),
s: fs.statSync(f).size
}))
.sort((a, b) => b.score - a.score || a.s - b.s);
// Limit by max size
let sum = 0, selected = [];
for (let {f,s} of files) {
if (sum + s > maxSize) break;
selected.push(f);
sum += s;
}
console.log("files to generate readme from: " + selected)
return selected;
}
ipcMain.handle('has-readme', async (_evt, folderPath) => {
const readmePath = path.join(folderPath, 'README.md');
return fs.existsSync(readmePath);