@mycodemap/mycodemap 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (329) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/LICENSE +21 -0
  3. package/README.md +559 -0
  4. package/dist/ai/claude.d.ts +38 -0
  5. package/dist/ai/claude.d.ts.map +1 -0
  6. package/dist/ai/claude.js +169 -0
  7. package/dist/ai/claude.js.map +1 -0
  8. package/dist/ai/codex.d.ts +38 -0
  9. package/dist/ai/codex.d.ts.map +1 -0
  10. package/dist/ai/codex.js +169 -0
  11. package/dist/ai/codex.js.map +1 -0
  12. package/dist/ai/factory.d.ts +48 -0
  13. package/dist/ai/factory.d.ts.map +1 -0
  14. package/dist/ai/factory.js +95 -0
  15. package/dist/ai/factory.js.map +1 -0
  16. package/dist/ai/index.d.ts +12 -0
  17. package/dist/ai/index.d.ts.map +1 -0
  18. package/dist/ai/index.js +29 -0
  19. package/dist/ai/index.js.map +1 -0
  20. package/dist/ai/provider.d.ts +70 -0
  21. package/dist/ai/provider.d.ts.map +1 -0
  22. package/dist/ai/provider.js +31 -0
  23. package/dist/ai/provider.js.map +1 -0
  24. package/dist/ai/subagent-caller.d.ts +90 -0
  25. package/dist/ai/subagent-caller.d.ts.map +1 -0
  26. package/dist/ai/subagent-caller.js +280 -0
  27. package/dist/ai/subagent-caller.js.map +1 -0
  28. package/dist/ai/types.d.ts +70 -0
  29. package/dist/ai/types.d.ts.map +1 -0
  30. package/dist/ai/types.js +5 -0
  31. package/dist/ai/types.js.map +1 -0
  32. package/dist/cache/file-hash-cache.d.ts +76 -0
  33. package/dist/cache/file-hash-cache.d.ts.map +1 -0
  34. package/dist/cache/file-hash-cache.js +159 -0
  35. package/dist/cache/file-hash-cache.js.map +1 -0
  36. package/dist/cache/index.d.ts +151 -0
  37. package/dist/cache/index.d.ts.map +1 -0
  38. package/dist/cache/index.js +303 -0
  39. package/dist/cache/index.js.map +1 -0
  40. package/dist/cache/lru-cache.d.ts +93 -0
  41. package/dist/cache/lru-cache.d.ts.map +1 -0
  42. package/dist/cache/lru-cache.js +194 -0
  43. package/dist/cache/lru-cache.js.map +1 -0
  44. package/dist/cache/parse-cache.d.ts +68 -0
  45. package/dist/cache/parse-cache.d.ts.map +1 -0
  46. package/dist/cache/parse-cache.js +173 -0
  47. package/dist/cache/parse-cache.js.map +1 -0
  48. package/dist/cli/commands/analyze.d.ts +96 -0
  49. package/dist/cli/commands/analyze.d.ts.map +1 -0
  50. package/dist/cli/commands/analyze.js +567 -0
  51. package/dist/cli/commands/analyze.js.map +1 -0
  52. package/dist/cli/commands/ci.d.ts +25 -0
  53. package/dist/cli/commands/ci.d.ts.map +1 -0
  54. package/dist/cli/commands/ci.js +481 -0
  55. package/dist/cli/commands/ci.js.map +1 -0
  56. package/dist/cli/commands/complexity.d.ts +70 -0
  57. package/dist/cli/commands/complexity.d.ts.map +1 -0
  58. package/dist/cli/commands/complexity.js +479 -0
  59. package/dist/cli/commands/complexity.js.map +1 -0
  60. package/dist/cli/commands/cycles.d.ts +10 -0
  61. package/dist/cli/commands/cycles.d.ts.map +1 -0
  62. package/dist/cli/commands/cycles.js +202 -0
  63. package/dist/cli/commands/cycles.js.map +1 -0
  64. package/dist/cli/commands/deps.d.ts +61 -0
  65. package/dist/cli/commands/deps.d.ts.map +1 -0
  66. package/dist/cli/commands/deps.js +340 -0
  67. package/dist/cli/commands/deps.js.map +1 -0
  68. package/dist/cli/commands/generate.d.ts +8 -0
  69. package/dist/cli/commands/generate.d.ts.map +1 -0
  70. package/dist/cli/commands/generate.js +61 -0
  71. package/dist/cli/commands/generate.js.map +1 -0
  72. package/dist/cli/commands/impact.d.ts +55 -0
  73. package/dist/cli/commands/impact.d.ts.map +1 -0
  74. package/dist/cli/commands/impact.js +455 -0
  75. package/dist/cli/commands/impact.js.map +1 -0
  76. package/dist/cli/commands/init.d.ts +4 -0
  77. package/dist/cli/commands/init.d.ts.map +1 -0
  78. package/dist/cli/commands/init.js +50 -0
  79. package/dist/cli/commands/init.js.map +1 -0
  80. package/dist/cli/commands/query.d.ts +22 -0
  81. package/dist/cli/commands/query.d.ts.map +1 -0
  82. package/dist/cli/commands/query.js +718 -0
  83. package/dist/cli/commands/query.js.map +1 -0
  84. package/dist/cli/commands/watch-foreground.d.ts +9 -0
  85. package/dist/cli/commands/watch-foreground.d.ts.map +1 -0
  86. package/dist/cli/commands/watch-foreground.js +67 -0
  87. package/dist/cli/commands/watch-foreground.js.map +1 -0
  88. package/dist/cli/commands/watch.d.ts +12 -0
  89. package/dist/cli/commands/watch.d.ts.map +1 -0
  90. package/dist/cli/commands/watch.js +114 -0
  91. package/dist/cli/commands/watch.js.map +1 -0
  92. package/dist/cli/commands/workflow.d.ts +25 -0
  93. package/dist/cli/commands/workflow.d.ts.map +1 -0
  94. package/dist/cli/commands/workflow.js +488 -0
  95. package/dist/cli/commands/workflow.js.map +1 -0
  96. package/dist/cli/index.d.ts +3 -0
  97. package/dist/cli/index.d.ts.map +1 -0
  98. package/dist/cli/index.js +115 -0
  99. package/dist/cli/index.js.map +1 -0
  100. package/dist/cli/paths.d.ts +90 -0
  101. package/dist/cli/paths.d.ts.map +1 -0
  102. package/dist/cli/paths.js +136 -0
  103. package/dist/cli/paths.js.map +1 -0
  104. package/dist/cli/runtime-logger.d.ts +13 -0
  105. package/dist/cli/runtime-logger.d.ts.map +1 -0
  106. package/dist/cli/runtime-logger.js +213 -0
  107. package/dist/cli/runtime-logger.js.map +1 -0
  108. package/dist/core/analyzer.d.ts +3 -0
  109. package/dist/core/analyzer.d.ts.map +1 -0
  110. package/dist/core/analyzer.js +359 -0
  111. package/dist/core/analyzer.js.map +1 -0
  112. package/dist/core/ast-complexity-analyzer.d.ts +40 -0
  113. package/dist/core/ast-complexity-analyzer.d.ts.map +1 -0
  114. package/dist/core/ast-complexity-analyzer.js +279 -0
  115. package/dist/core/ast-complexity-analyzer.js.map +1 -0
  116. package/dist/core/global-index.d.ts +69 -0
  117. package/dist/core/global-index.d.ts.map +1 -0
  118. package/dist/core/global-index.js +388 -0
  119. package/dist/core/global-index.js.map +1 -0
  120. package/dist/generator/ai-overview.d.ts +51 -0
  121. package/dist/generator/ai-overview.d.ts.map +1 -0
  122. package/dist/generator/ai-overview.js +160 -0
  123. package/dist/generator/ai-overview.js.map +1 -0
  124. package/dist/generator/context.d.ts +5 -0
  125. package/dist/generator/context.d.ts.map +1 -0
  126. package/dist/generator/context.js +514 -0
  127. package/dist/generator/context.js.map +1 -0
  128. package/dist/generator/file-describer.d.ts +93 -0
  129. package/dist/generator/file-describer.d.ts.map +1 -0
  130. package/dist/generator/file-describer.js +375 -0
  131. package/dist/generator/file-describer.js.map +1 -0
  132. package/dist/generator/index.d.ts +8 -0
  133. package/dist/generator/index.d.ts.map +1 -0
  134. package/dist/generator/index.js +259 -0
  135. package/dist/generator/index.js.map +1 -0
  136. package/dist/index.d.ts +5 -0
  137. package/dist/index.d.ts.map +1 -0
  138. package/dist/index.js +6 -0
  139. package/dist/index.js.map +1 -0
  140. package/dist/orchestrator/adapters/ast-grep-adapter.d.ts +75 -0
  141. package/dist/orchestrator/adapters/ast-grep-adapter.d.ts.map +1 -0
  142. package/dist/orchestrator/adapters/ast-grep-adapter.js +242 -0
  143. package/dist/orchestrator/adapters/ast-grep-adapter.js.map +1 -0
  144. package/dist/orchestrator/adapters/base-adapter.d.ts +24 -0
  145. package/dist/orchestrator/adapters/base-adapter.d.ts.map +1 -0
  146. package/dist/orchestrator/adapters/base-adapter.js +2 -0
  147. package/dist/orchestrator/adapters/base-adapter.js.map +1 -0
  148. package/dist/orchestrator/adapters/codemap-adapter.d.ts +56 -0
  149. package/dist/orchestrator/adapters/codemap-adapter.d.ts.map +1 -0
  150. package/dist/orchestrator/adapters/codemap-adapter.js +163 -0
  151. package/dist/orchestrator/adapters/codemap-adapter.js.map +1 -0
  152. package/dist/orchestrator/adapters/index.d.ts +10 -0
  153. package/dist/orchestrator/adapters/index.d.ts.map +1 -0
  154. package/dist/orchestrator/adapters/index.js +3 -0
  155. package/dist/orchestrator/adapters/index.js.map +1 -0
  156. package/dist/orchestrator/ai-feed-generator.d.ts +210 -0
  157. package/dist/orchestrator/ai-feed-generator.d.ts.map +1 -0
  158. package/dist/orchestrator/ai-feed-generator.js +377 -0
  159. package/dist/orchestrator/ai-feed-generator.js.map +1 -0
  160. package/dist/orchestrator/commit-validator.d.ts +30 -0
  161. package/dist/orchestrator/commit-validator.d.ts.map +1 -0
  162. package/dist/orchestrator/commit-validator.js +87 -0
  163. package/dist/orchestrator/commit-validator.js.map +1 -0
  164. package/dist/orchestrator/confidence.d.ts +25 -0
  165. package/dist/orchestrator/confidence.d.ts.map +1 -0
  166. package/dist/orchestrator/confidence.js +138 -0
  167. package/dist/orchestrator/confidence.js.map +1 -0
  168. package/dist/orchestrator/file-header-scanner.d.ts +48 -0
  169. package/dist/orchestrator/file-header-scanner.d.ts.map +1 -0
  170. package/dist/orchestrator/file-header-scanner.js +158 -0
  171. package/dist/orchestrator/file-header-scanner.js.map +1 -0
  172. package/dist/orchestrator/git-analyzer.d.ts +192 -0
  173. package/dist/orchestrator/git-analyzer.d.ts.map +1 -0
  174. package/dist/orchestrator/git-analyzer.js +539 -0
  175. package/dist/orchestrator/git-analyzer.js.map +1 -0
  176. package/dist/orchestrator/index.d.ts +20 -0
  177. package/dist/orchestrator/index.d.ts.map +1 -0
  178. package/dist/orchestrator/index.js +16 -0
  179. package/dist/orchestrator/index.js.map +1 -0
  180. package/dist/orchestrator/intent-router.d.ts +41 -0
  181. package/dist/orchestrator/intent-router.d.ts.map +1 -0
  182. package/dist/orchestrator/intent-router.js +98 -0
  183. package/dist/orchestrator/intent-router.js.map +1 -0
  184. package/dist/orchestrator/result-fusion.d.ts +114 -0
  185. package/dist/orchestrator/result-fusion.d.ts.map +1 -0
  186. package/dist/orchestrator/result-fusion.js +332 -0
  187. package/dist/orchestrator/result-fusion.js.map +1 -0
  188. package/dist/orchestrator/test-linker.d.ts +166 -0
  189. package/dist/orchestrator/test-linker.d.ts.map +1 -0
  190. package/dist/orchestrator/test-linker.js +570 -0
  191. package/dist/orchestrator/test-linker.js.map +1 -0
  192. package/dist/orchestrator/tool-orchestrator.d.ts +108 -0
  193. package/dist/orchestrator/tool-orchestrator.d.ts.map +1 -0
  194. package/dist/orchestrator/tool-orchestrator.js +247 -0
  195. package/dist/orchestrator/tool-orchestrator.js.map +1 -0
  196. package/dist/orchestrator/types.d.ts +162 -0
  197. package/dist/orchestrator/types.d.ts.map +1 -0
  198. package/dist/orchestrator/types.js +39 -0
  199. package/dist/orchestrator/types.js.map +1 -0
  200. package/dist/orchestrator/workflow/ci-executor.d.ts +157 -0
  201. package/dist/orchestrator/workflow/ci-executor.d.ts.map +1 -0
  202. package/dist/orchestrator/workflow/ci-executor.js +423 -0
  203. package/dist/orchestrator/workflow/ci-executor.js.map +1 -0
  204. package/dist/orchestrator/workflow/config.d.ts +97 -0
  205. package/dist/orchestrator/workflow/config.d.ts.map +1 -0
  206. package/dist/orchestrator/workflow/config.js +115 -0
  207. package/dist/orchestrator/workflow/config.js.map +1 -0
  208. package/dist/orchestrator/workflow/git-analyzer.d.ts +173 -0
  209. package/dist/orchestrator/workflow/git-analyzer.d.ts.map +1 -0
  210. package/dist/orchestrator/workflow/git-analyzer.js +473 -0
  211. package/dist/orchestrator/workflow/git-analyzer.js.map +1 -0
  212. package/dist/orchestrator/workflow/index.d.ts +21 -0
  213. package/dist/orchestrator/workflow/index.d.ts.map +1 -0
  214. package/dist/orchestrator/workflow/index.js +21 -0
  215. package/dist/orchestrator/workflow/index.js.map +1 -0
  216. package/dist/orchestrator/workflow/phase-checkpoint.d.ts +38 -0
  217. package/dist/orchestrator/workflow/phase-checkpoint.d.ts.map +1 -0
  218. package/dist/orchestrator/workflow/phase-checkpoint.js +75 -0
  219. package/dist/orchestrator/workflow/phase-checkpoint.js.map +1 -0
  220. package/dist/orchestrator/workflow/phase-inheritance.d.ts +128 -0
  221. package/dist/orchestrator/workflow/phase-inheritance.d.ts.map +1 -0
  222. package/dist/orchestrator/workflow/phase-inheritance.js +266 -0
  223. package/dist/orchestrator/workflow/phase-inheritance.js.map +1 -0
  224. package/dist/orchestrator/workflow/result-fusion.d.ts +117 -0
  225. package/dist/orchestrator/workflow/result-fusion.d.ts.map +1 -0
  226. package/dist/orchestrator/workflow/result-fusion.js +246 -0
  227. package/dist/orchestrator/workflow/result-fusion.js.map +1 -0
  228. package/dist/orchestrator/workflow/templates.d.ts +116 -0
  229. package/dist/orchestrator/workflow/templates.d.ts.map +1 -0
  230. package/dist/orchestrator/workflow/templates.js +546 -0
  231. package/dist/orchestrator/workflow/templates.js.map +1 -0
  232. package/dist/orchestrator/workflow/test-linker.d.ts +152 -0
  233. package/dist/orchestrator/workflow/test-linker.d.ts.map +1 -0
  234. package/dist/orchestrator/workflow/test-linker.js +342 -0
  235. package/dist/orchestrator/workflow/test-linker.js.map +1 -0
  236. package/dist/orchestrator/workflow/types.d.ts +202 -0
  237. package/dist/orchestrator/workflow/types.d.ts.map +1 -0
  238. package/dist/orchestrator/workflow/types.js +4 -0
  239. package/dist/orchestrator/workflow/types.js.map +1 -0
  240. package/dist/orchestrator/workflow/visualizer.d.ts +56 -0
  241. package/dist/orchestrator/workflow/visualizer.d.ts.map +1 -0
  242. package/dist/orchestrator/workflow/visualizer.js +300 -0
  243. package/dist/orchestrator/workflow/visualizer.js.map +1 -0
  244. package/dist/orchestrator/workflow/workflow-context.d.ts +50 -0
  245. package/dist/orchestrator/workflow/workflow-context.d.ts.map +1 -0
  246. package/dist/orchestrator/workflow/workflow-context.js +105 -0
  247. package/dist/orchestrator/workflow/workflow-context.js.map +1 -0
  248. package/dist/orchestrator/workflow/workflow-orchestrator.d.ts +126 -0
  249. package/dist/orchestrator/workflow/workflow-orchestrator.d.ts.map +1 -0
  250. package/dist/orchestrator/workflow/workflow-orchestrator.js +489 -0
  251. package/dist/orchestrator/workflow/workflow-orchestrator.js.map +1 -0
  252. package/dist/orchestrator/workflow/workflow-persistence.d.ts +38 -0
  253. package/dist/orchestrator/workflow/workflow-persistence.d.ts.map +1 -0
  254. package/dist/orchestrator/workflow/workflow-persistence.js +166 -0
  255. package/dist/orchestrator/workflow/workflow-persistence.js.map +1 -0
  256. package/dist/parser/implementations/fast-parser.d.ts +65 -0
  257. package/dist/parser/implementations/fast-parser.d.ts.map +1 -0
  258. package/dist/parser/implementations/fast-parser.js +299 -0
  259. package/dist/parser/implementations/fast-parser.js.map +1 -0
  260. package/dist/parser/implementations/smart-parser.d.ts +220 -0
  261. package/dist/parser/implementations/smart-parser.d.ts.map +1 -0
  262. package/dist/parser/implementations/smart-parser.js +1536 -0
  263. package/dist/parser/implementations/smart-parser.js.map +1 -0
  264. package/dist/parser/implementations/tree-sitter-parser.d.ts +57 -0
  265. package/dist/parser/implementations/tree-sitter-parser.d.ts.map +1 -0
  266. package/dist/parser/implementations/tree-sitter-parser.js +375 -0
  267. package/dist/parser/implementations/tree-sitter-parser.js.map +1 -0
  268. package/dist/parser/index.d.ts +19 -0
  269. package/dist/parser/index.d.ts.map +1 -0
  270. package/dist/parser/index.js +449 -0
  271. package/dist/parser/index.js.map +1 -0
  272. package/dist/parser/interfaces/IParser.d.ts +218 -0
  273. package/dist/parser/interfaces/IParser.d.ts.map +1 -0
  274. package/dist/parser/interfaces/IParser.js +22 -0
  275. package/dist/parser/interfaces/IParser.js.map +1 -0
  276. package/dist/plugins/built-in/call-graph.d.ts +38 -0
  277. package/dist/plugins/built-in/call-graph.d.ts.map +1 -0
  278. package/dist/plugins/built-in/call-graph.js +135 -0
  279. package/dist/plugins/built-in/call-graph.js.map +1 -0
  280. package/dist/plugins/built-in/complexity-analyzer.d.ts +45 -0
  281. package/dist/plugins/built-in/complexity-analyzer.d.ts.map +1 -0
  282. package/dist/plugins/built-in/complexity-analyzer.js +155 -0
  283. package/dist/plugins/built-in/complexity-analyzer.js.map +1 -0
  284. package/dist/plugins/index.d.ts +34 -0
  285. package/dist/plugins/index.d.ts.map +1 -0
  286. package/dist/plugins/index.js +103 -0
  287. package/dist/plugins/index.js.map +1 -0
  288. package/dist/plugins/plugin-loader.d.ts +22 -0
  289. package/dist/plugins/plugin-loader.d.ts.map +1 -0
  290. package/dist/plugins/plugin-loader.js +200 -0
  291. package/dist/plugins/plugin-loader.js.map +1 -0
  292. package/dist/plugins/plugin-registry.d.ts +20 -0
  293. package/dist/plugins/plugin-registry.d.ts.map +1 -0
  294. package/dist/plugins/plugin-registry.js +143 -0
  295. package/dist/plugins/plugin-registry.js.map +1 -0
  296. package/dist/plugins/types.d.ts +72 -0
  297. package/dist/plugins/types.d.ts.map +1 -0
  298. package/dist/plugins/types.js +5 -0
  299. package/dist/plugins/types.js.map +1 -0
  300. package/dist/types/index.d.ts +373 -0
  301. package/dist/types/index.d.ts.map +1 -0
  302. package/dist/types/index.js +7 -0
  303. package/dist/types/index.js.map +1 -0
  304. package/dist/watcher/daemon.d.ts +64 -0
  305. package/dist/watcher/daemon.d.ts.map +1 -0
  306. package/dist/watcher/daemon.js +189 -0
  307. package/dist/watcher/daemon.js.map +1 -0
  308. package/dist/watcher/file-watcher.d.ts +70 -0
  309. package/dist/watcher/file-watcher.d.ts.map +1 -0
  310. package/dist/watcher/file-watcher.js +127 -0
  311. package/dist/watcher/file-watcher.js.map +1 -0
  312. package/dist/watcher/index.d.ts +5 -0
  313. package/dist/watcher/index.d.ts.map +1 -0
  314. package/dist/watcher/index.js +6 -0
  315. package/dist/watcher/index.js.map +1 -0
  316. package/dist/watcher/watch-worker.d.ts +2 -0
  317. package/dist/watcher/watch-worker.d.ts.map +1 -0
  318. package/dist/watcher/watch-worker.js +57 -0
  319. package/dist/watcher/watch-worker.js.map +1 -0
  320. package/dist/worker/index.d.ts +76 -0
  321. package/dist/worker/index.d.ts.map +1 -0
  322. package/dist/worker/index.js +155 -0
  323. package/dist/worker/index.js.map +1 -0
  324. package/dist/worker/parse-worker.d.ts +2 -0
  325. package/dist/worker/parse-worker.d.ts.map +1 -0
  326. package/dist/worker/parse-worker.js +202 -0
  327. package/dist/worker/parse-worker.js.map +1 -0
  328. package/mycodemap.config.schema.json +42 -0
  329. package/package.json +69 -0
@@ -0,0 +1,718 @@
1
+ // [META] since:2026-03-03 | owner:orchestrator-team | stable:true
2
+ // [WHY] 查询命令优化:改进缓存策略、添加索引结构、添加性能指标输出
3
+ import chalk from 'chalk';
4
+ import fs from 'fs';
5
+ import path from 'path';
6
+ import { resolveDataPath } from '../paths.js';
7
+ // ===== 性能优化:改进的缓存机制 =====
8
+ // 缓存配置
9
+ const CACHE_CONFIG = {
10
+ TTL: 60000, // 60秒缓存(从5秒提升)
11
+ MAX_SIZE: 10, // 最大缓存 10 个项目的索引
12
+ };
13
+ const indexCache = new Map();
14
+ /**
15
+ * 标准化依赖名称,用于去重比较
16
+ * 1. 去除扩展名(如 .ts, .js)
17
+ * 2. 去除 ./ 前缀
18
+ * 3. 转换为小写
19
+ */
20
+ function normalizeDependencyName(name) {
21
+ return name
22
+ .replace(/\.[jt]s$/, "") // 去除 .ts 或 .js 扩展名
23
+ .replace(/^\.\//, "") // 去除 ./ 前缀
24
+ .toLowerCase();
25
+ }
26
+ /**
27
+ * 预构建索引以加速查询
28
+ */
29
+ function buildIndex(codeMap, rootDir) {
30
+ const index = {
31
+ symbols: new Map(),
32
+ symbolsExact: new Map(),
33
+ modules: new Map(),
34
+ dependencies: new Map(),
35
+ prefixIndex: new Map(),
36
+ prefixIndexExact: new Map(),
37
+ };
38
+ for (const module of codeMap.modules) {
39
+ const relativePath = path.relative(rootDir, module.absolutePath);
40
+ const moduleIndex = {
41
+ absolutePath: module.absolutePath,
42
+ relativePath,
43
+ type: module.type,
44
+ exports: module.exports.map(e => e.name),
45
+ };
46
+ // 索引模块(使用相对路径作为主键)
47
+ index.modules.set(relativePath.toLowerCase(), moduleIndex);
48
+ // 索引导出
49
+ for (const exp of module.exports) {
50
+ const entry = {
51
+ name: exp.name,
52
+ kind: exp.kind,
53
+ filePath: relativePath,
54
+ isExported: true,
55
+ };
56
+ // 精确索引(不区分大小写)
57
+ const lowerName = exp.name.toLowerCase();
58
+ if (!index.symbols.has(lowerName)) {
59
+ index.symbols.set(lowerName, []);
60
+ }
61
+ index.symbols.get(lowerName).push(entry);
62
+ // 精确索引(区分大小写)
63
+ if (!index.symbolsExact.has(exp.name)) {
64
+ index.symbolsExact.set(exp.name, []);
65
+ }
66
+ index.symbolsExact.get(exp.name).push(entry);
67
+ // 前缀索引(不区分大小写)
68
+ for (let i = 1; i <= exp.name.length; i++) {
69
+ const prefix = exp.name.substring(0, i).toLowerCase();
70
+ if (!index.prefixIndex.has(prefix)) {
71
+ index.prefixIndex.set(prefix, []);
72
+ }
73
+ index.prefixIndex.get(prefix).push(entry);
74
+ }
75
+ // 前缀索引(区分大小写)
76
+ for (let i = 1; i <= exp.name.length; i++) {
77
+ const prefix = exp.name.substring(0, i);
78
+ if (!index.prefixIndexExact.has(prefix)) {
79
+ index.prefixIndexExact.set(prefix, []);
80
+ }
81
+ index.prefixIndexExact.get(prefix).push(entry);
82
+ }
83
+ }
84
+ // 索引符号
85
+ for (const sym of module.symbols) {
86
+ const entry = {
87
+ name: sym.name,
88
+ kind: sym.kind,
89
+ filePath: relativePath,
90
+ line: sym.location.line,
91
+ column: sym.location.column,
92
+ isExported: false,
93
+ };
94
+ const lowerName = sym.name.toLowerCase();
95
+ if (!index.symbols.has(lowerName)) {
96
+ index.symbols.set(lowerName, []);
97
+ }
98
+ index.symbols.get(lowerName).push(entry);
99
+ // 精确索引(区分大小写)
100
+ if (!index.symbolsExact.has(sym.name)) {
101
+ index.symbolsExact.set(sym.name, []);
102
+ }
103
+ index.symbolsExact.get(sym.name).push(entry);
104
+ // 前缀索引(不区分大小写)
105
+ for (let i = 1; i <= sym.name.length; i++) {
106
+ const prefix = sym.name.substring(0, i).toLowerCase();
107
+ if (!index.prefixIndex.has(prefix)) {
108
+ index.prefixIndex.set(prefix, []);
109
+ }
110
+ index.prefixIndex.get(prefix).push(entry);
111
+ }
112
+ // 前缀索引(区分大小写)
113
+ for (let i = 1; i <= sym.name.length; i++) {
114
+ const prefix = sym.name.substring(0, i);
115
+ if (!index.prefixIndexExact.has(prefix)) {
116
+ index.prefixIndexExact.set(prefix, []);
117
+ }
118
+ index.prefixIndexExact.get(prefix).push(entry);
119
+ }
120
+ }
121
+ // 索引依赖和导入,进行去重处理
122
+ // 使用 Set 跟踪当前模块已添加的依赖(基于标准化名称)
123
+ const seenDeps = new Set();
124
+ // 先索引 dependencies(优先保留)
125
+ for (const dep of module.dependencies) {
126
+ const normalizedDep = normalizeDependencyName(dep);
127
+ const depKey = `${relativePath}:${normalizedDep}`;
128
+ // 跳过已存在的依赖
129
+ if (seenDeps.has(depKey)) {
130
+ continue;
131
+ }
132
+ seenDeps.add(depKey);
133
+ const depEntry = {
134
+ name: dep,
135
+ sourcePath: relativePath,
136
+ type: "dependency",
137
+ };
138
+ const lowerDep = dep.toLowerCase();
139
+ if (!index.dependencies.has(lowerDep)) {
140
+ index.dependencies.set(lowerDep, []);
141
+ }
142
+ index.dependencies.get(lowerDep).push(depEntry);
143
+ }
144
+ // 再索引 imports(如果与 dependency 重复则跳过)
145
+ for (const imp of module.imports) {
146
+ const normalizedSource = normalizeDependencyName(imp.source);
147
+ const impKey = `${relativePath}:${normalizedSource}`;
148
+ // 跳过已存在的依赖(避免与 dependency 重复)
149
+ if (seenDeps.has(impKey)) {
150
+ continue;
151
+ }
152
+ seenDeps.add(impKey);
153
+ const impEntry = {
154
+ name: imp.source,
155
+ sourcePath: relativePath,
156
+ type: "import",
157
+ };
158
+ const lowerSource = imp.source.toLowerCase();
159
+ if (!index.dependencies.has(lowerSource)) {
160
+ index.dependencies.set(lowerSource, []);
161
+ }
162
+ index.dependencies.get(lowerSource).push(impEntry);
163
+ }
164
+ }
165
+ return index;
166
+ }
167
+ /**
168
+ * 加载代码地图数据(带改进的缓存)
169
+ */
170
+ function loadCodeMap(rootDir, useCache = true) {
171
+ const codemapPath = resolveDataPath(rootDir);
172
+ const startTime = performance.now();
173
+ // 检查缓存
174
+ if (useCache) {
175
+ const cached = indexCache.get(rootDir);
176
+ if (cached && Date.now() - cached.timestamp < CACHE_CONFIG.TTL) {
177
+ return {
178
+ codeMap: cached.data,
179
+ index: cached.index,
180
+ cacheHit: true,
181
+ loadTime: 0,
182
+ };
183
+ }
184
+ }
185
+ if (!fs.existsSync(codemapPath)) {
186
+ console.log(chalk.red('错误: 代码地图不存在,请先运行 codemap generate'));
187
+ return { codeMap: null, index: null, cacheHit: false, loadTime: 0 };
188
+ }
189
+ try {
190
+ const data = fs.readFileSync(codemapPath, 'utf-8');
191
+ const parsed = JSON.parse(data);
192
+ const loadTime = performance.now() - startTime;
193
+ // 构建索引
194
+ const index = buildIndex(parsed, rootDir);
195
+ // 缓存(如果启用)
196
+ if (useCache) {
197
+ // 清理超出限制的旧缓存
198
+ if (indexCache.size >= CACHE_CONFIG.MAX_SIZE) {
199
+ // 删除最旧的缓存
200
+ let oldestKey = null;
201
+ let oldestTime = Infinity;
202
+ for (const [key, val] of indexCache.entries()) {
203
+ if (val.timestamp < oldestTime) {
204
+ oldestTime = val.timestamp;
205
+ oldestKey = key;
206
+ }
207
+ }
208
+ if (oldestKey) {
209
+ indexCache.delete(oldestKey);
210
+ }
211
+ }
212
+ indexCache.set(rootDir, {
213
+ data: parsed,
214
+ timestamp: Date.now(),
215
+ path: codemapPath,
216
+ index,
217
+ });
218
+ }
219
+ return {
220
+ codeMap: parsed,
221
+ index,
222
+ cacheHit: false,
223
+ loadTime,
224
+ };
225
+ }
226
+ catch (error) {
227
+ console.log(chalk.red('错误: 读取代码地图失败:', error instanceof Error ? error.message : String(error)));
228
+ return { codeMap: null, index: null, cacheHit: false, loadTime: 0 };
229
+ }
230
+ }
231
+ /**
232
+ * 清除缓存
233
+ */
234
+ function clearCache() {
235
+ indexCache.clear();
236
+ }
237
+ /**
238
+ * 查找符号的引用位置
239
+ */
240
+ function findReferences(codeMap, symbolName, excludeFile, caseSensitive = false) {
241
+ const references = [];
242
+ const searchName = caseSensitive ? symbolName : symbolName.toLowerCase();
243
+ for (const mod of codeMap.modules) {
244
+ const relPath = path.relative(codeMap.project.rootDir, mod.absolutePath);
245
+ // 跳过定义位置文件
246
+ if (relPath === excludeFile)
247
+ continue;
248
+ // 检查 imports 中是否引用了该符号
249
+ for (const imp of mod.imports) {
250
+ // 检查导入源
251
+ const impSource = caseSensitive ? imp.source : imp.source.toLowerCase();
252
+ if (impSource.includes(searchName)) {
253
+ references.push({
254
+ file: relPath,
255
+ line: 1, // 简化处理,显示第一行
256
+ type: 'import',
257
+ });
258
+ continue;
259
+ }
260
+ // 检查导入的 specifiers
261
+ for (const spec of imp.specifiers) {
262
+ const specName = caseSensitive ? spec.name : spec.name.toLowerCase();
263
+ if (specName === searchName) {
264
+ references.push({
265
+ file: relPath,
266
+ line: 1,
267
+ type: 'import',
268
+ });
269
+ break;
270
+ }
271
+ }
272
+ }
273
+ // 检查 exports 中是否导出该符号
274
+ for (const exp of mod.exports) {
275
+ const expName = caseSensitive ? exp.name : exp.name.toLowerCase();
276
+ if (expName === searchName) {
277
+ references.push({
278
+ file: relPath,
279
+ line: 1,
280
+ type: 'export',
281
+ });
282
+ }
283
+ }
284
+ }
285
+ return references.slice(0, 20); // 限制引用数量
286
+ }
287
+ /**
288
+ * 读取代码上下文
289
+ */
290
+ function readCodeContext(filePath, lineNumber, contextLines, rootDir) {
291
+ const fullPath = path.join(rootDir, filePath);
292
+ if (!fs.existsSync(fullPath)) {
293
+ return [];
294
+ }
295
+ try {
296
+ const content = fs.readFileSync(fullPath, 'utf-8');
297
+ const lines = content.split('\n');
298
+ const context = [];
299
+ const startLine = Math.max(1, lineNumber - contextLines);
300
+ const endLine = Math.min(lines.length, lineNumber + contextLines);
301
+ for (let i = startLine; i <= endLine; i++) {
302
+ context.push({
303
+ line: i,
304
+ content: lines[i - 1] || '',
305
+ });
306
+ }
307
+ return context;
308
+ }
309
+ catch {
310
+ return [];
311
+ }
312
+ }
313
+ /**
314
+ * 查询符号(使用索引)
315
+ */
316
+ function querySymbol(index, codeMap, symbolName, limit, caseSensitive = false, includeReferences = false, contextLines = 0) {
317
+ const results = [];
318
+ // 选择正确的索引
319
+ const symbolsMap = caseSensitive ? index.symbolsExact : index.symbols;
320
+ const prefixMap = caseSensitive ? index.prefixIndexExact : index.prefixIndex;
321
+ // 搜索键:不区分大小写时转换为小写
322
+ const searchKey = caseSensitive ? symbolName : symbolName.toLowerCase();
323
+ // 用于去重的 Set
324
+ const seenResults = new Set();
325
+ // 首先尝试精确匹配(完全相等)
326
+ const exactMatches = symbolsMap.get(searchKey);
327
+ if (exactMatches) {
328
+ for (const entry of exactMatches) {
329
+ const key = `${entry.name}:${entry.filePath}`;
330
+ if (!seenResults.has(key)) {
331
+ seenResults.add(key);
332
+ results.push({
333
+ name: entry.name,
334
+ path: path.join(codeMap.project.rootDir, entry.filePath),
335
+ kind: entry.kind,
336
+ details: `${entry.isExported ? '导出于' : '定义于'} ${entry.filePath}${entry.line ? ':' + entry.line : ''}`,
337
+ location: { file: entry.filePath, line: entry.line, column: entry.column },
338
+ isExported: entry.isExported,
339
+ });
340
+ }
341
+ }
342
+ }
343
+ // 尝试前缀匹配(以搜索词开头)
344
+ const prefixResults = prefixMap.get(searchKey) || [];
345
+ for (const entry of prefixResults) {
346
+ const key = `${entry.name}:${entry.filePath}`;
347
+ if (!seenResults.has(key)) {
348
+ seenResults.add(key);
349
+ results.push({
350
+ name: entry.name,
351
+ path: path.join(codeMap.project.rootDir, entry.filePath),
352
+ kind: entry.kind,
353
+ details: `${entry.isExported ? '导出于' : '定义于'} ${entry.filePath}${entry.line ? ':' + entry.line : ''}`,
354
+ location: { file: entry.filePath, line: entry.line, column: entry.column },
355
+ isExported: entry.isExported,
356
+ });
357
+ }
358
+ }
359
+ // 尝试子串匹配(包含匹配)
360
+ // 例如:搜索 "Analyzer" 应匹配 "GitAnalyzer"
361
+ for (const [key, entries] of symbolsMap.entries()) {
362
+ if (key.includes(searchKey) && !key.startsWith(searchKey)) {
363
+ // 跳过已经通过精确匹配和前缀匹配找到的键
364
+ for (const entry of entries) {
365
+ const resultKey = `${entry.name}:${entry.filePath}`;
366
+ if (!seenResults.has(resultKey)) {
367
+ seenResults.add(resultKey);
368
+ results.push({
369
+ name: entry.name,
370
+ path: path.join(codeMap.project.rootDir, entry.filePath),
371
+ kind: entry.kind,
372
+ details: `${entry.isExported ? '导出于' : '定义于'} ${entry.filePath}${entry.line ? ':' + entry.line : ''}`,
373
+ location: { file: entry.filePath, line: entry.line, column: entry.column },
374
+ isExported: entry.isExported,
375
+ });
376
+ }
377
+ }
378
+ }
379
+ }
380
+ // 添加引用信息
381
+ if (includeReferences) {
382
+ for (const result of results) {
383
+ result.references = findReferences(codeMap, symbolName, result.location?.file || '', caseSensitive);
384
+ }
385
+ }
386
+ // 添加代码上下文
387
+ if (contextLines > 0) {
388
+ for (const result of results) {
389
+ result.context = readCodeContext(result.location?.file || '', result.location?.line || 0, contextLines, codeMap.project.rootDir);
390
+ }
391
+ }
392
+ return results.slice(0, limit);
393
+ }
394
+ /**
395
+ * 查询模块(使用索引)
396
+ */
397
+ function queryModule(index, codeMap, modulePath, limit) {
398
+ const results = [];
399
+ const searchPath = modulePath.toLowerCase();
400
+ // 使用索引查找
401
+ for (const [key, moduleIdx] of index.modules.entries()) {
402
+ if (key.includes(searchPath) || moduleIdx.absolutePath.toLowerCase().includes(searchPath)) {
403
+ const exports = moduleIdx.exports.join(', ') || '无导出';
404
+ results.push({
405
+ name: moduleIdx.relativePath,
406
+ path: moduleIdx.absolutePath,
407
+ kind: moduleIdx.type,
408
+ details: `导出: ${exports}`,
409
+ location: { file: moduleIdx.relativePath },
410
+ isExported: moduleIdx.exports.length > 0,
411
+ });
412
+ }
413
+ }
414
+ return results.slice(0, limit);
415
+ }
416
+ /**
417
+ * 查询依赖(使用索引)
418
+ * @param index 符号索引
419
+ * @param codeMap 代码地图数据
420
+ * @param depName 依赖名称
421
+ * @param limit 结果数量限制
422
+ * @param deduplicate 是否去重(默认true)
423
+ */
424
+ function queryDeps(index, codeMap, depName, limit, deduplicate = true) {
425
+ const results = [];
426
+ const searchDep = depName.toLowerCase();
427
+ // 使用索引查找
428
+ const depEntries = index.dependencies.get(searchDep);
429
+ if (depEntries) {
430
+ for (const entry of depEntries) {
431
+ results.push({
432
+ name: entry.name,
433
+ path: path.join(codeMap.project.rootDir, entry.sourcePath),
434
+ kind: entry.type,
435
+ details: `${entry.type === 'dependency' ? '被' : '导入自'} ${entry.sourcePath} 引用`,
436
+ location: { file: entry.sourcePath },
437
+ isExported: false,
438
+ });
439
+ }
440
+ }
441
+ // 去重:基于 name:sourcePath 组合去重,避免 dependency 和 import 重复
442
+ if (deduplicate) {
443
+ const unique = new Map();
444
+ for (const item of results) {
445
+ const key = `${item.name}:${item.path}`;
446
+ if (!unique.has(key)) {
447
+ unique.set(key, item);
448
+ }
449
+ }
450
+ return Array.from(unique.values()).slice(0, limit);
451
+ }
452
+ return results.slice(0, limit);
453
+ }
454
+ /**
455
+ * 模糊搜索(使用索引)
456
+ * @param index 符号索引
457
+ * @param codeMap 代码地图数据
458
+ * @param keyword 搜索关键词
459
+ * @param limit 结果数量限制
460
+ * @param useRegex 是否使用正则表达式搜索
461
+ * @param caseSensitive 大小写敏感
462
+ * @param contextLines 代码上下文行数
463
+ */
464
+ function search(index, codeMap, keyword, limit, useRegex = false, caseSensitive = false, contextLines = 0) {
465
+ const results = [];
466
+ // 正则表达式模式
467
+ let regex = null;
468
+ if (useRegex) {
469
+ try {
470
+ const flags = caseSensitive ? '' : 'i';
471
+ regex = new RegExp(keyword, flags);
472
+ }
473
+ catch {
474
+ // 正则表达式无效,回退到普通搜索
475
+ regex = null;
476
+ }
477
+ }
478
+ // 1. 搜索模块路径
479
+ for (const [key, moduleIdx] of index.modules.entries()) {
480
+ const searchTarget = caseSensitive ? key : key.toLowerCase();
481
+ const keywordLower = keyword.toLowerCase();
482
+ const matches = regex
483
+ ? regex.test(key) || regex.test(moduleIdx.relativePath)
484
+ : searchTarget.includes(keywordLower);
485
+ if (matches) {
486
+ results.push({
487
+ name: moduleIdx.relativePath,
488
+ path: moduleIdx.absolutePath,
489
+ kind: 'module',
490
+ details: useRegex ? `正则匹配` : `模块匹配`,
491
+ location: { file: moduleIdx.relativePath },
492
+ isExported: false,
493
+ });
494
+ }
495
+ }
496
+ // 2. 搜索符号和导出(使用前缀索引)
497
+ // 选择正确的索引
498
+ const symbolsMap = caseSensitive ? index.symbolsExact : index.symbols;
499
+ const prefixMap = caseSensitive ? index.prefixIndexExact : index.prefixIndex;
500
+ if (useRegex && regex) {
501
+ // 正则模式:遍历所有符号进行匹配
502
+ for (const [symbolKey, entries] of symbolsMap.entries()) {
503
+ const matchTarget = caseSensitive ? symbolKey : symbolKey.toLowerCase();
504
+ if (regex.test(matchTarget) || regex.test(symbolKey)) {
505
+ for (const entry of entries) {
506
+ results.push({
507
+ name: entry.name,
508
+ path: path.join(codeMap.project.rootDir, entry.filePath),
509
+ kind: entry.isExported ? `export (${entry.kind})` : entry.kind,
510
+ details: `正则匹配于 ${entry.filePath}${entry.line ? ':' + entry.line : ''}`,
511
+ location: { file: entry.filePath, line: entry.line, column: entry.column },
512
+ isExported: entry.isExported,
513
+ });
514
+ }
515
+ }
516
+ }
517
+ }
518
+ else {
519
+ // 普通模式:使用前缀索引
520
+ // 搜索键:不区分大小写时转换为小写
521
+ const searchKey = caseSensitive ? keyword : keyword.toLowerCase();
522
+ const prefixResults = prefixMap.get(searchKey) || [];
523
+ for (const entry of prefixResults) {
524
+ results.push({
525
+ name: entry.name,
526
+ path: path.join(codeMap.project.rootDir, entry.filePath),
527
+ kind: entry.isExported ? `export (${entry.kind})` : entry.kind,
528
+ details: `${entry.isExported ? '导出于' : '定义于'} ${entry.filePath}${entry.line ? ':' + entry.line : ''}`,
529
+ location: { file: entry.filePath, line: entry.line, column: entry.column },
530
+ isExported: entry.isExported,
531
+ });
532
+ }
533
+ }
534
+ // 去重(基于 name:path 组合)
535
+ const unique = new Map();
536
+ for (const item of results) {
537
+ const key = `${item.name}:${item.path}`;
538
+ if (!unique.has(key)) {
539
+ unique.set(key, item);
540
+ }
541
+ }
542
+ const finalResults = Array.from(unique.values()).slice(0, limit);
543
+ // 添加代码上下文
544
+ if (contextLines > 0) {
545
+ for (const result of finalResults) {
546
+ result.context = readCodeContext(result.location?.file || '', result.location?.line || 0, contextLines, codeMap.project.rootDir);
547
+ }
548
+ }
549
+ return finalResults;
550
+ }
551
+ /**
552
+ * 格式化输出结果
553
+ */
554
+ function formatResults(result, isJson, verbose, structured = false) {
555
+ if (isJson) {
556
+ // 如果是 structured 模式,移除 details 字段
557
+ if (structured) {
558
+ const structuredResult = {
559
+ ...result,
560
+ results: result.results.map(item => {
561
+ const { details, ...rest } = item;
562
+ void details; // 标记为有意忽略的变量
563
+ return rest;
564
+ })
565
+ };
566
+ console.log(JSON.stringify(structuredResult, null, 2));
567
+ }
568
+ else {
569
+ console.log(JSON.stringify(result, null, 2));
570
+ }
571
+ return;
572
+ }
573
+ console.log(chalk.cyan(`\n查询 "${result.query}" (${result.type})`));
574
+ console.log(chalk.gray(` 找到 ${result.count} 个结果\n`));
575
+ if (result.count === 0) {
576
+ console.log(chalk.yellow(' 未找到匹配结果'));
577
+ return;
578
+ }
579
+ for (const item of result.results) {
580
+ console.log(chalk.green(` ${item.name}`));
581
+ if (item.kind) {
582
+ console.log(chalk.gray(` 类型: ${item.kind}`));
583
+ }
584
+ if (item.path) {
585
+ console.log(chalk.gray(` 路径: ${item.path}`));
586
+ }
587
+ if (item.details) {
588
+ console.log(chalk.gray(` ${item.details}`));
589
+ }
590
+ // 显示引用信息
591
+ if (item.references && item.references.length > 0) {
592
+ console.log(chalk.gray(` 引用:`));
593
+ for (const ref of item.references.slice(0, 5)) {
594
+ console.log(chalk.gray(` - ${ref.type}: ${ref.file}`));
595
+ }
596
+ if (item.references.length > 5) {
597
+ console.log(chalk.gray(` ... 还有 ${item.references.length - 5} 个引用`));
598
+ }
599
+ }
600
+ // 显示代码上下文
601
+ if (item.context && item.context.length > 0) {
602
+ console.log(chalk.gray(` 代码上下文:`));
603
+ for (const ctx of item.context) {
604
+ const prefix = ctx.line === item.location?.line ? '>' : ' ';
605
+ console.log(chalk.gray(` ${prefix}${ctx.line}: ${ctx.content}`));
606
+ }
607
+ }
608
+ console.log('');
609
+ }
610
+ // verbose 模式输出性能指标
611
+ if (verbose && result.metrics) {
612
+ const m = result.metrics;
613
+ console.log(chalk.cyan('性能指标:'));
614
+ console.log(chalk.gray(` 索引加载: ${m.indexLoadTime.toFixed(2)}ms ${m.cacheHit ? '(缓存命中)' : ''}`));
615
+ console.log(chalk.gray(` 查询执行: ${m.queryTime.toFixed(2)}ms`));
616
+ console.log(chalk.gray(` 总耗时: ${m.totalTime.toFixed(2)}ms`));
617
+ console.log(chalk.gray(` 索引大小: ${m.indexSize} 个条目`));
618
+ }
619
+ }
620
+ /**
621
+ * Query 命令实现
622
+ */
623
+ export async function queryCommand(options) {
624
+ const rootDir = process.cwd();
625
+ const limit = options.limit || 50;
626
+ const useCache = options.cache !== false; // 默认启用缓存
627
+ const verbose = options.verbose || false;
628
+ const useRegex = options.regex || false;
629
+ const depsFormat = options.depsFormat || 'default';
630
+ const caseSensitive = options.caseSensitive || false;
631
+ const contextLines = typeof options.context === 'string' ? parseInt(options.context, 10) : (options.context || 0);
632
+ const includeReferences = options.includeReferences || false;
633
+ // 清除缓存(如果指定 --no-cache)
634
+ if (!useCache) {
635
+ clearCache();
636
+ }
637
+ // 加载代码地图(带缓存)
638
+ const startTotal = performance.now();
639
+ const { codeMap, index, cacheHit, loadTime } = loadCodeMap(rootDir, useCache);
640
+ if (!codeMap || !index) {
641
+ process.exit(1);
642
+ }
643
+ const indexLoadTime = loadTime;
644
+ const indexSize = index.symbols.size + index.modules.size + index.dependencies.size;
645
+ let result;
646
+ const queryStartTime = performance.now();
647
+ // 执行查询
648
+ if (options.symbol) {
649
+ const items = querySymbol(index, codeMap, options.symbol, limit, caseSensitive, includeReferences, contextLines);
650
+ result = {
651
+ type: 'symbol',
652
+ query: options.symbol,
653
+ count: items.length,
654
+ results: items,
655
+ };
656
+ }
657
+ else if (options.module) {
658
+ const items = queryModule(index, codeMap, options.module, limit);
659
+ result = {
660
+ type: 'module',
661
+ query: options.module,
662
+ count: items.length,
663
+ results: items,
664
+ };
665
+ }
666
+ else if (options.deps) {
667
+ // 依赖查询:根据 depsFormat 决定是否去重
668
+ // default 模式下去重,detailed 模式显示完整信息(包含 dependency 和 import)
669
+ const deduplicate = depsFormat !== 'detailed';
670
+ const items = queryDeps(index, codeMap, options.deps, limit, deduplicate);
671
+ result = {
672
+ type: 'deps',
673
+ query: options.deps,
674
+ count: items.length,
675
+ results: items,
676
+ };
677
+ }
678
+ else if (options.search) {
679
+ const items = search(index, codeMap, options.search, limit, useRegex, caseSensitive, contextLines);
680
+ result = {
681
+ type: 'search',
682
+ query: options.search,
683
+ count: items.length,
684
+ results: items,
685
+ };
686
+ }
687
+ else {
688
+ console.log(chalk.red('错误: 请指定查询类型: --symbol, --module, --deps, 或 --search'));
689
+ console.log(chalk.gray('\n用法:'));
690
+ console.log(chalk.gray(' codemap query --symbol <name> # 查询符号'));
691
+ console.log(chalk.gray(' codemap query --module <path> # 查询模块'));
692
+ console.log(chalk.gray(' codemap query --deps <name> # 查询依赖'));
693
+ console.log(chalk.gray(' codemap query --search <word> # 模糊搜索'));
694
+ console.log(chalk.gray('\n选项:'));
695
+ console.log(chalk.gray(' -l, --limit <number> 限制结果数量'));
696
+ console.log(chalk.gray(' -j, --json JSON 格式输出'));
697
+ console.log(chalk.gray(' -v, --verbose 显示性能指标'));
698
+ console.log(chalk.gray(' -r, --regex 使用正则表达式搜索'));
699
+ console.log(chalk.gray(' --deps-format 依赖查询格式 (default|detailed)'));
700
+ console.log(chalk.gray(' --no-cache 禁用缓存'));
701
+ process.exit(1);
702
+ }
703
+ const queryTime = performance.now() - queryStartTime;
704
+ const totalTime = performance.now() - startTotal;
705
+ // 添加性能指标
706
+ if (verbose) {
707
+ result.metrics = {
708
+ indexLoadTime,
709
+ queryTime,
710
+ totalTime,
711
+ cacheHit,
712
+ indexSize,
713
+ };
714
+ }
715
+ // 输出结果
716
+ formatResults(result, options.json || false, verbose, options.structured || false);
717
+ }
718
+ //# sourceMappingURL=query.js.map