Skip to content

Commit d33a68d

Browse files
committed
Allow specifying filenames for codebase scan
1 parent 149c414 commit d33a68d

File tree

1 file changed

+27
-14
lines changed

1 file changed

+27
-14
lines changed

scripts/scan-codebase.ts

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,42 +22,55 @@ export function iterSourceFiles(root: string): IterableIterator<string> {
2222
);
2323
return sourceGlob.scanSync(root);
2424
}
25-
function* iterFilenames(
25+
async function* iterFilenames(
2626
root: string,
2727
dirsToInclude: string[],
28-
): IterableIterator<string> {
28+
): AsyncGenerator<string> {
2929
if (dirsToInclude.length === 1 && dirsToInclude[0] === "*") {
3030
yield* iterSourceFiles(root);
3131
} else {
3232
for (const dir of dirsToInclude) {
33-
for (const filename of iterSourceFiles(path.join(root, dir))) {
34-
// We want the path relative to the root
35-
yield path.join(dir, filename);
33+
if (await Bun.file(path.join(root, dir)).exists()) {
34+
yield dir;
35+
}
36+
else {
37+
for (const filename of iterSourceFiles(path.join(root, dir))) {
38+
// We want the path relative to the root
39+
yield path.join(dir, filename);
40+
}
3641
}
3742
}
3843
}
3944
}
4045

4146
async function* iterFunctionInfo(
4247
root: string,
43-
filenames: IterableIterator<string>,
48+
filenames: AsyncGenerator<string>,
4449
): AsyncIterableIterator<{
4550
node_count: number;
4651
start_position: { row: number; column: number };
4752
funcdef: string;
4853
filename: string;
4954
}> {
50-
for (const filename of filenames) {
55+
for await (const filename of filenames) {
5156
const code = await Bun.file(path.join(root, filename)).text();
52-
const language = getLanguage(filename);
57+
const language =( ()=>{
58+
try {return getLanguage(filename);}
59+
catch(_e) {return undefined;}
60+
})()
61+
if (!language) {continue;}
5362
for (const func of iterFunctions(code, language)) {
5463
const cfg = buildCFG(func, language);
55-
yield {
56-
node_count: cfg.graph.order,
57-
start_position: func.startPosition,
58-
funcdef: getFuncDef(code, func),
59-
filename: filename.replaceAll("\\", "/"),
60-
};
64+
try {
65+
yield {
66+
node_count: cfg.graph.order,
67+
start_position: func.startPosition,
68+
funcdef: getFuncDef(code, func),
69+
filename: filename.replaceAll("\\", "/"),
70+
};
71+
} catch (e) {
72+
console.error(`Failed getting function definition for ${func.text}`, e);
73+
}
6174
}
6275
}
6376
}

0 commit comments

Comments
 (0)