|
| 1 | +import { readFile } from 'fs/promises'; |
| 2 | +import { exists, processNamedFiles, verbose } from '../utils'; |
| 3 | +import { Metadata } from '@appland/models'; |
| 4 | +import { dirname, join } from 'path'; |
| 5 | +import { warn } from 'console'; |
| 6 | +import lunr from 'lunr'; |
| 7 | +import assert from 'assert'; |
| 8 | + |
| 9 | +const isCamelized = (str: string): boolean => { |
| 10 | + if (str.length < 3) return false; |
| 11 | + |
| 12 | + const testStr = str.slice(1); |
| 13 | + return /[a-z][A-Z]/.test(testStr); |
| 14 | +}; |
| 15 | + |
| 16 | +export const splitCamelized = (str: string): string => { |
| 17 | + if (!isCamelized(str)) return str; |
| 18 | + |
| 19 | + const result = new Array<string>(); |
| 20 | + let last = 0; |
| 21 | + for (let i = 1; i < str.length; i++) { |
| 22 | + const pc = str[i - 1]; |
| 23 | + const c = str[i]; |
| 24 | + const isUpper = c >= 'A' && c <= 'Z'; |
| 25 | + if (isUpper) { |
| 26 | + result.push(str.slice(last, i)); |
| 27 | + last = i; |
| 28 | + } |
| 29 | + } |
| 30 | + result.push(str.slice(last)); |
| 31 | + return result.join(' '); |
| 32 | +}; |
| 33 | + |
| 34 | +type SerializedCodeObject = { |
| 35 | + name: string; |
| 36 | + type: string; |
| 37 | + labels: string[]; |
| 38 | + children: SerializedCodeObject[]; |
| 39 | + static?: boolean; |
| 40 | + sourceLocation?: string; |
| 41 | +}; |
| 42 | + |
| 43 | +export type SearchOptions = { |
| 44 | + maxResults?: number; |
| 45 | +}; |
| 46 | + |
| 47 | +export type SearchResult = { |
| 48 | + appmap: string; |
| 49 | + score: number; |
| 50 | +}; |
| 51 | + |
| 52 | +export default class FindAppMaps { |
| 53 | + idx: lunr.Index | undefined; |
| 54 | + |
| 55 | + constructor(public appmapDir: string) {} |
| 56 | + |
| 57 | + async initialize() { |
| 58 | + const { appmapDir } = this; |
| 59 | + |
| 60 | + const documents = new Array<any>(); |
| 61 | + await processNamedFiles(appmapDir, 'metadata.json', async (metadataFile) => { |
| 62 | + const metadata = JSON.parse(await readFile(metadataFile, 'utf-8')) as Metadata; |
| 63 | + const indexDir = dirname(metadataFile); |
| 64 | + const classMap = JSON.parse( |
| 65 | + await readFile(join(indexDir, 'classMap.json'), 'utf-8') |
| 66 | + ) as SerializedCodeObject[]; |
| 67 | + const queries = new Array<string>(); |
| 68 | + const codeObjects = new Array<string>(); |
| 69 | + const routes = new Array<string>(); |
| 70 | + const externalRoutes = new Array<string>(); |
| 71 | + |
| 72 | + const collectFunction = (co: SerializedCodeObject) => { |
| 73 | + if (co.type === 'query') queries.push(co.name); |
| 74 | + else if (co.type === 'route') routes.push(co.name); |
| 75 | + else if (co.type === 'external-route') externalRoutes.push(co.name); |
| 76 | + else codeObjects.push(splitCamelized(co.name)); |
| 77 | + |
| 78 | + co.children?.forEach((child) => { |
| 79 | + collectFunction(child); |
| 80 | + }); |
| 81 | + }; |
| 82 | + classMap.forEach((co) => collectFunction(co)); |
| 83 | + |
| 84 | + const parameters = new Array<string>(); |
| 85 | + if (await exists(join(indexDir, 'canonical.parameters.json'))) { |
| 86 | + const canonicalParameters = JSON.parse( |
| 87 | + await readFile(join(indexDir, 'canonical.parameters.json'), 'utf-8') |
| 88 | + ) as string[]; |
| 89 | + canonicalParameters.forEach((cp) => { |
| 90 | + parameters.push(splitCamelized(cp)); |
| 91 | + }); |
| 92 | + } |
| 93 | + |
| 94 | + documents.push({ |
| 95 | + id: indexDir, |
| 96 | + name: metadata.name, |
| 97 | + source_location: metadata.source_location, |
| 98 | + code_objects: codeObjects.join(' '), |
| 99 | + queries: queries.join(' '), |
| 100 | + routes: routes.join(' '), |
| 101 | + external_routes: externalRoutes.join(' '), |
| 102 | + parameters: parameters, |
| 103 | + }); |
| 104 | + }); |
| 105 | + |
| 106 | + if (verbose()) warn(`Indexing ${documents.length} diagrams`); |
| 107 | + |
| 108 | + this.idx = lunr(function () { |
| 109 | + this.ref('id'); |
| 110 | + this.field('name'); |
| 111 | + this.field('source_location'); |
| 112 | + this.field('code_objects'); |
| 113 | + this.field('queries'); |
| 114 | + this.field('routes'); |
| 115 | + this.field('external_routes'); |
| 116 | + this.field('parameters'); |
| 117 | + |
| 118 | + this.tokenizer.separator = /[\s/-_:#.]+/; |
| 119 | + |
| 120 | + for (const doc of documents) this.add(doc); |
| 121 | + }); |
| 122 | + } |
| 123 | + |
| 124 | + search(search: string, options: SearchOptions = {}): SearchResult[] { |
| 125 | + assert(this.idx); |
| 126 | + let matches = this.idx.search(search); |
| 127 | + if (verbose()) warn(`Got ${matches.length} matches for search ${search}`); |
| 128 | + if (options.maxResults && matches.length > options.maxResults) { |
| 129 | + if (verbose()) warn(`Limiting to the top ${options.maxResults} matches`); |
| 130 | + matches = matches.slice(0, options.maxResults); |
| 131 | + } |
| 132 | + return matches.map((match) => ({ appmap: match.ref, score: match.score })); |
| 133 | + } |
| 134 | +} |
0 commit comments