Refactor file relevance scoring and sorting logic in main.js
This commit is contained in:
32
main.js
32
main.js
@@ -1961,11 +1961,22 @@ ipcMain.on('show-tree-context-menu', (event, { absPath, relPath, root, type }) =
|
|||||||
const gitignorePath = path.join(folderPath, '.gitignore');
|
const gitignorePath = path.join(folderPath, '.gitignore');
|
||||||
if (!fs.existsSync(gitignorePath)) return null;
|
if (!fs.existsSync(gitignorePath)) return null;
|
||||||
const content = fs.readFileSync(gitignorePath, 'utf8');
|
const content = fs.readFileSync(gitignorePath, 'utf8');
|
||||||
const ig = ignore().add(content);
|
return ignore().add(content);
|
||||||
return ig;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
base = base || dir;
|
||||||
if (!ig) ig = getGitignoreFilter(base);
|
if (!ig) ig = getGitignoreFilter(base);
|
||||||
let files = [];
|
let files = [];
|
||||||
@@ -1978,28 +1989,31 @@ ipcMain.on('show-tree-context-menu', (event, { absPath, relPath, root, type }) =
|
|||||||
if (f.startsWith('.')) continue;
|
if (f.startsWith('.')) continue;
|
||||||
walk(full);
|
walk(full);
|
||||||
} else if (isTextFile(full)) {
|
} else if (isTextFile(full)) {
|
||||||
files.push(full);
|
files.push({ f: full, rel });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
walk(dir);
|
walk(dir);
|
||||||
// Sortiere nach Größe, nimm so viele wie ins Limit passen
|
// Score & sort
|
||||||
files = files.map(f => ({f, s: fs.statSync(f).size}))
|
files = files.map(({f, rel}) => ({
|
||||||
.sort((a,b)=>a.s-b.s);
|
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 = [];
|
let sum = 0, selected = [];
|
||||||
for (let {f,s} of files) {
|
for (let {f,s} of files) {
|
||||||
if (sum + s > maxSize) break;
|
if (sum + s > maxSize) break;
|
||||||
selected.push(f);
|
selected.push(f);
|
||||||
sum += s;
|
sum += s;
|
||||||
}
|
}
|
||||||
console.log("files to generate readme from: " + selected)
|
|
||||||
return selected;
|
return selected;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
ipcMain.handle('has-readme', async (_evt, folderPath) => {
|
ipcMain.handle('has-readme', async (_evt, folderPath) => {
|
||||||
const readmePath = path.join(folderPath, 'README.md');
|
const readmePath = path.join(folderPath, 'README.md');
|
||||||
return fs.existsSync(readmePath);
|
return fs.existsSync(readmePath);
|
||||||
|
|||||||
Reference in New Issue
Block a user