|
| 1 | +import sqlite3 from 'better-sqlite3'; |
| 2 | +import makeDebug from 'debug'; |
| 3 | + |
| 4 | +import { |
| 5 | + buildFileIndex, |
| 6 | + FileIndex, |
| 7 | + fileTokens, |
| 8 | + FilterFn, |
| 9 | + isBinaryFile, |
| 10 | + isDataFile, |
| 11 | + isLargeFile, |
| 12 | + listProjectFiles, |
| 13 | + readFileSafe, |
| 14 | +} from '@appland/search'; |
| 15 | +import { fileNameMatchesFilterPatterns } from './index/filter-patterns'; |
| 16 | + |
| 17 | +const debug = makeDebug('appmap:rpc:explain:index-files'); |
| 18 | + |
| 19 | +function fileFilter( |
| 20 | + includePatterns: RegExp[] | undefined, |
| 21 | + excludePatterns: RegExp[] | undefined |
| 22 | +): FilterFn { |
| 23 | + return async (path: string) => { |
| 24 | + debug('Filtering: %s', path); |
| 25 | + if (isBinaryFile(path)) { |
| 26 | + debug('Skipping binary file: %s', path); |
| 27 | + return false; |
| 28 | + } |
| 29 | + |
| 30 | + const includeFile = fileNameMatchesFilterPatterns(path, includePatterns, excludePatterns); |
| 31 | + if (!includeFile) return false; |
| 32 | + |
| 33 | + const isData = isDataFile(path); |
| 34 | + if (isData && (await isLargeFile(path))) { |
| 35 | + debug('Skipping large data file: %s', path); |
| 36 | + return false; |
| 37 | + } |
| 38 | + |
| 39 | + return true; |
| 40 | + }; |
| 41 | +} |
| 42 | + |
| 43 | +export default async function indexFiles( |
| 44 | + db: sqlite3.Database, |
| 45 | + directories: string[], |
| 46 | + includePatterns: RegExp[] | undefined, |
| 47 | + excludePatterns: RegExp[] | undefined |
| 48 | +): Promise<FileIndex> { |
| 49 | + const fileIndex = new FileIndex(db); |
| 50 | + |
| 51 | + const filter = fileFilter(includePatterns, excludePatterns); |
| 52 | + await buildFileIndex(fileIndex, directories, listProjectFiles, filter, readFileSafe, fileTokens); |
| 53 | + |
| 54 | + return fileIndex; |
| 55 | +} |
0 commit comments