|
1 | 1 | import yargs from 'yargs'; |
2 | | - |
| 2 | +import sqlite3 from 'better-sqlite3'; |
3 | 3 | import assert from 'assert'; |
4 | 4 | import { readFileSync } from 'fs'; |
5 | 5 | import { writeFile } from 'fs/promises'; |
6 | 6 | import { AppMap, AppMapFilter, buildAppMap, deserializeFilter } from '@appland/models'; |
| 7 | +import { FileIndex } from '@appland/search'; |
7 | 8 |
|
8 | 9 | import { handleWorkingDirectory } from '../../lib/handleWorkingDirectory'; |
9 | 10 | import { verbose } from '../../utils'; |
10 | 11 | import searchSingleAppMap, { SearchOptions as SingleSearchOptions } from './searchSingleAppMap'; |
11 | | -import AppMapIndex, { |
12 | | - SearchResponse as DiagramsSearchResponse, |
13 | | - SearchOptions, |
14 | | -} from '../../fulltext/AppMapIndex'; |
| 12 | +import { SearchResponse as DiagramsSearchResponse } from '../../fulltext/appmap-match'; |
15 | 13 | import { |
16 | 14 | SearchResult as EventSearchResult, |
17 | 15 | SearchResponse as EventSearchResponse, |
18 | 16 | } from '../../fulltext/FindEvents'; |
19 | 17 | import { openInBrowser } from '../open/openers'; |
| 18 | +import { buildAppMapIndex, search } from '../../fulltext/appmap-index'; |
| 19 | +import buildIndex from '../../rpc/explain/buildIndex'; |
20 | 20 |
|
21 | 21 | export const command = 'search <query>'; |
22 | 22 | export const describe = |
@@ -83,7 +83,21 @@ export const builder = (args: yargs.Argv) => { |
83 | 83 | return args.strict(); |
84 | 84 | }; |
85 | 85 |
|
86 | | -export const handler = async (argv: any) => { |
| 86 | +type ArgumentTypes = { |
| 87 | + directory: string; |
| 88 | + query: string; |
| 89 | + appmap: string; |
| 90 | + contextDepth: number; |
| 91 | + maxSize: string; |
| 92 | + filter: string; |
| 93 | + show: boolean; |
| 94 | + maxResults: number; |
| 95 | + findEvents: boolean; |
| 96 | + format: 'json' | 'appmap'; |
| 97 | + verbose: boolean; |
| 98 | +}; |
| 99 | + |
| 100 | +export const handler = async (argv: ArgumentTypes) => { |
87 | 101 | verbose(argv.verbose); |
88 | 102 |
|
89 | 103 | const { directory, query, appmap, contextDepth, show, maxResults, findEvents, format } = argv; |
@@ -160,19 +174,29 @@ export const handler = async (argv: any) => { |
160 | 174 | maxResults, |
161 | 175 | }; |
162 | 176 | const { maxSize, filter: filterStr } = argv; |
163 | | - if (maxSize) options.maxSize = maxSize; |
| 177 | + if (maxSize) options.maxSize = parseInt(maxSize); |
164 | 178 | if (filterStr) options.filter = deserializeFilter(filterStr); |
165 | 179 | const response = await searchSingleAppMap(appmap, query, options); |
166 | 180 | await presentResults(response); |
167 | 181 | } else { |
168 | | - const options: SearchOptions = { |
| 182 | + const options = { |
169 | 183 | maxResults, |
170 | 184 | }; |
171 | | - const response = await AppMapIndex.search([process.cwd()], query, options); |
| 185 | + |
| 186 | + const index = await buildIndex('appmaps', async (indexFile) => { |
| 187 | + const db = new sqlite3(indexFile); |
| 188 | + const fileIndex = new FileIndex(db); |
| 189 | + await buildAppMapIndex(fileIndex, [process.cwd()]); |
| 190 | + return fileIndex; |
| 191 | + }); |
| 192 | + |
| 193 | + const response = await search(index.index, query.split(/\s+/).join(' OR '), maxResults); |
| 194 | + index.close(); |
| 195 | + |
172 | 196 | if (findEvents) { |
173 | 197 | const eventOptions: SingleSearchOptions = { maxResults }; |
174 | 198 | const { maxSize, filter: filterStr } = argv; |
175 | | - if (maxSize) eventOptions.maxSize = maxSize; |
| 199 | + if (maxSize) eventOptions.maxSize = parseInt(maxSize, 10); |
176 | 200 | if (filterStr) eventOptions.filter = deserializeFilter(filterStr); |
177 | 201 |
|
178 | 202 | const { results } = response; |
|
0 commit comments