Skip to content

Commit 10fb77e

Browse files
authored
Allow specifying filenames for codebase scan (#184)
1 parent 149c414 commit 10fb77e

File tree

1 file changed

+31
-14
lines changed

1 file changed

+31
-14
lines changed

scripts/scan-codebase.ts

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,42 +22,59 @@ 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+
} else {
36+
for (const filename of iterSourceFiles(path.join(root, dir))) {
37+
// We want the path relative to the root
38+
yield path.join(dir, filename);
39+
}
3640
}
3741
}
3842
}
3943
}
4044

4145
async function* iterFunctionInfo(
4246
root: string,
43-
filenames: IterableIterator<string>,
47+
filenames: AsyncGenerator<string>,
4448
): AsyncIterableIterator<{
4549
node_count: number;
4650
start_position: { row: number; column: number };
4751
funcdef: string;
4852
filename: string;
4953
}> {
50-
for (const filename of filenames) {
54+
for await (const filename of filenames) {
5155
const code = await Bun.file(path.join(root, filename)).text();
52-
const language = getLanguage(filename);
56+
const language = (() => {
57+
try {
58+
return getLanguage(filename);
59+
} catch {
60+
return undefined;
61+
}
62+
})();
63+
if (!language) {
64+
continue;
65+
}
5366
for (const func of iterFunctions(code, language)) {
5467
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-
};
68+
try {
69+
yield {
70+
node_count: cfg.graph.order,
71+
start_position: func.startPosition,
72+
funcdef: getFuncDef(code, func),
73+
filename: filename.replaceAll("\\", "/"),
74+
};
75+
} catch (e) {
76+
console.error(`Failed getting function definition for ${func.text}`, e);
77+
}
6178
}
6279
}
6380
}

0 commit comments

Comments
 (0)