@optave/codegraph 3.1.4 → 3.1.5

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 (181) hide show
  1. package/README.md +26 -70
  2. package/package.json +10 -8
  3. package/src/ast-analysis/engine.js +32 -12
  4. package/src/ast-analysis/shared.js +2 -2
  5. package/src/cli/commands/ast.js +2 -6
  6. package/src/cli/commands/audit.js +9 -10
  7. package/src/cli/commands/batch.js +4 -4
  8. package/src/cli/commands/branch-compare.js +1 -1
  9. package/src/cli/commands/build.js +1 -1
  10. package/src/cli/commands/cfg.js +3 -7
  11. package/src/cli/commands/check.js +12 -17
  12. package/src/cli/commands/children.js +3 -6
  13. package/src/cli/commands/co-change.js +5 -3
  14. package/src/cli/commands/communities.js +2 -6
  15. package/src/cli/commands/complexity.js +3 -2
  16. package/src/cli/commands/context.js +3 -7
  17. package/src/cli/commands/cycles.js +12 -8
  18. package/src/cli/commands/dataflow.js +3 -7
  19. package/src/cli/commands/deps.js +2 -6
  20. package/src/cli/commands/diff-impact.js +2 -6
  21. package/src/cli/commands/embed.js +1 -1
  22. package/src/cli/commands/export.js +34 -31
  23. package/src/cli/commands/exports.js +2 -6
  24. package/src/cli/commands/flow.js +3 -7
  25. package/src/cli/commands/fn-impact.js +3 -7
  26. package/src/cli/commands/impact.js +2 -6
  27. package/src/cli/commands/info.js +2 -2
  28. package/src/cli/commands/map.js +1 -1
  29. package/src/cli/commands/mcp.js +1 -1
  30. package/src/cli/commands/models.js +1 -1
  31. package/src/cli/commands/owners.js +1 -1
  32. package/src/cli/commands/path.js +2 -2
  33. package/src/cli/commands/plot.js +40 -31
  34. package/src/cli/commands/query.js +3 -7
  35. package/src/cli/commands/registry.js +2 -2
  36. package/src/cli/commands/roles.js +3 -7
  37. package/src/cli/commands/search.js +1 -1
  38. package/src/cli/commands/sequence.js +3 -7
  39. package/src/cli/commands/snapshot.js +6 -1
  40. package/src/cli/commands/stats.js +1 -1
  41. package/src/cli/commands/structure.js +5 -4
  42. package/src/cli/commands/triage.js +4 -4
  43. package/src/cli/commands/watch.js +1 -1
  44. package/src/cli/commands/where.js +2 -6
  45. package/src/cli/index.js +11 -5
  46. package/src/cli/shared/open-graph.js +13 -0
  47. package/src/cli/shared/options.js +22 -2
  48. package/src/cli.js +1 -1
  49. package/src/db/connection.js +127 -4
  50. package/src/{db.js → db/index.js} +12 -5
  51. package/src/db/migrations.js +1 -1
  52. package/src/db/query-builder.js +15 -8
  53. package/src/db/repository/base.js +1 -1
  54. package/src/db/repository/graph-read.js +3 -3
  55. package/src/db/repository/in-memory-repository.js +4 -13
  56. package/src/db/repository/nodes.js +3 -8
  57. package/src/{analysis → domain/analysis}/context.js +6 -6
  58. package/src/{analysis → domain/analysis}/dependencies.js +5 -5
  59. package/src/{analysis → domain/analysis}/exports.js +8 -4
  60. package/src/{analysis → domain/analysis}/impact.js +61 -58
  61. package/src/{analysis → domain/analysis}/module-map.js +3 -3
  62. package/src/{analysis → domain/analysis}/roles.js +4 -4
  63. package/src/{analysis → domain/analysis}/symbol-lookup.js +13 -7
  64. package/src/{builder → domain/graph/builder}/helpers.js +3 -3
  65. package/src/{builder → domain/graph/builder}/incremental.js +3 -3
  66. package/src/{builder → domain/graph/builder}/pipeline.js +4 -4
  67. package/src/{builder → domain/graph/builder}/stages/build-edges.js +2 -2
  68. package/src/{builder → domain/graph/builder}/stages/build-structure.js +4 -4
  69. package/src/{builder → domain/graph/builder}/stages/collect-files.js +2 -2
  70. package/src/{builder → domain/graph/builder}/stages/detect-changes.js +6 -6
  71. package/src/{builder → domain/graph/builder}/stages/finalize.js +4 -4
  72. package/src/{builder → domain/graph/builder}/stages/insert-nodes.js +1 -1
  73. package/src/{builder → domain/graph/builder}/stages/parse-files.js +2 -2
  74. package/src/{builder → domain/graph/builder}/stages/resolve-imports.js +1 -1
  75. package/src/{builder → domain/graph/builder}/stages/run-analyses.js +2 -2
  76. package/src/{change-journal.js → domain/graph/change-journal.js} +1 -1
  77. package/src/{cycles.js → domain/graph/cycles.js} +4 -4
  78. package/src/{journal.js → domain/graph/journal.js} +1 -1
  79. package/src/{resolve.js → domain/graph/resolve.js} +2 -2
  80. package/src/{watcher.js → domain/graph/watcher.js} +5 -5
  81. package/src/{parser.js → domain/parser.js} +5 -5
  82. package/src/{queries.js → domain/queries.js} +16 -16
  83. package/src/{embeddings → domain/search}/generator.js +3 -3
  84. package/src/{embeddings → domain/search}/models.js +2 -2
  85. package/src/{embeddings → domain/search}/search/cli-formatter.js +1 -1
  86. package/src/{embeddings → domain/search}/search/hybrid.js +1 -1
  87. package/src/{embeddings → domain/search}/search/keyword.js +1 -1
  88. package/src/{embeddings → domain/search}/search/prepare.js +2 -2
  89. package/src/{embeddings → domain/search}/search/semantic.js +1 -1
  90. package/src/{embeddings → domain/search}/strategies/structured.js +1 -1
  91. package/src/extractors/javascript.js +1 -1
  92. package/src/{ast.js → features/ast.js} +8 -8
  93. package/src/{audit.js → features/audit.js} +16 -44
  94. package/src/{batch.js → features/batch.js} +5 -5
  95. package/src/{boundaries.js → features/boundaries.js} +2 -2
  96. package/src/{branch-compare.js → features/branch-compare.js} +3 -3
  97. package/src/{cfg.js → features/cfg.js} +10 -10
  98. package/src/{check.js → features/check.js} +13 -30
  99. package/src/{cochange.js → features/cochange.js} +5 -5
  100. package/src/{communities.js → features/communities.js} +7 -7
  101. package/src/{complexity.js → features/complexity.js} +13 -13
  102. package/src/{dataflow.js → features/dataflow.js} +11 -11
  103. package/src/{export.js → features/export.js} +3 -3
  104. package/src/{flow.js → features/flow.js} +4 -4
  105. package/src/{viewer.js → features/graph-enrichment.js} +6 -6
  106. package/src/{manifesto.js → features/manifesto.js} +6 -6
  107. package/src/{owners.js → features/owners.js} +2 -2
  108. package/src/{sequence.js → features/sequence.js} +15 -15
  109. package/src/{snapshot.js → features/snapshot.js} +3 -3
  110. package/src/{structure.js → features/structure.js} +7 -7
  111. package/src/{triage.js → features/triage.js} +8 -8
  112. package/src/graph/builders/dependency.js +33 -14
  113. package/src/index.cjs +16 -0
  114. package/src/index.js +39 -39
  115. package/src/{native.js → infrastructure/native.js} +1 -1
  116. package/src/mcp/middleware.js +1 -1
  117. package/src/mcp/server.js +5 -5
  118. package/src/mcp/tool-registry.js +2 -2
  119. package/src/mcp/tools/ast-query.js +1 -1
  120. package/src/mcp/tools/audit.js +1 -1
  121. package/src/mcp/tools/batch-query.js +1 -1
  122. package/src/mcp/tools/branch-compare.js +3 -1
  123. package/src/mcp/tools/cfg.js +1 -1
  124. package/src/mcp/tools/check.js +3 -3
  125. package/src/mcp/tools/co-changes.js +1 -1
  126. package/src/mcp/tools/code-owners.js +1 -1
  127. package/src/mcp/tools/communities.js +1 -1
  128. package/src/mcp/tools/complexity.js +1 -1
  129. package/src/mcp/tools/dataflow.js +2 -2
  130. package/src/mcp/tools/execution-flow.js +2 -2
  131. package/src/mcp/tools/export-graph.js +2 -2
  132. package/src/mcp/tools/find-cycles.js +2 -2
  133. package/src/mcp/tools/list-repos.js +1 -1
  134. package/src/mcp/tools/sequence.js +1 -1
  135. package/src/mcp/tools/structure.js +1 -1
  136. package/src/mcp/tools/triage.js +2 -2
  137. package/src/{commands → presentation}/audit.js +2 -2
  138. package/src/{commands → presentation}/batch.js +1 -1
  139. package/src/{commands → presentation}/branch-compare.js +2 -2
  140. package/src/{commands → presentation}/cfg.js +1 -1
  141. package/src/{commands → presentation}/check.js +2 -2
  142. package/src/{commands → presentation}/communities.js +1 -1
  143. package/src/{commands → presentation}/complexity.js +1 -1
  144. package/src/{commands → presentation}/dataflow.js +1 -1
  145. package/src/{commands → presentation}/flow.js +2 -2
  146. package/src/{commands → presentation}/manifesto.js +1 -1
  147. package/src/{commands → presentation}/owners.js +1 -1
  148. package/src/presentation/queries-cli/exports.js +46 -0
  149. package/src/presentation/queries-cli/impact.js +198 -0
  150. package/src/presentation/queries-cli/index.js +5 -0
  151. package/src/presentation/queries-cli/inspect.js +334 -0
  152. package/src/presentation/queries-cli/overview.js +197 -0
  153. package/src/presentation/queries-cli/path.js +58 -0
  154. package/src/presentation/queries-cli.js +27 -0
  155. package/src/{commands → presentation}/query.js +1 -1
  156. package/src/presentation/result-formatter.js +126 -3
  157. package/src/{commands → presentation}/sequence.js +2 -2
  158. package/src/{commands → presentation}/structure.js +1 -1
  159. package/src/{commands → presentation}/triage.js +1 -1
  160. package/src/{constants.js → shared/constants.js} +1 -1
  161. package/src/shared/file-utils.js +2 -2
  162. package/src/shared/generators.js +2 -2
  163. package/src/shared/hierarchy.js +1 -1
  164. package/src/mcp.js +0 -2
  165. package/src/queries-cli.js +0 -866
  166. /package/src/{builder → domain/graph/builder}/context.js +0 -0
  167. /package/src/{builder.js → domain/graph/builder.js} +0 -0
  168. /package/src/{embeddings → domain/search}/index.js +0 -0
  169. /package/src/{embeddings → domain/search}/search/filters.js +0 -0
  170. /package/src/{embeddings → domain/search}/stores/fts5.js +0 -0
  171. /package/src/{embeddings → domain/search}/stores/sqlite-blob.js +0 -0
  172. /package/src/{embeddings → domain/search}/strategies/source.js +0 -0
  173. /package/src/{embeddings → domain/search}/strategies/text-utils.js +0 -0
  174. /package/src/{config.js → infrastructure/config.js} +0 -0
  175. /package/src/{logger.js → infrastructure/logger.js} +0 -0
  176. /package/src/{registry.js → infrastructure/registry.js} +0 -0
  177. /package/src/{update-check.js → infrastructure/update-check.js} +0 -0
  178. /package/src/{commands → presentation}/cochange.js +0 -0
  179. /package/src/{errors.js → shared/errors.js} +0 -0
  180. /package/src/{kinds.js → shared/kinds.js} +0 -0
  181. /package/src/{paginate.js → shared/paginate.js} +0 -0
@@ -1,4 +1,4 @@
1
- import { fileDeps } from '../../queries-cli.js';
1
+ import { fileDeps } from '../../presentation/queries-cli.js';
2
2
 
3
3
  export const command = {
4
4
  name: 'deps <file>',
@@ -6,11 +6,7 @@ export const command = {
6
6
  queryOpts: true,
7
7
  execute([file], opts, ctx) {
8
8
  fileDeps(file, opts.db, {
9
- noTests: ctx.resolveNoTests(opts),
10
- json: opts.json,
11
- limit: opts.limit ? parseInt(opts.limit, 10) : undefined,
12
- offset: opts.offset ? parseInt(opts.offset, 10) : undefined,
13
- ndjson: opts.ndjson,
9
+ ...ctx.resolveQueryOpts(opts),
14
10
  });
15
11
  },
16
12
  };
@@ -1,4 +1,4 @@
1
- import { diffImpact } from '../../queries-cli.js';
1
+ import { diffImpact } from '../../presentation/queries-cli.js';
2
2
 
3
3
  export const command = {
4
4
  name: 'diff-impact [ref]',
@@ -19,12 +19,8 @@ export const command = {
19
19
  ref,
20
20
  staged: opts.staged,
21
21
  depth: parseInt(opts.depth, 10),
22
- noTests: ctx.resolveNoTests(opts),
23
- json: opts.json,
24
22
  format: opts.format,
25
- limit: opts.limit ? parseInt(opts.limit, 10) : undefined,
26
- offset: opts.offset ? parseInt(opts.offset, 10) : undefined,
27
- ndjson: opts.ndjson,
23
+ ...ctx.resolveQueryOpts(opts),
28
24
  });
29
25
  },
30
26
  };
@@ -1,5 +1,5 @@
1
1
  import path from 'node:path';
2
- import { buildEmbeddings, DEFAULT_MODEL, EMBEDDING_STRATEGIES } from '../../embeddings/index.js';
2
+ import { buildEmbeddings, DEFAULT_MODEL, EMBEDDING_STRATEGIES } from '../../domain/search/index.js';
3
3
 
4
4
  export const command = {
5
5
  name: 'embed [dir]',
@@ -1,5 +1,4 @@
1
1
  import fs from 'node:fs';
2
- import { openReadonlyOrFail } from '../../db.js';
3
2
  import {
4
3
  exportDOT,
5
4
  exportGraphML,
@@ -7,7 +6,8 @@ import {
7
6
  exportJSON,
8
7
  exportMermaid,
9
8
  exportNeo4jCSV,
10
- } from '../../export.js';
9
+ } from '../../features/export.js';
10
+ import { openGraph } from '../shared/open-graph.js';
11
11
 
12
12
  export const command = {
13
13
  name: 'export',
@@ -23,7 +23,7 @@ export const command = {
23
23
  ['-o, --output <file>', 'Write to file instead of stdout'],
24
24
  ],
25
25
  execute(_args, opts, ctx) {
26
- const db = openReadonlyOrFail(opts.db);
26
+ const { db, close } = openGraph(opts);
27
27
  const exportOpts = {
28
28
  fileLevel: !opts.functions,
29
29
  noTests: ctx.resolveNoTests(opts),
@@ -32,38 +32,41 @@ export const command = {
32
32
  };
33
33
 
34
34
  let output;
35
- switch (opts.format) {
36
- case 'mermaid':
37
- output = exportMermaid(db, exportOpts);
38
- break;
39
- case 'json':
40
- output = JSON.stringify(exportJSON(db, exportOpts), null, 2);
41
- break;
42
- case 'graphml':
43
- output = exportGraphML(db, exportOpts);
44
- break;
45
- case 'graphson':
46
- output = JSON.stringify(exportGraphSON(db, exportOpts), null, 2);
47
- break;
48
- case 'neo4j': {
49
- const csv = exportNeo4jCSV(db, exportOpts);
50
- if (opts.output) {
51
- const base = opts.output.replace(/\.[^.]+$/, '') || opts.output;
52
- fs.writeFileSync(`${base}-nodes.csv`, csv.nodes, 'utf-8');
53
- fs.writeFileSync(`${base}-relationships.csv`, csv.relationships, 'utf-8');
54
- db.close();
55
- console.log(`Exported to ${base}-nodes.csv and ${base}-relationships.csv`);
56
- return;
35
+ try {
36
+ switch (opts.format) {
37
+ case 'mermaid':
38
+ output = exportMermaid(db, exportOpts);
39
+ break;
40
+ case 'json':
41
+ output = JSON.stringify(exportJSON(db, exportOpts), null, 2);
42
+ break;
43
+ case 'graphml':
44
+ output = exportGraphML(db, exportOpts);
45
+ break;
46
+ case 'graphson':
47
+ output = JSON.stringify(exportGraphSON(db, exportOpts), null, 2);
48
+ break;
49
+ case 'neo4j': {
50
+ const csv = exportNeo4jCSV(db, exportOpts);
51
+ if (opts.output) {
52
+ const base = opts.output.replace(/\.[^.]+$/, '') || opts.output;
53
+ fs.writeFileSync(`${base}-nodes.csv`, csv.nodes, 'utf-8');
54
+ fs.writeFileSync(`${base}-relationships.csv`, csv.relationships, 'utf-8');
55
+ console.log(`Exported to ${base}-nodes.csv and ${base}-relationships.csv`);
56
+ } else {
57
+ output = `--- nodes.csv ---\n${csv.nodes}\n\n--- relationships.csv ---\n${csv.relationships}`;
58
+ }
59
+ break;
57
60
  }
58
- output = `--- nodes.csv ---\n${csv.nodes}\n\n--- relationships.csv ---\n${csv.relationships}`;
59
- break;
61
+ default:
62
+ output = exportDOT(db, exportOpts);
63
+ break;
60
64
  }
61
- default:
62
- output = exportDOT(db, exportOpts);
63
- break;
65
+ } finally {
66
+ close();
64
67
  }
65
68
 
66
- db.close();
69
+ if (output === undefined) return;
67
70
 
68
71
  if (opts.output) {
69
72
  fs.writeFileSync(opts.output, output, 'utf-8');
@@ -1,4 +1,4 @@
1
- import { fileExports } from '../../queries-cli.js';
1
+ import { fileExports } from '../../presentation/queries-cli.js';
2
2
 
3
3
  export const command = {
4
4
  name: 'exports <file>',
@@ -7,12 +7,8 @@ export const command = {
7
7
  options: [['--unused', 'Show only exports with zero consumers (dead exports)']],
8
8
  execute([file], opts, ctx) {
9
9
  fileExports(file, opts.db, {
10
- noTests: ctx.resolveNoTests(opts),
11
- json: opts.json,
12
10
  unused: opts.unused || false,
13
- limit: opts.limit ? parseInt(opts.limit, 10) : undefined,
14
- offset: opts.offset ? parseInt(opts.offset, 10) : undefined,
15
- ndjson: opts.ndjson,
11
+ ...ctx.resolveQueryOpts(opts),
16
12
  });
17
13
  },
18
14
  };
@@ -1,4 +1,4 @@
1
- import { EVERY_SYMBOL_KIND } from '../../queries.js';
1
+ import { EVERY_SYMBOL_KIND } from '../../domain/queries.js';
2
2
 
3
3
  export const command = {
4
4
  name: 'flow [name]',
@@ -20,17 +20,13 @@ export const command = {
20
20
  }
21
21
  },
22
22
  async execute([name], opts, ctx) {
23
- const { flow } = await import('../../commands/flow.js');
23
+ const { flow } = await import('../../presentation/flow.js');
24
24
  flow(name, opts.db, {
25
25
  list: opts.list,
26
26
  depth: parseInt(opts.depth, 10),
27
27
  file: opts.file,
28
28
  kind: opts.kind,
29
- noTests: ctx.resolveNoTests(opts),
30
- json: opts.json,
31
- limit: opts.limit ? parseInt(opts.limit, 10) : undefined,
32
- offset: opts.offset ? parseInt(opts.offset, 10) : undefined,
33
- ndjson: opts.ndjson,
29
+ ...ctx.resolveQueryOpts(opts),
34
30
  });
35
31
  },
36
32
  };
@@ -1,5 +1,5 @@
1
- import { EVERY_SYMBOL_KIND } from '../../queries.js';
2
- import { fnImpact } from '../../queries-cli.js';
1
+ import { EVERY_SYMBOL_KIND } from '../../domain/queries.js';
2
+ import { fnImpact } from '../../presentation/queries-cli.js';
3
3
 
4
4
  export const command = {
5
5
  name: 'fn-impact <name>',
@@ -20,11 +20,7 @@ export const command = {
20
20
  depth: parseInt(opts.depth, 10),
21
21
  file: opts.file,
22
22
  kind: opts.kind,
23
- noTests: ctx.resolveNoTests(opts),
24
- json: opts.json,
25
- limit: opts.limit ? parseInt(opts.limit, 10) : undefined,
26
- offset: opts.offset ? parseInt(opts.offset, 10) : undefined,
27
- ndjson: opts.ndjson,
23
+ ...ctx.resolveQueryOpts(opts),
28
24
  });
29
25
  },
30
26
  };
@@ -1,4 +1,4 @@
1
- import { impactAnalysis } from '../../queries-cli.js';
1
+ import { impactAnalysis } from '../../presentation/queries-cli.js';
2
2
 
3
3
  export const command = {
4
4
  name: 'impact <file>',
@@ -6,11 +6,7 @@ export const command = {
6
6
  queryOpts: true,
7
7
  execute([file], opts, ctx) {
8
8
  impactAnalysis(file, opts.db, {
9
- noTests: ctx.resolveNoTests(opts),
10
- json: opts.json,
11
- limit: opts.limit ? parseInt(opts.limit, 10) : undefined,
12
- offset: opts.offset ? parseInt(opts.offset, 10) : undefined,
13
- ndjson: opts.ndjson,
9
+ ...ctx.resolveQueryOpts(opts),
14
10
  });
15
11
  },
16
12
  };
@@ -3,9 +3,9 @@ export const command = {
3
3
  description: 'Show codegraph engine info and diagnostics',
4
4
  async execute(_args, _opts, ctx) {
5
5
  const { getNativePackageVersion, isNativeAvailable, loadNative } = await import(
6
- '../../native.js'
6
+ '../../infrastructure/native.js'
7
7
  );
8
- const { getActiveEngine } = await import('../../parser.js');
8
+ const { getActiveEngine } = await import('../../domain/parser.js');
9
9
 
10
10
  const engine = ctx.program.opts().engine;
11
11
  const { name: activeName, version: activeVersion } = getActiveEngine({ engine });
@@ -1,4 +1,4 @@
1
- import { moduleMap } from '../../queries-cli.js';
1
+ import { moduleMap } from '../../presentation/queries-cli.js';
2
2
 
3
3
  export const command = {
4
4
  name: 'map',
@@ -7,7 +7,7 @@ export const command = {
7
7
  ['--repos <names>', 'Comma-separated list of allowed repo names (restricts access)'],
8
8
  ],
9
9
  async execute(_args, opts) {
10
- const { startMCPServer } = await import('../../mcp.js');
10
+ const { startMCPServer } = await import('../../mcp/index.js');
11
11
  const mcpOpts = {};
12
12
  mcpOpts.multiRepo = opts.multiRepo || !!opts.repos;
13
13
  if (opts.repos) {
@@ -1,4 +1,4 @@
1
- import { DEFAULT_MODEL, MODELS } from '../../embeddings/index.js';
1
+ import { DEFAULT_MODEL, MODELS } from '../../domain/search/index.js';
2
2
 
3
3
  export const command = {
4
4
  name: 'models',
@@ -12,7 +12,7 @@ export const command = {
12
12
  ['-j, --json', 'Output as JSON'],
13
13
  ],
14
14
  async execute([target], opts, ctx) {
15
- const { owners } = await import('../../commands/owners.js');
15
+ const { owners } = await import('../../presentation/owners.js');
16
16
  owners(opts.db, {
17
17
  owner: opts.owner,
18
18
  boundary: opts.boundary,
@@ -1,5 +1,5 @@
1
- import { EVERY_SYMBOL_KIND } from '../../queries.js';
2
- import { symbolPath } from '../../queries-cli.js';
1
+ import { EVERY_SYMBOL_KIND } from '../../domain/queries.js';
2
+ import { symbolPath } from '../../presentation/queries-cli.js';
3
3
 
4
4
  export const command = {
5
5
  name: 'path <from> <to>',
@@ -1,6 +1,6 @@
1
1
  import fs from 'node:fs';
2
2
  import path from 'node:path';
3
- import { openReadonlyOrFail } from '../../db.js';
3
+ import { openGraph } from '../shared/open-graph.js';
4
4
 
5
5
  export const command = {
6
6
  name: 'plot',
@@ -22,43 +22,52 @@ export const command = {
22
22
  ['--color-by <mode>', 'Color nodes by: kind | role | community | complexity'],
23
23
  ],
24
24
  async execute(_args, opts, ctx) {
25
- const { generatePlotHTML, loadPlotConfig } = await import('../../viewer.js');
25
+ const { generatePlotHTML, loadPlotConfig } = await import('../../features/graph-enrichment.js');
26
26
  const os = await import('node:os');
27
- const db = openReadonlyOrFail(opts.db);
27
+ const { db, close } = openGraph(opts);
28
28
 
29
29
  let plotCfg;
30
- if (opts.config) {
31
- try {
32
- plotCfg = JSON.parse(fs.readFileSync(opts.config, 'utf-8'));
33
- } catch (e) {
34
- console.error(`Failed to load config: ${e.message}`);
35
- db.close();
36
- process.exitCode = 1;
37
- return;
30
+ let html;
31
+ try {
32
+ if (opts.config) {
33
+ try {
34
+ plotCfg = JSON.parse(fs.readFileSync(opts.config, 'utf-8'));
35
+ } catch (e) {
36
+ console.error(`Failed to load config: ${e.message}`);
37
+ process.exitCode = 1;
38
+ return;
39
+ }
40
+ } else {
41
+ plotCfg = loadPlotConfig(process.cwd());
42
+ }
43
+
44
+ if (opts.cluster) plotCfg.clusterBy = opts.cluster;
45
+ if (opts.colorBy) plotCfg.colorBy = opts.colorBy;
46
+ if (opts.sizeBy) plotCfg.sizeBy = opts.sizeBy;
47
+ if (opts.seed) plotCfg.seedStrategy = opts.seed;
48
+ if (opts.seedCount) plotCfg.seedCount = parseInt(opts.seedCount, 10);
49
+ if (opts.overlay) {
50
+ const parts = opts.overlay.split(',').map((s) => s.trim());
51
+ if (!plotCfg.overlays) plotCfg.overlays = {};
52
+ if (parts.includes('complexity')) plotCfg.overlays.complexity = true;
53
+ if (parts.includes('risk')) plotCfg.overlays.risk = true;
38
54
  }
39
- } else {
40
- plotCfg = loadPlotConfig(process.cwd());
41
- }
42
55
 
43
- if (opts.cluster) plotCfg.clusterBy = opts.cluster;
44
- if (opts.colorBy) plotCfg.colorBy = opts.colorBy;
45
- if (opts.sizeBy) plotCfg.sizeBy = opts.sizeBy;
46
- if (opts.seed) plotCfg.seedStrategy = opts.seed;
47
- if (opts.seedCount) plotCfg.seedCount = parseInt(opts.seedCount, 10);
48
- if (opts.overlay) {
49
- const parts = opts.overlay.split(',').map((s) => s.trim());
50
- if (!plotCfg.overlays) plotCfg.overlays = {};
51
- if (parts.includes('complexity')) plotCfg.overlays.complexity = true;
52
- if (parts.includes('risk')) plotCfg.overlays.risk = true;
56
+ html = generatePlotHTML(db, {
57
+ fileLevel: !opts.functions,
58
+ noTests: ctx.resolveNoTests(opts),
59
+ minConfidence: parseFloat(opts.minConfidence),
60
+ config: plotCfg,
61
+ });
62
+ } finally {
63
+ close();
53
64
  }
54
65
 
55
- const html = generatePlotHTML(db, {
56
- fileLevel: !opts.functions,
57
- noTests: ctx.resolveNoTests(opts),
58
- minConfidence: parseFloat(opts.minConfidence),
59
- config: plotCfg,
60
- });
61
- db.close();
66
+ if (!html) {
67
+ console.error('generatePlotHTML returned no output');
68
+ process.exitCode = 1;
69
+ return;
70
+ }
62
71
 
63
72
  const outPath = opts.output || path.join(os.tmpdir(), `codegraph-plot-${Date.now()}.html`);
64
73
  fs.writeFileSync(outPath, html, 'utf-8');
@@ -1,5 +1,5 @@
1
- import { EVERY_SYMBOL_KIND } from '../../queries.js';
2
- import { fnDeps, symbolPath } from '../../queries-cli.js';
1
+ import { EVERY_SYMBOL_KIND } from '../../domain/queries.js';
2
+ import { fnDeps, symbolPath } from '../../presentation/queries-cli.js';
3
3
 
4
4
  export const command = {
5
5
  name: 'query <name>',
@@ -38,11 +38,7 @@ export const command = {
38
38
  depth: parseInt(opts.depth, 10),
39
39
  file: opts.file,
40
40
  kind: opts.kind,
41
- noTests: ctx.resolveNoTests(opts),
42
- json: opts.json,
43
- limit: opts.limit ? parseInt(opts.limit, 10) : undefined,
44
- offset: opts.offset ? parseInt(opts.offset, 10) : undefined,
45
- ndjson: opts.ndjson,
41
+ ...ctx.resolveQueryOpts(opts),
46
42
  });
47
43
  }
48
44
  },
@@ -1,13 +1,13 @@
1
1
  import fs from 'node:fs';
2
2
  import path from 'node:path';
3
- import { ConfigError } from '../../errors.js';
4
3
  import {
5
4
  listRepos,
6
5
  pruneRegistry,
7
6
  REGISTRY_PATH,
8
7
  registerRepo,
9
8
  unregisterRepo,
10
- } from '../../registry.js';
9
+ } from '../../infrastructure/registry.js';
10
+ import { ConfigError } from '../../shared/errors.js';
11
11
 
12
12
  export const command = {
13
13
  name: 'registry',
@@ -1,5 +1,5 @@
1
- import { VALID_ROLES } from '../../queries.js';
2
- import { roles } from '../../queries-cli.js';
1
+ import { VALID_ROLES } from '../../domain/queries.js';
2
+ import { roles } from '../../presentation/queries-cli.js';
3
3
 
4
4
  export const command = {
5
5
  name: 'roles',
@@ -24,11 +24,7 @@ export const command = {
24
24
  roles(opts.db, {
25
25
  role: opts.role,
26
26
  file: opts.file,
27
- noTests: ctx.resolveNoTests(opts),
28
- json: opts.json,
29
- limit: opts.limit ? parseInt(opts.limit, 10) : undefined,
30
- offset: opts.offset ? parseInt(opts.offset, 10) : undefined,
31
- ndjson: opts.ndjson,
27
+ ...ctx.resolveQueryOpts(opts),
32
28
  });
33
29
  },
34
30
  };
@@ -1,4 +1,4 @@
1
- import { search } from '../../embeddings/index.js';
1
+ import { search } from '../../domain/search/index.js';
2
2
 
3
3
  export const command = {
4
4
  name: 'search <query>',
@@ -1,4 +1,4 @@
1
- import { EVERY_SYMBOL_KIND } from '../../queries.js';
1
+ import { EVERY_SYMBOL_KIND } from '../../domain/queries.js';
2
2
 
3
3
  export const command = {
4
4
  name: 'sequence <name>',
@@ -16,17 +16,13 @@ export const command = {
16
16
  }
17
17
  },
18
18
  async execute([name], opts, ctx) {
19
- const { sequence } = await import('../../commands/sequence.js');
19
+ const { sequence } = await import('../../presentation/sequence.js');
20
20
  sequence(name, opts.db, {
21
21
  depth: parseInt(opts.depth, 10),
22
22
  file: opts.file,
23
23
  kind: opts.kind,
24
- noTests: ctx.resolveNoTests(opts),
25
- json: opts.json,
26
24
  dataflow: opts.dataflow,
27
- limit: opts.limit ? parseInt(opts.limit, 10) : undefined,
28
- offset: opts.offset ? parseInt(opts.offset, 10) : undefined,
29
- ndjson: opts.ndjson,
25
+ ...ctx.resolveQueryOpts(opts),
30
26
  });
31
27
  },
32
28
  };
@@ -1,4 +1,9 @@
1
- import { snapshotDelete, snapshotList, snapshotRestore, snapshotSave } from '../../snapshot.js';
1
+ import {
2
+ snapshotDelete,
3
+ snapshotList,
4
+ snapshotRestore,
5
+ snapshotSave,
6
+ } from '../../features/snapshot.js';
2
7
 
3
8
  export const command = {
4
9
  name: 'snapshot',
@@ -1,4 +1,4 @@
1
- import { stats } from '../../queries-cli.js';
1
+ import { stats } from '../../presentation/queries-cli.js';
2
2
 
3
3
  export const command = {
4
4
  name: 'stats',
@@ -15,15 +15,16 @@ export const command = {
15
15
  ['--ndjson', 'Newline-delimited JSON output'],
16
16
  ],
17
17
  async execute([dir], opts, ctx) {
18
- const { structureData, formatStructure } = await import('../../commands/structure.js');
18
+ const { structureData, formatStructure } = await import('../../presentation/structure.js');
19
+ const qOpts = ctx.resolveQueryOpts(opts);
19
20
  const data = structureData(opts.db, {
20
21
  directory: dir,
21
22
  depth: opts.depth ? parseInt(opts.depth, 10) : undefined,
22
23
  sort: opts.sort,
23
24
  full: opts.full,
24
- noTests: ctx.resolveNoTests(opts),
25
- limit: opts.limit ? parseInt(opts.limit, 10) : undefined,
26
- offset: opts.offset ? parseInt(opts.offset, 10) : undefined,
25
+ noTests: qOpts.noTests,
26
+ limit: qOpts.limit,
27
+ offset: qOpts.offset,
27
28
  });
28
29
  if (!ctx.outputResult(data, 'directories', opts)) {
29
30
  console.log(formatStructure(data));
@@ -1,5 +1,5 @@
1
- import { ConfigError } from '../../errors.js';
2
- import { EVERY_SYMBOL_KIND, VALID_ROLES } from '../../queries.js';
1
+ import { EVERY_SYMBOL_KIND, VALID_ROLES } from '../../domain/queries.js';
2
+ import { ConfigError } from '../../shared/errors.js';
3
3
 
4
4
  export const command = {
5
5
  name: 'triage',
@@ -31,7 +31,7 @@ export const command = {
31
31
  ],
32
32
  async execute(_args, opts, ctx) {
33
33
  if (opts.level === 'file' || opts.level === 'directory') {
34
- const { hotspotsData, formatHotspots } = await import('../../commands/structure.js');
34
+ const { hotspotsData, formatHotspots } = await import('../../presentation/structure.js');
35
35
  const metric = opts.sort === 'risk' ? 'fan-in' : opts.sort;
36
36
  const data = hotspotsData(opts.db, {
37
37
  metric,
@@ -60,7 +60,7 @@ export const command = {
60
60
  throw new ConfigError('Invalid --weights JSON', { cause: err });
61
61
  }
62
62
  }
63
- const { triage } = await import('../../commands/triage.js');
63
+ const { triage } = await import('../../presentation/triage.js');
64
64
  triage(opts.db, {
65
65
  limit: parseInt(opts.limit, 10),
66
66
  offset: opts.offset ? parseInt(opts.offset, 10) : undefined,
@@ -1,5 +1,5 @@
1
1
  import path from 'node:path';
2
- import { watchProject } from '../../watcher.js';
2
+ import { watchProject } from '../../domain/graph/watcher.js';
3
3
 
4
4
  export const command = {
5
5
  name: 'watch [dir]',
@@ -1,4 +1,4 @@
1
- import { where } from '../../queries-cli.js';
1
+ import { where } from '../../presentation/queries-cli.js';
2
2
 
3
3
  export const command = {
4
4
  name: 'where [name]',
@@ -14,11 +14,7 @@ export const command = {
14
14
  const target = opts.file || name;
15
15
  where(target, opts.db, {
16
16
  file: !!opts.file,
17
- noTests: ctx.resolveNoTests(opts),
18
- json: opts.json,
19
- limit: opts.limit ? parseInt(opts.limit, 10) : undefined,
20
- offset: opts.offset ? parseInt(opts.offset, 10) : undefined,
21
- ndjson: opts.ndjson,
17
+ ...ctx.resolveQueryOpts(opts),
22
18
  });
23
19
  },
24
20
  };
package/src/cli/index.js CHANGED
@@ -2,10 +2,16 @@ import fs from 'node:fs';
2
2
  import path from 'node:path';
3
3
  import { pathToFileURL } from 'node:url';
4
4
  import { Command } from 'commander';
5
- import { ConfigError } from '../errors.js';
6
- import { setVerbose } from '../logger.js';
7
- import { checkForUpdates, printUpdateNotification } from '../update-check.js';
8
- import { applyQueryOpts, config, formatSize, resolveNoTests } from './shared/options.js';
5
+ import { setVerbose } from '../infrastructure/logger.js';
6
+ import { checkForUpdates, printUpdateNotification } from '../infrastructure/update-check.js';
7
+ import { ConfigError } from '../shared/errors.js';
8
+ import {
9
+ applyQueryOpts,
10
+ config,
11
+ formatSize,
12
+ resolveNoTests,
13
+ resolveQueryOpts,
14
+ } from './shared/options.js';
9
15
  import { outputResult } from './shared/output.js';
10
16
 
11
17
  const __cliDir = path.dirname(new URL(import.meta.url).pathname.replace(/^\/([A-Z]:)/i, '$1'));
@@ -35,7 +41,7 @@ program
35
41
  });
36
42
 
37
43
  /** Shared context passed to every command's execute(). */
38
- const ctx = { config, resolveNoTests, formatSize, outputResult, program };
44
+ const ctx = { config, resolveNoTests, resolveQueryOpts, formatSize, outputResult, program };
39
45
 
40
46
  /**
41
47
  * Register a command definition onto a Commander parent.
@@ -0,0 +1,13 @@
1
+ import { openReadonlyOrFail } from '../../db/index.js';
2
+
3
+ /**
4
+ * Open the graph database in readonly mode with a clean close() handle.
5
+ *
6
+ * @param {object} [opts]
7
+ * @param {string} [opts.db] - Custom path to graph.db
8
+ * @returns {{ db: import('better-sqlite3').Database, close: () => void }}
9
+ */
10
+ export function openGraph(opts = {}) {
11
+ const db = openReadonlyOrFail(opts.db);
12
+ return { db, close: () => db.close() };
13
+ }