monomind 1.17.0 → 1.17.1

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 (91) hide show
  1. package/.claude/agents/engineering/engineering-security-engineer.md +1 -1
  2. package/.claude/commands/mastermind/_repeat.md +4 -0
  3. package/.claude/commands/mastermind/master.md +52 -1
  4. package/.claude/scheduled_tasks.lock +1 -1
  5. package/.claude/skills/mastermind/_repeat.md +2 -0
  6. package/package.json +1 -1
  7. package/packages/@monomind/cli/.claude/agents/engineering/engineering-security-engineer.md +1 -1
  8. package/packages/@monomind/cli/.claude/commands/mastermind/_repeat.md +4 -0
  9. package/packages/@monomind/cli/.claude/commands/mastermind/master.md +52 -1
  10. package/packages/@monomind/cli/.claude/skills/mastermind/_repeat.md +2 -0
  11. package/packages/@monomind/cli/dist/src/__tests__/browse-analyzer.test.js +42 -59
  12. package/packages/@monomind/cli/dist/src/browser/dashboard/server.js +18 -0
  13. package/packages/@monomind/cli/dist/src/browser/dashboard/ui.html +37 -125
  14. package/packages/@monomind/cli/dist/src/commands/agent-lifecycle.d.ts +17 -0
  15. package/packages/@monomind/cli/dist/src/commands/agent-lifecycle.js +320 -0
  16. package/packages/@monomind/cli/dist/src/commands/agent-ops.d.ts +9 -0
  17. package/packages/@monomind/cli/dist/src/commands/agent-ops.js +329 -0
  18. package/packages/@monomind/cli/dist/src/commands/agent.js +5 -907
  19. package/packages/@monomind/cli/dist/src/commands/analyze-ast.d.ts +26 -0
  20. package/packages/@monomind/cli/dist/src/commands/analyze-ast.js +284 -0
  21. package/packages/@monomind/cli/dist/src/commands/analyze-boundaries.d.ts +14 -0
  22. package/packages/@monomind/cli/dist/src/commands/analyze-boundaries.js +295 -0
  23. package/packages/@monomind/cli/dist/src/commands/analyze-diff.d.ts +8 -0
  24. package/packages/@monomind/cli/dist/src/commands/analyze-diff.js +395 -0
  25. package/packages/@monomind/cli/dist/src/commands/analyze-graph.d.ts +14 -0
  26. package/packages/@monomind/cli/dist/src/commands/analyze-graph.js +304 -0
  27. package/packages/@monomind/cli/dist/src/commands/analyze-imports.d.ts +11 -0
  28. package/packages/@monomind/cli/dist/src/commands/analyze-imports.js +287 -0
  29. package/packages/@monomind/cli/dist/src/commands/analyze-symbols.d.ts +14 -0
  30. package/packages/@monomind/cli/dist/src/commands/analyze-symbols.js +302 -0
  31. package/packages/@monomind/cli/dist/src/commands/analyze.d.ts +38 -0
  32. package/packages/@monomind/cli/dist/src/commands/analyze.js +12 -1827
  33. package/packages/@monomind/cli/dist/src/commands/doctor-env-checks.d.ts +26 -0
  34. package/packages/@monomind/cli/dist/src/commands/doctor-env-checks.js +189 -0
  35. package/packages/@monomind/cli/dist/src/commands/doctor-project-checks.d.ts +19 -0
  36. package/packages/@monomind/cli/dist/src/commands/doctor-project-checks.js +388 -0
  37. package/packages/@monomind/cli/dist/src/commands/doctor.js +51 -942
  38. package/packages/@monomind/cli/dist/src/commands/hive-mind-comms.d.ts +11 -0
  39. package/packages/@monomind/cli/dist/src/commands/hive-mind-comms.js +242 -0
  40. package/packages/@monomind/cli/dist/src/commands/hive-mind-helpers.d.ts +35 -0
  41. package/packages/@monomind/cli/dist/src/commands/hive-mind-helpers.js +203 -0
  42. package/packages/@monomind/cli/dist/src/commands/hive-mind-ops.d.ts +8 -0
  43. package/packages/@monomind/cli/dist/src/commands/hive-mind-ops.js +233 -0
  44. package/packages/@monomind/cli/dist/src/commands/hive-mind-spawn.d.ts +12 -0
  45. package/packages/@monomind/cli/dist/src/commands/hive-mind-spawn.js +274 -0
  46. package/packages/@monomind/cli/dist/src/commands/hive-mind.js +10 -1129
  47. package/packages/@monomind/cli/dist/src/commands/hooks-coverage-commands.d.ts +4 -4
  48. package/packages/@monomind/cli/dist/src/commands/hooks-coverage-commands.js +19 -819
  49. package/packages/@monomind/cli/dist/src/commands/hooks-coverage-gaps.d.ts +7 -0
  50. package/packages/@monomind/cli/dist/src/commands/hooks-coverage-gaps.js +334 -0
  51. package/packages/@monomind/cli/dist/src/commands/hooks-coverage-routing.d.ts +7 -0
  52. package/packages/@monomind/cli/dist/src/commands/hooks-coverage-routing.js +399 -0
  53. package/packages/@monomind/cli/dist/src/commands/init-subcommands.d.ts +8 -0
  54. package/packages/@monomind/cli/dist/src/commands/init-subcommands.js +156 -0
  55. package/packages/@monomind/cli/dist/src/commands/init-upgrade.d.ts +6 -0
  56. package/packages/@monomind/cli/dist/src/commands/init-upgrade.js +203 -0
  57. package/packages/@monomind/cli/dist/src/commands/init-wizard.d.ts +6 -0
  58. package/packages/@monomind/cli/dist/src/commands/init-wizard.js +246 -0
  59. package/packages/@monomind/cli/dist/src/commands/init.js +6 -623
  60. package/packages/@monomind/cli/dist/src/commands/memory-admin.d.ts +10 -0
  61. package/packages/@monomind/cli/dist/src/commands/memory-admin.js +433 -0
  62. package/packages/@monomind/cli/dist/src/commands/memory-crud.d.ts +9 -0
  63. package/packages/@monomind/cli/dist/src/commands/memory-crud.js +342 -0
  64. package/packages/@monomind/cli/dist/src/commands/memory-list.d.ts +10 -0
  65. package/packages/@monomind/cli/dist/src/commands/memory-list.js +321 -0
  66. package/packages/@monomind/cli/dist/src/commands/memory-transfer.d.ts +9 -0
  67. package/packages/@monomind/cli/dist/src/commands/memory-transfer.js +372 -0
  68. package/packages/@monomind/cli/dist/src/commands/memory.d.ts +6 -0
  69. package/packages/@monomind/cli/dist/src/commands/memory.js +10 -1441
  70. package/packages/@monomind/cli/dist/src/commands/neural-core.d.ts +8 -0
  71. package/packages/@monomind/cli/dist/src/commands/neural-core.js +274 -0
  72. package/packages/@monomind/cli/dist/src/commands/neural-optimize.d.ts +7 -0
  73. package/packages/@monomind/cli/dist/src/commands/neural-optimize.js +332 -0
  74. package/packages/@monomind/cli/dist/src/commands/neural-registry.d.ts +7 -0
  75. package/packages/@monomind/cli/dist/src/commands/neural-registry.js +290 -0
  76. package/packages/@monomind/cli/dist/src/commands/neural.js +3 -974
  77. package/packages/@monomind/cli/dist/src/commands/platforms.js +327 -7
  78. package/packages/@monomind/cli/dist/src/commands/security-cve.d.ts +6 -0
  79. package/packages/@monomind/cli/dist/src/commands/security-cve.js +310 -0
  80. package/packages/@monomind/cli/dist/src/commands/security-misc.d.ts +9 -0
  81. package/packages/@monomind/cli/dist/src/commands/security-misc.js +293 -0
  82. package/packages/@monomind/cli/dist/src/commands/security-scan.d.ts +18 -0
  83. package/packages/@monomind/cli/dist/src/commands/security-scan.js +328 -0
  84. package/packages/@monomind/cli/dist/src/commands/security.js +3 -958
  85. package/packages/@monomind/cli/dist/src/commands/session.js +1 -1
  86. package/packages/@monomind/cli/dist/src/commands/swarm.js +23 -17
  87. package/packages/@monomind/cli/dist/src/mcp-tools/swarm-tools.js +77 -0
  88. package/packages/@monomind/cli/dist/src/parser.js +11 -6
  89. package/packages/@monomind/cli/dist/src/routing/llm-caller.js +1 -2
  90. package/packages/@monomind/cli/package.json +2 -3
  91. package/packages/@monomind/cli/scripts/understand-analyze.mjs +1 -1
@@ -14,18 +14,21 @@
14
14
  * github.com/monoes/monomind
15
15
  */
16
16
  import { output } from '../output.js';
17
- import { callMCPTool, MCPClientError } from '../mcp-client.js';
18
17
  import * as path from 'path';
19
18
  import * as fs from 'fs/promises';
20
19
  import { writeFile } from 'fs/promises';
21
- import { resolve } from 'path';
22
- import { execSync } from 'child_process';
20
+ import { diffCommand, codeCommand } from './analyze-diff.js';
21
+ import { astCommand } from './analyze-ast.js';
22
+ import { complexityAstCommand, symbolsCommand } from './analyze-symbols.js';
23
+ import { importsCommand, depsCommand } from './analyze-imports.js';
24
+ import { boundariesCommand, modulesCommand } from './analyze-boundaries.js';
25
+ import { dependenciesCommand, circularCommand } from './analyze-graph.js';
23
26
  // AST analyzer module was never shipped — always falls back to the regex path.
24
- async function getASTAnalyzer() {
27
+ export async function getASTAnalyzer() {
25
28
  return null;
26
29
  }
27
30
  // Graph analyzer module was never shipped — callers handle the null path.
28
- async function getGraphAnalyzer() {
31
+ export async function getGraphAnalyzer() {
29
32
  return null;
30
33
  }
31
34
  /**
@@ -33,7 +36,7 @@ async function getGraphAnalyzer() {
33
36
  * directory to prevent path traversal attacks via --output /etc/cron.d/x or
34
37
  * similar. Throws if the resolved path escapes cwd.
35
38
  */
36
- async function safeWriteOutputFile(outputFile, data) {
39
+ export async function safeWriteOutputFile(outputFile, data) {
37
40
  const projectRoot = path.resolve(process.cwd());
38
41
  const fullPath = path.resolve(process.cwd(), outputFile);
39
42
  if (!fullPath.startsWith(projectRoot + path.sep) && fullPath !== projectRoot) {
@@ -41,1079 +44,10 @@ async function safeWriteOutputFile(outputFile, data) {
41
44
  }
42
45
  await writeFile(fullPath, data);
43
46
  }
44
- // Diff subcommand
45
- const diffCommand = {
46
- name: 'diff',
47
- description: 'Analyze git diff for change risk assessment and classification',
48
- options: [
49
- {
50
- name: 'risk',
51
- short: 'r',
52
- description: 'Show risk assessment',
53
- type: 'boolean',
54
- default: false,
55
- },
56
- {
57
- name: 'classify',
58
- short: 'c',
59
- description: 'Classify change type',
60
- type: 'boolean',
61
- default: false,
62
- },
63
- {
64
- name: 'reviewers',
65
- description: 'Show recommended reviewers',
66
- type: 'boolean',
67
- default: false,
68
- },
69
- {
70
- name: 'format',
71
- short: 'f',
72
- description: 'Output format: text, json, table',
73
- type: 'string',
74
- default: 'text',
75
- choices: ['text', 'json', 'table'],
76
- },
77
- {
78
- name: 'verbose',
79
- short: 'v',
80
- description: 'Show detailed file-level analysis',
81
- type: 'boolean',
82
- default: false,
83
- },
84
- ],
85
- examples: [
86
- { command: 'monomind analyze diff --risk', description: 'Analyze current diff with risk assessment' },
87
- { command: 'monomind analyze diff HEAD~1 --classify', description: 'Classify changes from last commit' },
88
- { command: 'monomind analyze diff main..feature --format json', description: 'Compare branches with JSON output' },
89
- { command: 'monomind analyze diff --reviewers', description: 'Get recommended reviewers for changes' },
90
- ],
91
- action: async (ctx) => {
92
- const ref = ctx.args[0] || 'HEAD';
93
- const showRisk = ctx.flags.risk;
94
- const showClassify = ctx.flags.classify;
95
- const showReviewers = ctx.flags.reviewers;
96
- const formatType = ctx.flags.format || 'text';
97
- const verbose = ctx.flags.verbose;
98
- // If no specific flag, show all
99
- const showAll = !showRisk && !showClassify && !showReviewers;
100
- output.printInfo(`Analyzing diff: ${output.highlight(ref)}`);
101
- try {
102
- // Call MCP tool for diff analysis
103
- const result = await callMCPTool('analyze_diff', {
104
- ref,
105
- includeFileRisks: verbose,
106
- includeReviewers: showReviewers || showAll,
107
- });
108
- // JSON output
109
- if (formatType === 'json') {
110
- output.printJson(result);
111
- return { success: true, data: result };
112
- }
113
- output.writeln();
114
- // Summary box
115
- const files = result.files || [];
116
- const risk = result.risk || { overall: 'unknown', score: 0, breakdown: { fileCount: 0, totalChanges: 0, highRiskFiles: [], securityConcerns: [], breakingChanges: [], testCoverage: 'unknown' } };
117
- const classification = result.classification || { category: 'unknown', confidence: 0, reasoning: '' };
118
- output.printBox([
119
- `Ref: ${result.ref || 'HEAD'}`,
120
- `Files: ${files.length}`,
121
- `Risk: ${getRiskDisplay(risk.overall)} (${risk.score}/100)`,
122
- `Type: ${classification.category}${classification.subcategory ? ` (${classification.subcategory})` : ''}`,
123
- ``,
124
- result.summary || 'No summary available',
125
- ].join('\n'), 'Diff Analysis');
126
- // Risk assessment
127
- if (showRisk || showAll) {
128
- output.writeln();
129
- output.writeln(output.bold('Risk Assessment'));
130
- output.writeln(output.dim('-'.repeat(50)));
131
- output.printTable({
132
- columns: [
133
- { key: 'metric', header: 'Metric', width: 25 },
134
- { key: 'value', header: 'Value', width: 30 },
135
- ],
136
- data: [
137
- { metric: 'Overall Risk', value: getRiskDisplay(risk.overall) },
138
- { metric: 'Risk Score', value: `${risk.score}/100` },
139
- { metric: 'Files Changed', value: risk.breakdown.fileCount },
140
- { metric: 'Total Lines Changed', value: risk.breakdown.totalChanges },
141
- { metric: 'Test Coverage', value: risk.breakdown.testCoverage },
142
- ],
143
- });
144
- // Security concerns
145
- if (risk.breakdown.securityConcerns.length > 0) {
146
- output.writeln();
147
- output.writeln(output.bold(output.warning('Security Concerns')));
148
- output.printList(risk.breakdown.securityConcerns.map(c => output.warning(c)));
149
- }
150
- // Breaking changes
151
- if (risk.breakdown.breakingChanges.length > 0) {
152
- output.writeln();
153
- output.writeln(output.bold(output.error('Potential Breaking Changes')));
154
- output.printList(risk.breakdown.breakingChanges.map(c => output.error(c)));
155
- }
156
- // High risk files
157
- if (risk.breakdown.highRiskFiles.length > 0) {
158
- output.writeln();
159
- output.writeln(output.bold('High Risk Files'));
160
- output.printList(risk.breakdown.highRiskFiles.map(f => output.warning(f)));
161
- }
162
- }
163
- // Classification
164
- if (showClassify || showAll) {
165
- output.writeln();
166
- output.writeln(output.bold('Classification'));
167
- output.writeln(output.dim('-'.repeat(50)));
168
- output.printTable({
169
- columns: [
170
- { key: 'field', header: 'Field', width: 15 },
171
- { key: 'value', header: 'Value', width: 40 },
172
- ],
173
- data: [
174
- { field: 'Category', value: classification.category },
175
- { field: 'Subcategory', value: classification.subcategory || '-' },
176
- { field: 'Confidence', value: `${(classification.confidence * 100).toFixed(0)}%` },
177
- ],
178
- });
179
- output.writeln();
180
- output.writeln(output.dim(`Reasoning: ${classification.reasoning}`));
181
- }
182
- // Reviewers
183
- if (showReviewers || showAll) {
184
- output.writeln();
185
- output.writeln(output.bold('Recommended Reviewers'));
186
- output.writeln(output.dim('-'.repeat(50)));
187
- const reviewers = result.recommendedReviewers || [];
188
- if (reviewers.length > 0) {
189
- output.printNumberedList(reviewers.map(r => output.highlight(r)));
190
- }
191
- else {
192
- output.writeln(output.dim('No specific reviewers recommended'));
193
- }
194
- }
195
- // Verbose file-level details
196
- if (verbose && result.fileRisks) {
197
- output.writeln();
198
- output.writeln(output.bold('File-Level Analysis'));
199
- output.writeln(output.dim('-'.repeat(50)));
200
- output.printTable({
201
- columns: [
202
- { key: 'path', header: 'File', width: 40 },
203
- { key: 'risk', header: 'Risk', width: 12, format: (v) => getRiskDisplay(String(v)) },
204
- { key: 'score', header: 'Score', width: 8, align: 'right' },
205
- { key: 'reasons', header: 'Reasons', width: 30, format: (v) => {
206
- const reasons = v;
207
- return reasons.slice(0, 2).join('; ');
208
- } },
209
- ],
210
- data: result.fileRisks,
211
- });
212
- }
213
- // Files changed table
214
- if (formatType === 'table' || showAll) {
215
- output.writeln();
216
- output.writeln(output.bold('Files Changed'));
217
- output.writeln(output.dim('-'.repeat(50)));
218
- output.printTable({
219
- columns: [
220
- { key: 'status', header: 'Status', width: 10, format: (v) => getStatusDisplay(String(v)) },
221
- { key: 'path', header: 'File', width: 45 },
222
- { key: 'additions', header: '+', width: 8, align: 'right', format: (v) => output.success(`+${v}`) },
223
- { key: 'deletions', header: '-', width: 8, align: 'right', format: (v) => output.error(`-${v}`) },
224
- ],
225
- data: files.slice(0, 20),
226
- });
227
- if (files.length > 20) {
228
- output.writeln(output.dim(` ... and ${files.length - 20} more files`));
229
- }
230
- }
231
- return { success: true, data: result };
232
- }
233
- catch (error) {
234
- if (error instanceof MCPClientError) {
235
- output.printError(`Diff analysis failed: ${error.message}`);
236
- }
237
- else {
238
- output.printError(`Unexpected error: ${String(error)}`);
239
- }
240
- return { success: false, exitCode: 1 };
241
- }
242
- },
243
- };
244
- // Code subcommand (placeholder for future code analysis)
245
- const codeCommand = {
246
- name: 'code',
247
- description: 'Static code analysis and quality assessment',
248
- options: [
249
- { name: 'path', short: 'p', type: 'string', description: 'Path to analyze', default: '.' },
250
- { name: 'type', short: 't', type: 'string', description: 'Analysis type: quality, complexity, security', default: 'quality' },
251
- { name: 'format', short: 'f', type: 'string', description: 'Output format: text, json', default: 'text' },
252
- ],
253
- examples: [
254
- { command: 'monomind analyze code -p ./src', description: 'Analyze source directory' },
255
- { command: 'monomind analyze code --type complexity', description: 'Run complexity analysis' },
256
- ],
257
- action: async (ctx) => {
258
- const targetPath = resolve(ctx.flags.path || '.');
259
- const analysisType = ctx.flags.type || 'quality';
260
- const formatJson = ctx.flags.format === 'json';
261
- output.writeln();
262
- output.writeln(output.bold('Code Analysis'));
263
- output.writeln(output.dim('-'.repeat(50)));
264
- const spinner = output.createSpinner({ text: `Analyzing ${targetPath}...`, spinner: 'dots' });
265
- spinner.start();
266
- try {
267
- const files = await scanSourceFiles(targetPath);
268
- if (files.length === 0) {
269
- spinner.stop();
270
- output.printWarning('No source files found');
271
- return { success: true };
272
- }
273
- const fileStats = [];
274
- for (const filePath of files) {
275
- const content = await fs.readFile(filePath, 'utf-8');
276
- const lines = content.split('\n');
277
- const nonEmpty = lines.filter(l => l.trim().length > 0 && !/^\s*(\/\/|\/\*|\*\s|#)/.test(l)).length;
278
- const todos = (content.match(/\b(TODO|FIXME|HACK|XXX)\b/gi) || []).length;
279
- const fns = (content.match(/(?:export\s+)?(?:async\s+)?function\s+\w+|(?:const|let|var)\s+\w+\s*=\s*(?:async\s+)?\([^)]*\)\s*=>/g) || []).length;
280
- const imps = (content.match(/^import\s+/gm) || []).length + (content.match(/require\s*\(/g) || []).length;
281
- let maxNesting = 0;
282
- let nesting = 0;
283
- for (const line of lines) {
284
- nesting += (line.match(/\{/g) || []).length;
285
- nesting -= (line.match(/\}/g) || []).length;
286
- if (nesting > maxNesting)
287
- maxNesting = nesting;
288
- }
289
- const securityIssues = [];
290
- if (/\beval\s*\(/.test(content))
291
- securityIssues.push('eval()');
292
- if (/\bexec\s*\(/.test(content))
293
- securityIssues.push('exec()');
294
- if (/\.innerHTML\s*=/.test(content))
295
- securityIssues.push('innerHTML');
296
- if (/dangerouslySetInnerHTML/.test(content))
297
- securityIssues.push('dangerouslySetInnerHTML');
298
- if (/['"](?:password|secret|api[_-]?key|token)\s*[:=]\s*['"][^'"]{3,}['"]/i.test(content))
299
- securityIssues.push('hardcoded secret');
300
- if (/new\s+Function\s*\(/.test(content))
301
- securityIssues.push('new Function()');
302
- fileStats.push({
303
- file: filePath,
304
- loc: nonEmpty,
305
- todos,
306
- functions: fns,
307
- imports: imps,
308
- maxNesting,
309
- securityIssues,
310
- });
311
- }
312
- spinner.stop();
313
- const totalLoc = fileStats.reduce((s, f) => s + f.loc, 0);
314
- const totalTodos = fileStats.reduce((s, f) => s + f.todos, 0);
315
- const totalFunctions = fileStats.reduce((s, f) => s + f.functions, 0);
316
- const totalImports = fileStats.reduce((s, f) => s + f.imports, 0);
317
- const avgFileSize = Math.round(totalLoc / files.length);
318
- const longestFile = fileStats.reduce((a, b) => a.loc > b.loc ? a : b);
319
- const avgFnPerFile = (totalFunctions / files.length).toFixed(1);
320
- const deepestNesting = fileStats.reduce((a, b) => a.maxNesting > b.maxNesting ? a : b);
321
- const allSecurityIssues = fileStats.filter(f => f.securityIssues.length > 0);
322
- if (formatJson) {
323
- const jsonData = { type: analysisType, path: targetPath, files: files.length, totalLoc, totalTodos, totalFunctions, totalImports, avgFileSize, fileStats: fileStats.map(f => ({ relativePath: path.relative(targetPath, f.file), loc: f.loc, todos: f.todos, functions: f.functions, imports: f.imports, maxNesting: f.maxNesting, securityIssues: f.securityIssues })) };
324
- output.printJson(jsonData);
325
- return { success: true, data: jsonData };
326
- }
327
- if (analysisType === 'quality') {
328
- output.printBox([`Files: ${files.length}`, `Lines of Code: ${totalLoc.toLocaleString()}`, `Avg File Size: ${avgFileSize} LOC`, `TODO/FIXME: ${totalTodos}`, `Functions: ${totalFunctions}`, `Imports: ${totalImports}`].join('\n'), 'Quality Summary');
329
- output.writeln();
330
- output.writeln(output.bold('Largest Files'));
331
- output.writeln(output.dim('-'.repeat(60)));
332
- const top10 = [...fileStats].sort((a, b) => b.loc - a.loc).slice(0, 10);
333
- output.printTable({
334
- columns: [
335
- { key: 'file', header: 'File', width: 45 },
336
- { key: 'loc', header: 'LOC', width: 8, align: 'right' },
337
- { key: 'fns', header: 'Fns', width: 6, align: 'right' },
338
- { key: 'todos', header: 'TODOs', width: 7, align: 'right' },
339
- ],
340
- data: top10.map(f => ({ file: path.relative(targetPath, f.file), loc: f.loc, fns: f.functions, todos: f.todos })),
341
- });
342
- if (totalTodos > 0) {
343
- output.writeln();
344
- output.printWarning(`${totalTodos} TODO/FIXME comments found across ${fileStats.filter(f => f.todos > 0).length} files`);
345
- }
346
- }
347
- else if (analysisType === 'complexity') {
348
- output.printBox([`Files: ${files.length}`, `Total Functions: ${totalFunctions}`, `Avg Functions/File: ${avgFnPerFile}`, `Deepest Nesting: ${deepestNesting.maxNesting} levels (${path.relative(targetPath, deepestNesting.file)})`, `Longest File: ${longestFile.loc} LOC (${path.relative(targetPath, longestFile.file)})`].join('\n'), 'Complexity Summary');
349
- output.writeln();
350
- output.writeln(output.bold('High Complexity Files (nesting > 5)'));
351
- output.writeln(output.dim('-'.repeat(60)));
352
- const complex = fileStats.filter(f => f.maxNesting > 5).sort((a, b) => b.maxNesting - a.maxNesting);
353
- if (complex.length === 0) {
354
- output.printSuccess('No files with excessive nesting detected');
355
- }
356
- else {
357
- output.printTable({
358
- columns: [
359
- { key: 'file', header: 'File', width: 45 },
360
- { key: 'nesting', header: 'Max Nest', width: 10, align: 'right' },
361
- { key: 'fns', header: 'Fns', width: 6, align: 'right' },
362
- { key: 'loc', header: 'LOC', width: 8, align: 'right' },
363
- ],
364
- data: complex.slice(0, 15).map(f => ({ file: path.relative(targetPath, f.file), nesting: f.maxNesting, fns: f.functions, loc: f.loc })),
365
- });
366
- }
367
- }
368
- else if (analysisType === 'security') {
369
- output.printBox([`Files Scanned: ${files.length}`, `Files with Issues: ${allSecurityIssues.length}`, `Total Issues: ${allSecurityIssues.reduce((s, f) => s + f.securityIssues.length, 0)}`].join('\n'), 'Security Summary');
370
- if (allSecurityIssues.length === 0) {
371
- output.writeln();
372
- output.printSuccess('No common security patterns detected');
373
- }
374
- else {
375
- output.writeln();
376
- output.writeln(output.bold('Security Concerns'));
377
- output.writeln(output.dim('-'.repeat(60)));
378
- output.printTable({
379
- columns: [
380
- { key: 'file', header: 'File', width: 40 },
381
- { key: 'issues', header: 'Issues', width: 35 },
382
- ],
383
- data: allSecurityIssues.map(f => ({ file: path.relative(targetPath, f.file), issues: f.securityIssues.join(', ') })),
384
- });
385
- }
386
- }
387
- else {
388
- output.printWarning(`Unknown analysis type: ${analysisType}. Use quality, complexity, or security.`);
389
- }
390
- return { success: true };
391
- }
392
- catch (error) {
393
- spinner.stop();
394
- const message = error instanceof Error ? error.message : String(error);
395
- output.printError(`Code analysis failed: ${message}`);
396
- return { success: false, exitCode: 1 };
397
- }
398
- },
399
- };
400
- // ============================================================================
401
- // AST Analysis Subcommands (using monovector tree-sitter with fallback)
402
- // ============================================================================
403
- /**
404
- * Helper: Truncate file path for display
405
- */
406
- function truncatePathAst(filePath, maxLen = 45) {
407
- if (filePath.length <= maxLen)
408
- return filePath;
409
- return '...' + filePath.slice(-(maxLen - 3));
410
- }
411
- /**
412
- * Helper: Format complexity value with color coding
413
- */
414
- function formatComplexityValueAst(value) {
415
- if (value <= 5)
416
- return output.success(String(value));
417
- if (value <= 10)
418
- return output.warning(String(value));
419
- return output.error(String(value));
420
- }
421
- /**
422
- * Helper: Get type marker for symbols
423
- */
424
- function getTypeMarkerAst(type) {
425
- switch (type) {
426
- case 'function': return output.success('fn');
427
- case 'class': return output.info('class');
428
- case 'variable': return output.dim('var');
429
- case 'type': return output.highlight('type');
430
- case 'interface': return output.highlight('iface');
431
- default: return output.dim(type.slice(0, 5));
432
- }
433
- }
434
- /**
435
- * Helper: Get complexity rating text
436
- */
437
- function getComplexityRatingAst(value) {
438
- if (value <= 5)
439
- return output.success('Simple');
440
- if (value <= 10)
441
- return output.warning('Moderate');
442
- if (value <= 20)
443
- return output.error('Complex');
444
- return output.error(output.bold('Very Complex'));
445
- }
446
- /**
447
- * AST analysis subcommand
448
- */
449
- const astCommand = {
450
- name: 'ast',
451
- description: 'Analyze code using AST parsing (tree-sitter via monovector)',
452
- options: [
453
- {
454
- name: 'complexity',
455
- short: 'c',
456
- description: 'Include complexity metrics',
457
- type: 'boolean',
458
- default: false,
459
- },
460
- {
461
- name: 'symbols',
462
- short: 's',
463
- description: 'Include symbol extraction',
464
- type: 'boolean',
465
- default: false,
466
- },
467
- {
468
- name: 'format',
469
- short: 'f',
470
- description: 'Output format (text, json, table)',
471
- type: 'string',
472
- default: 'text',
473
- choices: ['text', 'json', 'table'],
474
- },
475
- {
476
- name: 'output',
477
- short: 'o',
478
- description: 'Output file path',
479
- type: 'string',
480
- },
481
- {
482
- name: 'verbose',
483
- short: 'v',
484
- description: 'Show detailed analysis',
485
- type: 'boolean',
486
- default: false,
487
- },
488
- ],
489
- examples: [
490
- { command: 'monomind analyze ast src/', description: 'Analyze all files in src/' },
491
- { command: 'monomind analyze ast src/index.ts --complexity', description: 'Analyze with complexity' },
492
- { command: 'monomind analyze ast . --format json', description: 'JSON output' },
493
- { command: 'monomind analyze ast src/ --symbols', description: 'Extract symbols' },
494
- ],
495
- action: async (ctx) => {
496
- const targetPath = ctx.args[0] || ctx.cwd;
497
- const showComplexity = ctx.flags.complexity;
498
- const showSymbols = ctx.flags.symbols;
499
- const formatType = ctx.flags.format || 'text';
500
- const outputFile = ctx.flags.output;
501
- const verbose = ctx.flags.verbose;
502
- // If no specific flags, show summary
503
- const showAll = !showComplexity && !showSymbols;
504
- output.printInfo(`Analyzing: ${output.highlight(targetPath)}`);
505
- output.writeln();
506
- const spinner = output.createSpinner({ text: 'Parsing AST...', spinner: 'dots' });
507
- spinner.start();
508
- try {
509
- const astModule = await getASTAnalyzer();
510
- if (!astModule) {
511
- spinner.stop();
512
- output.printWarning('AST analyzer not available, using regex fallback');
513
- }
514
- // Resolve path and check if file or directory
515
- const resolvedPath = resolve(targetPath);
516
- const stat = await fs.stat(resolvedPath);
517
- const isDirectory = stat.isDirectory();
518
- let results = [];
519
- if (isDirectory) {
520
- // Scan directory for source files
521
- const files = await scanSourceFiles(resolvedPath);
522
- spinner.stop();
523
- output.printInfo(`Found ${files.length} source files`);
524
- spinner.start();
525
- for (const file of files.slice(0, 100)) {
526
- try {
527
- const content = await fs.readFile(file, 'utf-8');
528
- if (astModule) {
529
- const analyzer = astModule.createASTAnalyzer();
530
- const analysis = analyzer.analyze(content, file);
531
- results.push(analysis);
532
- }
533
- else {
534
- // Fallback analysis
535
- results.push(fallbackAnalyze(content, file));
536
- }
537
- }
538
- catch {
539
- // Skip files that can't be analyzed
540
- }
541
- }
542
- }
543
- else {
544
- // Single file
545
- const content = await fs.readFile(resolvedPath, 'utf-8');
546
- if (astModule) {
547
- const analyzer = astModule.createASTAnalyzer();
548
- const analysis = analyzer.analyze(content, resolvedPath);
549
- results.push(analysis);
550
- }
551
- else {
552
- results.push(fallbackAnalyze(content, resolvedPath));
553
- }
554
- }
555
- spinner.stop();
556
- if (results.length === 0) {
557
- output.printWarning('No files analyzed');
558
- return { success: true };
559
- }
560
- // Calculate totals
561
- const totals = {
562
- files: results.length,
563
- functions: results.reduce((sum, r) => sum + r.functions.length, 0),
564
- classes: results.reduce((sum, r) => sum + r.classes.length, 0),
565
- imports: results.reduce((sum, r) => sum + r.imports.length, 0),
566
- avgComplexity: results.reduce((sum, r) => sum + r.complexity.cyclomatic, 0) / results.length,
567
- totalLoc: results.reduce((sum, r) => sum + r.complexity.loc, 0),
568
- };
569
- // JSON output
570
- if (formatType === 'json') {
571
- const jsonOutput = { files: results, totals };
572
- if (outputFile) {
573
- await safeWriteOutputFile(outputFile, JSON.stringify(jsonOutput, null, 2));
574
- output.printSuccess(`Results written to ${outputFile}`);
575
- }
576
- else {
577
- output.printJson(jsonOutput);
578
- }
579
- return { success: true, data: jsonOutput };
580
- }
581
- // Summary box
582
- output.printBox([
583
- `Files analyzed: ${totals.files}`,
584
- `Functions: ${totals.functions}`,
585
- `Classes: ${totals.classes}`,
586
- `Total LOC: ${totals.totalLoc}`,
587
- `Avg Complexity: ${formatComplexityValueAst(Math.round(totals.avgComplexity))}`,
588
- ].join('\n'), 'AST Analysis Summary');
589
- // Complexity view
590
- if (showComplexity || showAll) {
591
- output.writeln();
592
- output.writeln(output.bold('Complexity by File'));
593
- output.writeln(output.dim('-'.repeat(60)));
594
- const complexityData = results
595
- .map(r => ({
596
- file: truncatePathAst(r.filePath),
597
- cyclomatic: r.complexity.cyclomatic,
598
- cognitive: r.complexity.cognitive,
599
- loc: r.complexity.loc,
600
- rating: getComplexityRatingAst(r.complexity.cyclomatic),
601
- }))
602
- .sort((a, b) => b.cyclomatic - a.cyclomatic)
603
- .slice(0, 15);
604
- output.printTable({
605
- columns: [
606
- { key: 'file', header: 'File', width: 40 },
607
- { key: 'cyclomatic', header: 'Cyclo', width: 8, align: 'right', format: (v) => formatComplexityValueAst(v) },
608
- { key: 'cognitive', header: 'Cogni', width: 8, align: 'right' },
609
- { key: 'loc', header: 'LOC', width: 8, align: 'right' },
610
- { key: 'rating', header: 'Rating', width: 15 },
611
- ],
612
- data: complexityData,
613
- });
614
- if (results.length > 15) {
615
- output.writeln(output.dim(` ... and ${results.length - 15} more files`));
616
- }
617
- }
618
- // Symbols view
619
- if (showSymbols || showAll) {
620
- output.writeln();
621
- output.writeln(output.bold('Extracted Symbols'));
622
- output.writeln(output.dim('-'.repeat(60)));
623
- const allSymbols = [];
624
- for (const r of results) {
625
- for (const fn of r.functions) {
626
- allSymbols.push({ name: fn.name, type: 'function', file: truncatePathAst(r.filePath, 30), line: fn.startLine });
627
- }
628
- for (const cls of r.classes) {
629
- allSymbols.push({ name: cls.name, type: 'class', file: truncatePathAst(r.filePath, 30), line: cls.startLine });
630
- }
631
- }
632
- const displaySymbols = allSymbols.slice(0, 20);
633
- output.printTable({
634
- columns: [
635
- { key: 'type', header: 'Type', width: 8, format: (v) => getTypeMarkerAst(v) },
636
- { key: 'name', header: 'Symbol', width: 30 },
637
- { key: 'file', header: 'File', width: 35 },
638
- { key: 'line', header: 'Line', width: 8, align: 'right' },
639
- ],
640
- data: displaySymbols,
641
- });
642
- if (allSymbols.length > 20) {
643
- output.writeln(output.dim(` ... and ${allSymbols.length - 20} more symbols`));
644
- }
645
- }
646
- // Verbose output
647
- if (verbose) {
648
- output.writeln();
649
- output.writeln(output.bold('Import Analysis'));
650
- output.writeln(output.dim('-'.repeat(60)));
651
- const importCounts = new Map();
652
- for (const r of results) {
653
- for (const imp of r.imports) {
654
- importCounts.set(imp, (importCounts.get(imp) || 0) + 1);
655
- }
656
- }
657
- const topImports = Array.from(importCounts.entries())
658
- .sort((a, b) => b[1] - a[1])
659
- .slice(0, 10);
660
- for (const [imp, count] of topImports) {
661
- output.writeln(` ${output.highlight(count.toString().padStart(3))} ${imp}`);
662
- }
663
- }
664
- if (outputFile) {
665
- await safeWriteOutputFile(outputFile, JSON.stringify({ files: results, totals }, null, 2));
666
- output.printSuccess(`Results written to ${outputFile}`);
667
- }
668
- return { success: true, data: { files: results, totals } };
669
- }
670
- catch (error) {
671
- spinner.stop();
672
- const message = error instanceof Error ? error.message : String(error);
673
- output.printError(`AST analysis failed: ${message}`);
674
- return { success: false, exitCode: 1 };
675
- }
676
- },
677
- };
678
- /**
679
- * Complexity analysis subcommand
680
- */
681
- const complexityAstCommand = {
682
- name: 'complexity',
683
- aliases: ['cx'],
684
- description: 'Analyze code complexity metrics',
685
- options: [
686
- {
687
- name: 'threshold',
688
- short: 't',
689
- description: 'Complexity threshold to flag (default: 10)',
690
- type: 'number',
691
- default: 10,
692
- },
693
- {
694
- name: 'format',
695
- short: 'f',
696
- description: 'Output format (text, json)',
697
- type: 'string',
698
- default: 'text',
699
- choices: ['text', 'json'],
700
- },
701
- {
702
- name: 'output',
703
- short: 'o',
704
- description: 'Output file path',
705
- type: 'string',
706
- },
707
- ],
708
- examples: [
709
- { command: 'monomind analyze complexity src/', description: 'Analyze complexity' },
710
- { command: 'monomind analyze complexity src/ --threshold 15', description: 'Flag high complexity' },
711
- ],
712
- action: async (ctx) => {
713
- const targetPath = ctx.args[0] || ctx.cwd;
714
- const threshold = ctx.flags.threshold || 10;
715
- const formatType = ctx.flags.format || 'text';
716
- const outputFile = ctx.flags.output;
717
- output.printInfo(`Analyzing complexity: ${output.highlight(targetPath)}`);
718
- output.writeln();
719
- const spinner = output.createSpinner({ text: 'Calculating complexity...', spinner: 'dots' });
720
- spinner.start();
721
- try {
722
- const astModule = await getASTAnalyzer();
723
- const resolvedPath = resolve(targetPath);
724
- const stat = await fs.stat(resolvedPath);
725
- const files = stat.isDirectory() ? await scanSourceFiles(resolvedPath) : [resolvedPath];
726
- const results = [];
727
- for (const file of files.slice(0, 100)) {
728
- try {
729
- const content = await fs.readFile(file, 'utf-8');
730
- let analysis;
731
- if (astModule) {
732
- const analyzer = astModule.createASTAnalyzer();
733
- analysis = analyzer.analyze(content, file);
734
- }
735
- else {
736
- analysis = fallbackAnalyze(content, file);
737
- }
738
- const flagged = analysis.complexity.cyclomatic > threshold;
739
- const rating = analysis.complexity.cyclomatic <= 5 ? 'Simple' :
740
- analysis.complexity.cyclomatic <= 10 ? 'Moderate' :
741
- analysis.complexity.cyclomatic <= 20 ? 'Complex' : 'Very Complex';
742
- results.push({
743
- file: file,
744
- cyclomatic: analysis.complexity.cyclomatic,
745
- cognitive: analysis.complexity.cognitive,
746
- loc: analysis.complexity.loc,
747
- commentDensity: analysis.complexity.commentDensity,
748
- rating,
749
- flagged,
750
- });
751
- }
752
- catch {
753
- // Skip files that can't be analyzed
754
- }
755
- }
756
- spinner.stop();
757
- // Sort by complexity descending
758
- results.sort((a, b) => b.cyclomatic - a.cyclomatic);
759
- const flaggedCount = results.filter(r => r.flagged).length;
760
- const avgComplexity = results.length > 0
761
- ? results.reduce((sum, r) => sum + r.cyclomatic, 0) / results.length
762
- : 0;
763
- if (formatType === 'json') {
764
- const jsonOutput = { files: results, summary: { total: results.length, flagged: flaggedCount, avgComplexity, threshold } };
765
- if (outputFile) {
766
- await safeWriteOutputFile(outputFile, JSON.stringify(jsonOutput, null, 2));
767
- output.printSuccess(`Results written to ${outputFile}`);
768
- }
769
- else {
770
- output.printJson(jsonOutput);
771
- }
772
- return { success: true, data: jsonOutput };
773
- }
774
- // Summary
775
- output.printBox([
776
- `Files analyzed: ${results.length}`,
777
- `Threshold: ${threshold}`,
778
- `Flagged files: ${flaggedCount > 0 ? output.error(String(flaggedCount)) : output.success('0')}`,
779
- `Average complexity: ${formatComplexityValueAst(Math.round(avgComplexity))}`,
780
- ].join('\n'), 'Complexity Analysis');
781
- // Show flagged files first
782
- if (flaggedCount > 0) {
783
- output.writeln();
784
- output.writeln(output.bold(output.warning(`High Complexity Files (>${threshold})`)));
785
- output.writeln(output.dim('-'.repeat(60)));
786
- const flaggedFiles = results.filter(r => r.flagged).slice(0, 10);
787
- output.printTable({
788
- columns: [
789
- { key: 'file', header: 'File', width: 40, format: (v) => truncatePathAst(v) },
790
- { key: 'cyclomatic', header: 'Cyclo', width: 8, align: 'right', format: (v) => output.error(String(v)) },
791
- { key: 'cognitive', header: 'Cogni', width: 8, align: 'right' },
792
- { key: 'loc', header: 'LOC', width: 8, align: 'right' },
793
- { key: 'rating', header: 'Rating', width: 15 },
794
- ],
795
- data: flaggedFiles,
796
- });
797
- }
798
- // Show all files in table format
799
- output.writeln();
800
- output.writeln(output.bold('All Files'));
801
- output.writeln(output.dim('-'.repeat(60)));
802
- const displayFiles = results.slice(0, 15);
803
- output.printTable({
804
- columns: [
805
- { key: 'file', header: 'File', width: 40, format: (v) => truncatePathAst(v) },
806
- { key: 'cyclomatic', header: 'Cyclo', width: 8, align: 'right', format: (v) => formatComplexityValueAst(v) },
807
- { key: 'cognitive', header: 'Cogni', width: 8, align: 'right' },
808
- { key: 'loc', header: 'LOC', width: 8, align: 'right' },
809
- ],
810
- data: displayFiles,
811
- });
812
- if (results.length > 15) {
813
- output.writeln(output.dim(` ... and ${results.length - 15} more files`));
814
- }
815
- if (outputFile) {
816
- await safeWriteOutputFile(outputFile, JSON.stringify({ files: results, summary: { total: results.length, flagged: flaggedCount, avgComplexity, threshold } }, null, 2));
817
- output.printSuccess(`Results written to ${outputFile}`);
818
- }
819
- return { success: true, data: { files: results, flaggedCount } };
820
- }
821
- catch (error) {
822
- spinner.stop();
823
- const message = error instanceof Error ? error.message : String(error);
824
- output.printError(`Complexity analysis failed: ${message}`);
825
- return { success: false, exitCode: 1 };
826
- }
827
- },
828
- };
829
- /**
830
- * Symbol extraction subcommand
831
- */
832
- const symbolsCommand = {
833
- name: 'symbols',
834
- aliases: ['sym'],
835
- description: 'Extract and list code symbols (functions, classes, types)',
836
- options: [
837
- {
838
- name: 'type',
839
- short: 't',
840
- description: 'Filter by symbol type (function, class, all)',
841
- type: 'string',
842
- default: 'all',
843
- choices: ['function', 'class', 'all'],
844
- },
845
- {
846
- name: 'format',
847
- short: 'f',
848
- description: 'Output format (text, json)',
849
- type: 'string',
850
- default: 'text',
851
- choices: ['text', 'json'],
852
- },
853
- {
854
- name: 'output',
855
- short: 'o',
856
- description: 'Output file path',
857
- type: 'string',
858
- },
859
- ],
860
- examples: [
861
- { command: 'monomind analyze symbols src/', description: 'Extract all symbols' },
862
- { command: 'monomind analyze symbols src/ --type function', description: 'Only functions' },
863
- { command: 'monomind analyze symbols src/ --format json', description: 'JSON output' },
864
- ],
865
- action: async (ctx) => {
866
- const targetPath = ctx.args[0] || ctx.cwd;
867
- const symbolType = ctx.flags.type || 'all';
868
- const formatType = ctx.flags.format || 'text';
869
- const outputFile = ctx.flags.output;
870
- output.printInfo(`Extracting symbols: ${output.highlight(targetPath)}`);
871
- output.writeln();
872
- const spinner = output.createSpinner({ text: 'Parsing code...', spinner: 'dots' });
873
- spinner.start();
874
- try {
875
- const astModule = await getASTAnalyzer();
876
- const resolvedPath = resolve(targetPath);
877
- const stat = await fs.stat(resolvedPath);
878
- const files = stat.isDirectory() ? await scanSourceFiles(resolvedPath) : [resolvedPath];
879
- const symbols = [];
880
- for (const file of files.slice(0, 100)) {
881
- try {
882
- const content = await fs.readFile(file, 'utf-8');
883
- let analysis;
884
- if (astModule) {
885
- const analyzer = astModule.createASTAnalyzer();
886
- analysis = analyzer.analyze(content, file);
887
- }
888
- else {
889
- analysis = fallbackAnalyze(content, file);
890
- }
891
- if (symbolType === 'all' || symbolType === 'function') {
892
- for (const fn of analysis.functions) {
893
- symbols.push({
894
- name: fn.name,
895
- type: 'function',
896
- file,
897
- startLine: fn.startLine,
898
- endLine: fn.endLine,
899
- });
900
- }
901
- }
902
- if (symbolType === 'all' || symbolType === 'class') {
903
- for (const cls of analysis.classes) {
904
- symbols.push({
905
- name: cls.name,
906
- type: 'class',
907
- file,
908
- startLine: cls.startLine,
909
- endLine: cls.endLine,
910
- });
911
- }
912
- }
913
- }
914
- catch {
915
- // Skip files that can't be parsed
916
- }
917
- }
918
- spinner.stop();
919
- // Sort by file then name
920
- symbols.sort((a, b) => a.file.localeCompare(b.file) || a.name.localeCompare(b.name));
921
- if (formatType === 'json') {
922
- if (outputFile) {
923
- await safeWriteOutputFile(outputFile, JSON.stringify(symbols, null, 2));
924
- output.printSuccess(`Results written to ${outputFile}`);
925
- }
926
- else {
927
- output.printJson(symbols);
928
- }
929
- return { success: true, data: symbols };
930
- }
931
- // Summary
932
- const functionCount = symbols.filter(s => s.type === 'function').length;
933
- const classCount = symbols.filter(s => s.type === 'class').length;
934
- output.printBox([
935
- `Total symbols: ${symbols.length}`,
936
- `Functions: ${functionCount}`,
937
- `Classes: ${classCount}`,
938
- `Files: ${files.length}`,
939
- ].join('\n'), 'Symbol Extraction');
940
- output.writeln();
941
- output.writeln(output.bold('Symbols'));
942
- output.writeln(output.dim('-'.repeat(60)));
943
- const displaySymbols = symbols.slice(0, 30);
944
- output.printTable({
945
- columns: [
946
- { key: 'type', header: 'Type', width: 10, format: (v) => getTypeMarkerAst(v) },
947
- { key: 'name', header: 'Name', width: 30 },
948
- { key: 'file', header: 'File', width: 35, format: (v) => truncatePathAst(v, 33) },
949
- { key: 'startLine', header: 'Line', width: 8, align: 'right' },
950
- ],
951
- data: displaySymbols,
952
- });
953
- if (symbols.length > 30) {
954
- output.writeln(output.dim(` ... and ${symbols.length - 30} more symbols`));
955
- }
956
- if (outputFile) {
957
- await safeWriteOutputFile(outputFile, JSON.stringify(symbols, null, 2));
958
- output.printSuccess(`Results written to ${outputFile}`);
959
- }
960
- return { success: true, data: symbols };
961
- }
962
- catch (error) {
963
- spinner.stop();
964
- const message = error instanceof Error ? error.message : String(error);
965
- output.printError(`Symbol extraction failed: ${message}`);
966
- return { success: false, exitCode: 1 };
967
- }
968
- },
969
- };
970
- /**
971
- * Imports analysis subcommand
972
- */
973
- const importsCommand = {
974
- name: 'imports',
975
- aliases: ['imp'],
976
- description: 'Analyze import dependencies across files',
977
- options: [
978
- {
979
- name: 'format',
980
- short: 'f',
981
- description: 'Output format (text, json)',
982
- type: 'string',
983
- default: 'text',
984
- choices: ['text', 'json'],
985
- },
986
- {
987
- name: 'output',
988
- short: 'o',
989
- description: 'Output file path',
990
- type: 'string',
991
- },
992
- {
993
- name: 'external',
994
- short: 'e',
995
- description: 'Show only external (npm) imports',
996
- type: 'boolean',
997
- default: false,
998
- },
999
- ],
1000
- examples: [
1001
- { command: 'monomind analyze imports src/', description: 'Analyze all imports' },
1002
- { command: 'monomind analyze imports src/ --external', description: 'Only npm packages' },
1003
- ],
1004
- action: async (ctx) => {
1005
- const targetPath = ctx.args[0] || ctx.cwd;
1006
- const formatType = ctx.flags.format || 'text';
1007
- const outputFile = ctx.flags.output;
1008
- const externalOnly = ctx.flags.external;
1009
- output.printInfo(`Analyzing imports: ${output.highlight(targetPath)}`);
1010
- output.writeln();
1011
- const spinner = output.createSpinner({ text: 'Scanning imports...', spinner: 'dots' });
1012
- spinner.start();
1013
- try {
1014
- const astModule = await getASTAnalyzer();
1015
- const resolvedPath = resolve(targetPath);
1016
- const stat = await fs.stat(resolvedPath);
1017
- const files = stat.isDirectory() ? await scanSourceFiles(resolvedPath) : [resolvedPath];
1018
- const importCounts = new Map();
1019
- const fileImports = new Map();
1020
- for (const file of files.slice(0, 100)) {
1021
- try {
1022
- const content = await fs.readFile(file, 'utf-8');
1023
- let analysis;
1024
- if (astModule) {
1025
- const analyzer = astModule.createASTAnalyzer();
1026
- analysis = analyzer.analyze(content, file);
1027
- }
1028
- else {
1029
- analysis = fallbackAnalyze(content, file);
1030
- }
1031
- const imports = analysis.imports.filter(imp => {
1032
- if (externalOnly) {
1033
- return !imp.startsWith('.') && !imp.startsWith('/');
1034
- }
1035
- return true;
1036
- });
1037
- fileImports.set(file, imports);
1038
- for (const imp of imports) {
1039
- const existing = importCounts.get(imp) || { count: 0, files: [] };
1040
- existing.count++;
1041
- existing.files.push(file);
1042
- importCounts.set(imp, existing);
1043
- }
1044
- }
1045
- catch {
1046
- // Skip files that can't be parsed
1047
- }
1048
- }
1049
- spinner.stop();
1050
- // Sort by count
1051
- const sortedImports = Array.from(importCounts.entries())
1052
- .sort((a, b) => b[1].count - a[1].count);
1053
- if (formatType === 'json') {
1054
- const jsonOutput = {
1055
- imports: Object.fromEntries(sortedImports),
1056
- fileImports: Object.fromEntries(fileImports),
1057
- };
1058
- if (outputFile) {
1059
- await safeWriteOutputFile(outputFile, JSON.stringify(jsonOutput, null, 2));
1060
- output.printSuccess(`Results written to ${outputFile}`);
1061
- }
1062
- else {
1063
- output.printJson(jsonOutput);
1064
- }
1065
- return { success: true, data: jsonOutput };
1066
- }
1067
- // Summary
1068
- const externalImports = sortedImports.filter(([imp]) => !imp.startsWith('.') && !imp.startsWith('/'));
1069
- const localImports = sortedImports.filter(([imp]) => imp.startsWith('.') || imp.startsWith('/'));
1070
- output.printBox([
1071
- `Total unique imports: ${sortedImports.length}`,
1072
- `External (npm): ${externalImports.length}`,
1073
- `Local (relative): ${localImports.length}`,
1074
- `Files scanned: ${files.length}`,
1075
- ].join('\n'), 'Import Analysis');
1076
- // Most used imports
1077
- output.writeln();
1078
- output.writeln(output.bold('Most Used Imports'));
1079
- output.writeln(output.dim('-'.repeat(60)));
1080
- const topImports = sortedImports.slice(0, 20);
1081
- output.printTable({
1082
- columns: [
1083
- { key: 'count', header: 'Uses', width: 8, align: 'right' },
1084
- { key: 'import', header: 'Import', width: 50 },
1085
- { key: 'type', header: 'Type', width: 10 },
1086
- ],
1087
- data: topImports.map(([imp, data]) => ({
1088
- count: data.count,
1089
- import: imp,
1090
- type: imp.startsWith('.') || imp.startsWith('/') ? output.dim('local') : output.highlight('npm'),
1091
- })),
1092
- });
1093
- if (sortedImports.length > 20) {
1094
- output.writeln(output.dim(` ... and ${sortedImports.length - 20} more imports`));
1095
- }
1096
- if (outputFile) {
1097
- await safeWriteOutputFile(outputFile, JSON.stringify({
1098
- imports: Object.fromEntries(sortedImports),
1099
- fileImports: Object.fromEntries(fileImports),
1100
- }, null, 2));
1101
- output.printSuccess(`Results written to ${outputFile}`);
1102
- }
1103
- return { success: true, data: { imports: sortedImports } };
1104
- }
1105
- catch (error) {
1106
- spinner.stop();
1107
- const message = error instanceof Error ? error.message : String(error);
1108
- output.printError(`Import analysis failed: ${message}`);
1109
- return { success: false, exitCode: 1 };
1110
- }
1111
- },
1112
- };
1113
47
  /**
1114
48
  * Helper: Scan directory for source files
1115
49
  */
1116
- async function scanSourceFiles(dir, maxDepth = 10) {
50
+ export async function scanSourceFiles(dir, maxDepth = 10) {
1117
51
  const files = [];
1118
52
  const extensions = ['.ts', '.tsx', '.js', '.jsx', '.mjs', '.cjs'];
1119
53
  const excludeDirs = ['node_modules', 'dist', 'build', '.git', 'coverage', '__pycache__'];
@@ -1147,7 +81,7 @@ async function scanSourceFiles(dir, maxDepth = 10) {
1147
81
  /**
1148
82
  * Fallback analysis when monovector is not available
1149
83
  */
1150
- function fallbackAnalyze(code, filePath) {
84
+ export function fallbackAnalyze(code, filePath) {
1151
85
  const lines = code.split('\n');
1152
86
  const functions = [];
1153
87
  const classes = [];
@@ -1217,755 +151,6 @@ function fallbackAnalyze(code, filePath) {
1217
151
  },
1218
152
  };
1219
153
  }
1220
- // Dependencies subcommand
1221
- const depsCommand = {
1222
- name: 'deps',
1223
- description: 'Analyze project dependencies',
1224
- options: [
1225
- { name: 'outdated', short: 'o', type: 'boolean', description: 'Show only outdated dependencies' },
1226
- { name: 'security', short: 's', type: 'boolean', description: 'Check for security vulnerabilities' },
1227
- { name: 'format', short: 'f', type: 'string', description: 'Output format: text, json', default: 'text' },
1228
- ],
1229
- examples: [
1230
- { command: 'monomind analyze deps --outdated', description: 'Show outdated dependencies' },
1231
- { command: 'monomind analyze deps --security', description: 'Check for vulnerabilities' },
1232
- ],
1233
- action: async (ctx) => {
1234
- const showOutdated = ctx.flags.outdated;
1235
- const checkSecurity = ctx.flags.security;
1236
- const formatJson = ctx.flags.format === 'json';
1237
- output.writeln();
1238
- output.writeln(output.bold('Dependency Analysis'));
1239
- output.writeln(output.dim('-'.repeat(50)));
1240
- try {
1241
- const pkgPath = resolve('package.json');
1242
- let pkgContent;
1243
- try {
1244
- pkgContent = await fs.readFile(pkgPath, 'utf-8');
1245
- }
1246
- catch {
1247
- output.printError('No package.json found in current directory');
1248
- return { success: false, exitCode: 1 };
1249
- }
1250
- const pkg = JSON.parse(pkgContent);
1251
- const deps = Object.entries(pkg.dependencies || {});
1252
- const devDeps = Object.entries(pkg.devDependencies || {});
1253
- const optDeps = Object.entries(pkg.optionalDependencies || {});
1254
- const peerDeps = Object.entries(pkg.peerDependencies || {});
1255
- const total = deps.length + devDeps.length + optDeps.length + peerDeps.length;
1256
- if (formatJson && !showOutdated && !checkSecurity) {
1257
- const jsonData = { name: pkg.name, version: pkg.version, dependencies: deps.length, devDependencies: devDeps.length, optionalDependencies: optDeps.length, peerDependencies: peerDeps.length, total };
1258
- output.printJson(jsonData);
1259
- return { success: true, data: jsonData };
1260
- }
1261
- output.printBox([`Package: ${pkg.name || 'unknown'} @ ${pkg.version || '0.0.0'}`, `Dependencies: ${deps.length}`, `Dev Dependencies: ${devDeps.length}`, `Optional: ${optDeps.length}`, `Peer: ${peerDeps.length}`, `Total: ${total}`].join('\n'), 'Dependency Summary');
1262
- if (showOutdated) {
1263
- output.writeln();
1264
- output.writeln(output.bold('Outdated Check'));
1265
- output.writeln(output.dim('-'.repeat(60)));
1266
- const outdated = [];
1267
- const checkDeps = async (entries, category) => {
1268
- for (const [name, declared] of entries) {
1269
- try {
1270
- const installedPkg = resolve('node_modules', name, 'package.json');
1271
- const raw = await fs.readFile(installedPkg, 'utf-8');
1272
- const installedContent = JSON.parse(raw);
1273
- const installed = installedContent.version || 'unknown';
1274
- const cleanDeclared = declared.replace(/^[\^~>=<]+/, '');
1275
- if (installed !== cleanDeclared) {
1276
- outdated.push({ name, declared: declared, installed, category });
1277
- }
1278
- }
1279
- catch {
1280
- outdated.push({ name, declared: declared, installed: 'not installed', category });
1281
- }
1282
- }
1283
- };
1284
- await checkDeps(deps, 'prod');
1285
- await checkDeps(devDeps, 'dev');
1286
- if (outdated.length === 0) {
1287
- output.printSuccess('All dependencies match declared versions');
1288
- }
1289
- else {
1290
- output.printTable({
1291
- columns: [
1292
- { key: 'name', header: 'Package', width: 30 },
1293
- { key: 'declared', header: 'Declared', width: 14 },
1294
- { key: 'installed', header: 'Installed', width: 14 },
1295
- { key: 'category', header: 'Type', width: 6 },
1296
- ],
1297
- data: outdated.slice(0, 30),
1298
- });
1299
- if (outdated.length > 30) {
1300
- output.writeln(output.dim(` ... and ${outdated.length - 30} more`));
1301
- }
1302
- }
1303
- }
1304
- if (checkSecurity) {
1305
- output.writeln();
1306
- output.writeln(output.bold('Security Audit'));
1307
- output.writeln(output.dim('-'.repeat(60)));
1308
- try {
1309
- const auditRaw = execSync('npm audit --json 2>/dev/null', { encoding: 'utf-8', maxBuffer: 10 * 1024 * 1024 });
1310
- const audit = JSON.parse(auditRaw);
1311
- const vulns = audit.metadata?.vulnerabilities || audit.vulnerabilities || {};
1312
- const info = vulns.info || 0;
1313
- const low = vulns.low || 0;
1314
- const moderate = vulns.moderate || 0;
1315
- const high = vulns.high || 0;
1316
- const critical = vulns.critical || 0;
1317
- const totalVulns = info + low + moderate + high + critical;
1318
- if (totalVulns === 0) {
1319
- output.printSuccess('No known vulnerabilities found');
1320
- }
1321
- else {
1322
- output.printTable({
1323
- columns: [
1324
- { key: 'severity', header: 'Severity', width: 12 },
1325
- { key: 'count', header: 'Count', width: 8, align: 'right' },
1326
- ],
1327
- data: [
1328
- ...(critical > 0 ? [{ severity: 'Critical', count: critical }] : []),
1329
- ...(high > 0 ? [{ severity: 'High', count: high }] : []),
1330
- ...(moderate > 0 ? [{ severity: 'Moderate', count: moderate }] : []),
1331
- ...(low > 0 ? [{ severity: 'Low', count: low }] : []),
1332
- ...(info > 0 ? [{ severity: 'Info', count: info }] : []),
1333
- { severity: 'Total', count: totalVulns },
1334
- ],
1335
- });
1336
- if (critical > 0 || high > 0) {
1337
- output.printWarning(`${critical + high} high/critical vulnerabilities found. Run 'npm audit' for details.`);
1338
- }
1339
- }
1340
- }
1341
- catch {
1342
- output.printWarning('npm audit failed. Ensure npm is available and node_modules is installed.');
1343
- }
1344
- }
1345
- return { success: true };
1346
- }
1347
- catch (error) {
1348
- const message = error instanceof Error ? error.message : String(error);
1349
- output.printError(`Dependency analysis failed: ${message}`);
1350
- return { success: false, exitCode: 1 };
1351
- }
1352
- },
1353
- };
1354
- // ============================================================================
1355
- // Graph Analysis Subcommands (MinCut, Louvain, Circular Dependencies)
1356
- // ============================================================================
1357
- /**
1358
- * Analyze code boundaries using MinCut algorithm
1359
- */
1360
- const boundariesCommand = {
1361
- name: 'boundaries',
1362
- aliases: ['boundary', 'mincut'],
1363
- description: 'Find natural code boundaries using MinCut algorithm',
1364
- options: [
1365
- {
1366
- name: 'partitions',
1367
- short: 'p',
1368
- description: 'Number of partitions to find',
1369
- type: 'number',
1370
- default: 2,
1371
- },
1372
- {
1373
- name: 'output',
1374
- short: 'o',
1375
- description: 'Output file path',
1376
- type: 'string',
1377
- },
1378
- {
1379
- name: 'format',
1380
- short: 'f',
1381
- description: 'Output format (text, json, dot)',
1382
- type: 'string',
1383
- default: 'text',
1384
- choices: ['text', 'json', 'dot'],
1385
- },
1386
- ],
1387
- examples: [
1388
- { command: 'monomind analyze boundaries src/', description: 'Find code boundaries in src/' },
1389
- { command: 'monomind analyze boundaries -p 3 src/', description: 'Find 3 partitions' },
1390
- { command: 'monomind analyze boundaries -f dot -o graph.dot src/', description: 'Export to DOT format' },
1391
- ],
1392
- action: async (ctx) => {
1393
- const targetDir = ctx.args[0] || ctx.cwd;
1394
- const rawPartitions = ctx.flags.partitions || 2;
1395
- const numPartitions = Number.isFinite(rawPartitions) ? Math.max(1, Math.min(rawPartitions, 100)) : 2;
1396
- const outputFile = ctx.flags.output;
1397
- const format = ctx.flags.format || 'text';
1398
- output.printInfo(`Analyzing code boundaries in: ${output.highlight(targetDir)}`);
1399
- output.writeln();
1400
- const spinner = output.createSpinner({ text: 'Building dependency graph...', spinner: 'dots' });
1401
- spinner.start();
1402
- try {
1403
- const analyzer = await getGraphAnalyzer();
1404
- if (!analyzer) {
1405
- spinner.stop();
1406
- output.printError('Graph analyzer module not available');
1407
- return { success: false, exitCode: 1 };
1408
- }
1409
- const result = await analyzer.analyzeGraph(resolve(targetDir), {
1410
- includeBoundaries: true,
1411
- includeModules: false,
1412
- numPartitions,
1413
- });
1414
- spinner.stop();
1415
- // Handle different output formats
1416
- if (format === 'json') {
1417
- const jsonOutput = {
1418
- boundaries: result.boundaries,
1419
- statistics: result.statistics,
1420
- circularDependencies: result.circularDependencies,
1421
- };
1422
- if (outputFile) {
1423
- await safeWriteOutputFile(outputFile, JSON.stringify(jsonOutput, null, 2));
1424
- output.printSuccess(`Results written to ${outputFile}`);
1425
- }
1426
- else {
1427
- output.printJson(jsonOutput);
1428
- }
1429
- return { success: true, data: jsonOutput };
1430
- }
1431
- if (format === 'dot') {
1432
- const dotOutput = analyzer.exportToDot(result, {
1433
- includeLabels: true,
1434
- highlightCycles: true,
1435
- });
1436
- if (outputFile) {
1437
- await safeWriteOutputFile(outputFile, dotOutput);
1438
- output.printSuccess(`DOT graph written to ${outputFile}`);
1439
- output.writeln(output.dim('Visualize with: dot -Tpng -o graph.png ' + outputFile));
1440
- }
1441
- else {
1442
- output.writeln(dotOutput);
1443
- }
1444
- return { success: true };
1445
- }
1446
- // Text format (default)
1447
- output.printBox([
1448
- `Files analyzed: ${result.statistics.nodeCount}`,
1449
- `Dependencies: ${result.statistics.edgeCount}`,
1450
- `Avg degree: ${result.statistics.avgDegree.toFixed(2)}`,
1451
- `Density: ${(result.statistics.density * 100).toFixed(2)}%`,
1452
- `Components: ${result.statistics.componentCount}`,
1453
- ].join('\n'), 'Graph Statistics');
1454
- if (result.boundaries && result.boundaries.length > 0) {
1455
- output.writeln();
1456
- output.writeln(output.bold('MinCut Boundaries'));
1457
- output.writeln();
1458
- for (let i = 0; i < result.boundaries.length; i++) {
1459
- const boundary = result.boundaries[i];
1460
- output.writeln(output.bold(`Boundary ${i + 1} (cut value: ${boundary.cutValue})`));
1461
- output.writeln();
1462
- output.writeln(output.dim('Partition 1:'));
1463
- const p1Display = boundary.partition1.slice(0, 10);
1464
- output.printList(p1Display);
1465
- if (boundary.partition1.length > 10) {
1466
- output.writeln(output.dim(` ... and ${boundary.partition1.length - 10} more files`));
1467
- }
1468
- output.writeln();
1469
- output.writeln(output.dim('Partition 2:'));
1470
- const p2Display = boundary.partition2.slice(0, 10);
1471
- output.printList(p2Display);
1472
- if (boundary.partition2.length > 10) {
1473
- output.writeln(output.dim(` ... and ${boundary.partition2.length - 10} more files`));
1474
- }
1475
- output.writeln();
1476
- output.writeln(output.success('Suggestion:'));
1477
- output.writeln(` ${boundary.suggestion}`);
1478
- output.writeln();
1479
- }
1480
- }
1481
- // Show circular dependencies
1482
- if (result.circularDependencies.length > 0) {
1483
- output.writeln();
1484
- output.writeln(output.bold(output.warning('Circular Dependencies Detected')));
1485
- output.writeln();
1486
- for (const cycle of result.circularDependencies.slice(0, 5)) {
1487
- const severityColor = cycle.severity === 'high' ? output.error : cycle.severity === 'medium' ? output.warning : output.dim;
1488
- output.writeln(`${severityColor(`[${cycle.severity.toUpperCase()}]`)} ${cycle.cycle.join(' -> ')}`);
1489
- output.writeln(output.dim(` ${cycle.suggestion}`));
1490
- output.writeln();
1491
- }
1492
- if (result.circularDependencies.length > 5) {
1493
- output.writeln(output.dim(`... and ${result.circularDependencies.length - 5} more cycles`));
1494
- }
1495
- }
1496
- if (outputFile) {
1497
- await safeWriteOutputFile(outputFile, JSON.stringify(result, null, 2));
1498
- output.printSuccess(`Full results written to ${outputFile}`);
1499
- }
1500
- return { success: true, data: result };
1501
- }
1502
- catch (error) {
1503
- spinner.stop();
1504
- const message = error instanceof Error ? error.message : String(error);
1505
- output.printError(`Analysis failed: ${message}`);
1506
- return { success: false, exitCode: 1 };
1507
- }
1508
- },
1509
- };
1510
- /**
1511
- * Analyze modules/communities using Louvain algorithm
1512
- */
1513
- const modulesCommand = {
1514
- name: 'modules',
1515
- aliases: ['communities', 'louvain'],
1516
- description: 'Detect module communities using Louvain algorithm',
1517
- options: [
1518
- {
1519
- name: 'output',
1520
- short: 'o',
1521
- description: 'Output file path',
1522
- type: 'string',
1523
- },
1524
- {
1525
- name: 'format',
1526
- short: 'f',
1527
- description: 'Output format (text, json, dot)',
1528
- type: 'string',
1529
- default: 'text',
1530
- choices: ['text', 'json', 'dot'],
1531
- },
1532
- {
1533
- name: 'min-size',
1534
- short: 'm',
1535
- description: 'Minimum community size to display',
1536
- type: 'number',
1537
- default: 2,
1538
- },
1539
- ],
1540
- examples: [
1541
- { command: 'monomind analyze modules src/', description: 'Detect module communities' },
1542
- { command: 'monomind analyze modules -f dot -o modules.dot src/', description: 'Export colored DOT graph' },
1543
- { command: 'monomind analyze modules -m 3 src/', description: 'Only show communities with 3+ files' },
1544
- ],
1545
- action: async (ctx) => {
1546
- const targetDir = ctx.args[0] || ctx.cwd;
1547
- const outputFile = ctx.flags.output;
1548
- const format = ctx.flags.format || 'text';
1549
- const minSize = ctx.flags['min-size'] || 2;
1550
- output.printInfo(`Detecting module communities in: ${output.highlight(targetDir)}`);
1551
- output.writeln();
1552
- const spinner = output.createSpinner({ text: 'Building dependency graph...', spinner: 'dots' });
1553
- spinner.start();
1554
- try {
1555
- const analyzer = await getGraphAnalyzer();
1556
- if (!analyzer) {
1557
- spinner.stop();
1558
- output.printError('Graph analyzer module not available');
1559
- return { success: false, exitCode: 1 };
1560
- }
1561
- const result = await analyzer.analyzeGraph(resolve(targetDir), {
1562
- includeBoundaries: false,
1563
- includeModules: true,
1564
- });
1565
- spinner.stop();
1566
- // Filter communities by size
1567
- const communities = result.communities?.filter(c => c.members.length >= minSize) || [];
1568
- // Handle different output formats
1569
- if (format === 'json') {
1570
- const jsonOutput = {
1571
- communities,
1572
- statistics: result.statistics,
1573
- };
1574
- if (outputFile) {
1575
- await safeWriteOutputFile(outputFile, JSON.stringify(jsonOutput, null, 2));
1576
- output.printSuccess(`Results written to ${outputFile}`);
1577
- }
1578
- else {
1579
- output.printJson(jsonOutput);
1580
- }
1581
- return { success: true, data: jsonOutput };
1582
- }
1583
- if (format === 'dot') {
1584
- const dotOutput = analyzer.exportToDot(result, {
1585
- includeLabels: true,
1586
- colorByCommunity: true,
1587
- highlightCycles: true,
1588
- });
1589
- if (outputFile) {
1590
- await safeWriteOutputFile(outputFile, dotOutput);
1591
- output.printSuccess(`DOT graph written to ${outputFile}`);
1592
- output.writeln(output.dim('Visualize with: dot -Tpng -o modules.png ' + outputFile));
1593
- }
1594
- else {
1595
- output.writeln(dotOutput);
1596
- }
1597
- return { success: true };
1598
- }
1599
- // Text format (default)
1600
- output.printBox([
1601
- `Files analyzed: ${result.statistics.nodeCount}`,
1602
- `Dependencies: ${result.statistics.edgeCount}`,
1603
- `Communities found: ${result.communities?.length || 0}`,
1604
- `Showing: ${communities.length} (min size: ${minSize})`,
1605
- ].join('\n'), 'Module Detection Results');
1606
- if (communities.length > 0) {
1607
- output.writeln();
1608
- output.writeln(output.bold('Detected Communities'));
1609
- output.writeln();
1610
- for (const community of communities.slice(0, 10)) {
1611
- const cohesionIndicator = community.cohesion > 0.5 ? output.success('High') :
1612
- community.cohesion > 0.2 ? output.warning('Medium') : output.dim('Low');
1613
- output.writeln(output.bold(`Community ${community.id}: ${community.suggestedName || 'unnamed'}`));
1614
- output.writeln(` ${output.dim('Cohesion:')} ${cohesionIndicator} (${(community.cohesion * 100).toFixed(1)}%)`);
1615
- output.writeln(` ${output.dim('Central node:')} ${community.centralNode || 'none'}`);
1616
- output.writeln(` ${output.dim('Members:')} ${community.members.length} files`);
1617
- const displayMembers = community.members.slice(0, 5);
1618
- for (const member of displayMembers) {
1619
- output.writeln(` - ${member}`);
1620
- }
1621
- if (community.members.length > 5) {
1622
- output.writeln(output.dim(` ... and ${community.members.length - 5} more`));
1623
- }
1624
- output.writeln();
1625
- }
1626
- if (communities.length > 10) {
1627
- output.writeln(output.dim(`... and ${communities.length - 10} more communities`));
1628
- }
1629
- }
1630
- if (outputFile) {
1631
- await safeWriteOutputFile(outputFile, JSON.stringify(result, null, 2));
1632
- output.printSuccess(`Full results written to ${outputFile}`);
1633
- }
1634
- return { success: true, data: result };
1635
- }
1636
- catch (error) {
1637
- spinner.stop();
1638
- const message = error instanceof Error ? error.message : String(error);
1639
- output.printError(`Analysis failed: ${message}`);
1640
- return { success: false, exitCode: 1 };
1641
- }
1642
- },
1643
- };
1644
- /**
1645
- * Build and export dependency graph
1646
- */
1647
- const dependenciesCommand = {
1648
- name: 'dependencies',
1649
- aliases: ['graph'],
1650
- description: 'Build and export full dependency graph',
1651
- options: [
1652
- {
1653
- name: 'output',
1654
- short: 'o',
1655
- description: 'Output file path',
1656
- type: 'string',
1657
- },
1658
- {
1659
- name: 'format',
1660
- short: 'f',
1661
- description: 'Output format (text, json, dot)',
1662
- type: 'string',
1663
- default: 'text',
1664
- choices: ['text', 'json', 'dot'],
1665
- },
1666
- {
1667
- name: 'include',
1668
- short: 'i',
1669
- description: 'File extensions to include (comma-separated)',
1670
- type: 'string',
1671
- default: '.ts,.tsx,.js,.jsx,.mjs,.cjs',
1672
- },
1673
- {
1674
- name: 'exclude',
1675
- short: 'e',
1676
- description: 'Patterns to exclude (comma-separated)',
1677
- type: 'string',
1678
- default: 'node_modules,dist,build,.git',
1679
- },
1680
- {
1681
- name: 'depth',
1682
- short: 'd',
1683
- description: 'Maximum directory depth',
1684
- type: 'number',
1685
- default: 10,
1686
- },
1687
- ],
1688
- examples: [
1689
- { command: 'monomind analyze dependencies src/', description: 'Build dependency graph' },
1690
- { command: 'monomind analyze dependencies -f dot -o deps.dot src/', description: 'Export to DOT' },
1691
- { command: 'monomind analyze dependencies -i .ts,.tsx src/', description: 'Only TypeScript files' },
1692
- ],
1693
- action: async (ctx) => {
1694
- const targetDir = ctx.args[0] || ctx.cwd;
1695
- const outputFile = ctx.flags.output;
1696
- const format = ctx.flags.format || 'text';
1697
- const include = (ctx.flags.include || '.ts,.tsx,.js,.jsx,.mjs,.cjs').split(',');
1698
- const exclude = (ctx.flags.exclude || 'node_modules,dist,build,.git').split(',');
1699
- const rawDepth = ctx.flags.depth || 10;
1700
- const maxDepth = Number.isFinite(rawDepth) ? Math.max(1, Math.min(rawDepth, 50)) : 10;
1701
- output.printInfo(`Building dependency graph for: ${output.highlight(targetDir)}`);
1702
- output.writeln();
1703
- const spinner = output.createSpinner({ text: 'Scanning files...', spinner: 'dots' });
1704
- spinner.start();
1705
- try {
1706
- const analyzer = await getGraphAnalyzer();
1707
- if (!analyzer) {
1708
- spinner.stop();
1709
- output.printError('Graph analyzer module not available');
1710
- return { success: false, exitCode: 1 };
1711
- }
1712
- const graph = await analyzer.buildDependencyGraph(resolve(targetDir), {
1713
- include,
1714
- exclude,
1715
- maxDepth,
1716
- });
1717
- spinner.stop();
1718
- // Detect circular dependencies
1719
- const circularDeps = analyzer.detectCircularDependencies(graph);
1720
- // Handle different output formats
1721
- if (format === 'json') {
1722
- const jsonOutput = {
1723
- nodes: Array.from(graph.nodes.values()),
1724
- edges: graph.edges,
1725
- metadata: graph.metadata,
1726
- circularDependencies: circularDeps,
1727
- };
1728
- if (outputFile) {
1729
- await safeWriteOutputFile(outputFile, JSON.stringify(jsonOutput, null, 2));
1730
- output.printSuccess(`Graph written to ${outputFile}`);
1731
- }
1732
- else {
1733
- output.printJson(jsonOutput);
1734
- }
1735
- return { success: true, data: jsonOutput };
1736
- }
1737
- if (format === 'dot') {
1738
- const result = { graph, circularDependencies: circularDeps, statistics: {
1739
- nodeCount: graph.nodes.size,
1740
- edgeCount: graph.edges.length,
1741
- avgDegree: 0,
1742
- maxDegree: 0,
1743
- density: 0,
1744
- componentCount: 0,
1745
- } };
1746
- const dotOutput = analyzer.exportToDot(result, {
1747
- includeLabels: true,
1748
- highlightCycles: true,
1749
- });
1750
- if (outputFile) {
1751
- await safeWriteOutputFile(outputFile, dotOutput);
1752
- output.printSuccess(`DOT graph written to ${outputFile}`);
1753
- output.writeln(output.dim('Visualize with: dot -Tpng -o deps.png ' + outputFile));
1754
- }
1755
- else {
1756
- output.writeln(dotOutput);
1757
- }
1758
- return { success: true };
1759
- }
1760
- // Text format (default)
1761
- output.printBox([
1762
- `Files: ${graph.metadata.totalFiles}`,
1763
- `Dependencies: ${graph.metadata.totalEdges}`,
1764
- `Build time: ${graph.metadata.buildTime}ms`,
1765
- `Root: ${graph.metadata.rootDir}`,
1766
- ].join('\n'), 'Dependency Graph');
1767
- // Show top files by imports
1768
- output.writeln();
1769
- output.writeln(output.bold('Most Connected Files'));
1770
- output.writeln();
1771
- const nodesByDegree = Array.from(graph.nodes.values())
1772
- .map((n) => ({
1773
- ...n,
1774
- degree: graph.edges.filter((e) => e.source === n.id || e.target === n.id).length,
1775
- }))
1776
- .sort((a, b) => b.degree - a.degree)
1777
- .slice(0, 10);
1778
- output.printTable({
1779
- columns: [
1780
- { key: 'path', header: 'File', width: 50 },
1781
- { key: 'degree', header: 'Connections', width: 12, align: 'right' },
1782
- { key: 'complexity', header: 'Complexity', width: 12, align: 'right' },
1783
- ],
1784
- data: nodesByDegree.map(n => ({
1785
- path: n.path.length > 48 ? '...' + n.path.slice(-45) : n.path,
1786
- degree: n.degree,
1787
- complexity: n.complexity || 0,
1788
- })),
1789
- });
1790
- // Show circular dependencies
1791
- if (circularDeps.length > 0) {
1792
- output.writeln();
1793
- output.writeln(output.bold(output.warning(`Circular Dependencies: ${circularDeps.length}`)));
1794
- output.writeln();
1795
- for (const cycle of circularDeps.slice(0, 3)) {
1796
- output.writeln(` ${cycle.cycle.join(' -> ')}`);
1797
- }
1798
- if (circularDeps.length > 3) {
1799
- output.writeln(output.dim(` ... and ${circularDeps.length - 3} more`));
1800
- }
1801
- }
1802
- if (outputFile) {
1803
- const fullOutput = {
1804
- nodes: Array.from(graph.nodes.values()),
1805
- edges: graph.edges,
1806
- metadata: graph.metadata,
1807
- circularDependencies: circularDeps,
1808
- };
1809
- await safeWriteOutputFile(outputFile, JSON.stringify(fullOutput, null, 2));
1810
- output.printSuccess(`Full results written to ${outputFile}`);
1811
- }
1812
- return { success: true };
1813
- }
1814
- catch (error) {
1815
- spinner.stop();
1816
- const message = error instanceof Error ? error.message : String(error);
1817
- output.printError(`Analysis failed: ${message}`);
1818
- return { success: false, exitCode: 1 };
1819
- }
1820
- },
1821
- };
1822
- /**
1823
- * Detect circular dependencies
1824
- */
1825
- const circularCommand = {
1826
- name: 'circular',
1827
- aliases: ['cycles'],
1828
- description: 'Detect circular dependencies in codebase',
1829
- options: [
1830
- {
1831
- name: 'output',
1832
- short: 'o',
1833
- description: 'Output file path',
1834
- type: 'string',
1835
- },
1836
- {
1837
- name: 'format',
1838
- short: 'f',
1839
- description: 'Output format (text, json)',
1840
- type: 'string',
1841
- default: 'text',
1842
- choices: ['text', 'json'],
1843
- },
1844
- {
1845
- name: 'severity',
1846
- short: 's',
1847
- description: 'Minimum severity to show (low, medium, high)',
1848
- type: 'string',
1849
- default: 'low',
1850
- choices: ['low', 'medium', 'high'],
1851
- },
1852
- ],
1853
- examples: [
1854
- { command: 'monomind analyze circular src/', description: 'Find circular dependencies' },
1855
- { command: 'monomind analyze circular -s high src/', description: 'Only high severity cycles' },
1856
- ],
1857
- action: async (ctx) => {
1858
- const targetDir = ctx.args[0] || ctx.cwd;
1859
- const outputFile = ctx.flags.output;
1860
- const format = ctx.flags.format || 'text';
1861
- const minSeverity = ctx.flags.severity || 'low';
1862
- output.printInfo(`Detecting circular dependencies in: ${output.highlight(targetDir)}`);
1863
- output.writeln();
1864
- const spinner = output.createSpinner({ text: 'Analyzing dependencies...', spinner: 'dots' });
1865
- spinner.start();
1866
- try {
1867
- const analyzer = await getGraphAnalyzer();
1868
- if (!analyzer) {
1869
- spinner.stop();
1870
- output.printError('Graph analyzer module not available');
1871
- return { success: false, exitCode: 1 };
1872
- }
1873
- const graph = await analyzer.buildDependencyGraph(resolve(targetDir));
1874
- const cycles = analyzer.detectCircularDependencies(graph);
1875
- spinner.stop();
1876
- // Filter by severity
1877
- const severityOrder = { low: 0, medium: 1, high: 2 };
1878
- const minLevel = severityOrder[minSeverity] || 0;
1879
- const filtered = cycles.filter(c => severityOrder[c.severity] >= minLevel);
1880
- if (format === 'json') {
1881
- const jsonOutput = { cycles: filtered, total: cycles.length, filtered: filtered.length };
1882
- if (outputFile) {
1883
- await safeWriteOutputFile(outputFile, JSON.stringify(jsonOutput, null, 2));
1884
- output.printSuccess(`Results written to ${outputFile}`);
1885
- }
1886
- else {
1887
- output.printJson(jsonOutput);
1888
- }
1889
- return { success: true, data: jsonOutput };
1890
- }
1891
- // Text format
1892
- if (filtered.length === 0) {
1893
- output.printSuccess('No circular dependencies found!');
1894
- return { success: true };
1895
- }
1896
- output.printBox([
1897
- `Total cycles: ${cycles.length}`,
1898
- `Shown (${minSeverity}+): ${filtered.length}`,
1899
- `High severity: ${cycles.filter(c => c.severity === 'high').length}`,
1900
- `Medium severity: ${cycles.filter(c => c.severity === 'medium').length}`,
1901
- `Low severity: ${cycles.filter(c => c.severity === 'low').length}`,
1902
- ].join('\n'), 'Circular Dependencies');
1903
- output.writeln();
1904
- // Group by severity
1905
- const grouped = {
1906
- high: filtered.filter(c => c.severity === 'high'),
1907
- medium: filtered.filter(c => c.severity === 'medium'),
1908
- low: filtered.filter(c => c.severity === 'low'),
1909
- };
1910
- for (const [severity, items] of Object.entries(grouped)) {
1911
- if (items.length === 0)
1912
- continue;
1913
- const color = severity === 'high' ? output.error : severity === 'medium' ? output.warning : output.dim;
1914
- output.writeln(color(output.bold(`${severity.toUpperCase()} SEVERITY (${items.length})`)));
1915
- output.writeln();
1916
- for (const cycle of items.slice(0, 5)) {
1917
- output.writeln(` ${cycle.cycle.join(' -> ')}`);
1918
- output.writeln(output.dim(` Fix: ${cycle.suggestion}`));
1919
- output.writeln();
1920
- }
1921
- if (items.length > 5) {
1922
- output.writeln(output.dim(` ... and ${items.length - 5} more ${severity} cycles`));
1923
- output.writeln();
1924
- }
1925
- }
1926
- if (outputFile) {
1927
- await safeWriteOutputFile(outputFile, JSON.stringify({ cycles: filtered }, null, 2));
1928
- output.printSuccess(`Results written to ${outputFile}`);
1929
- }
1930
- return { success: true, data: { cycles: filtered } };
1931
- }
1932
- catch (error) {
1933
- spinner.stop();
1934
- const message = error instanceof Error ? error.message : String(error);
1935
- output.printError(`Analysis failed: ${message}`);
1936
- return { success: false, exitCode: 1 };
1937
- }
1938
- },
1939
- };
1940
- // Helper functions
1941
- function getRiskDisplay(risk) {
1942
- switch (risk) {
1943
- case 'critical':
1944
- return output.color(output.bold('CRITICAL'), 'bgRed', 'white');
1945
- case 'high-risk':
1946
- return output.error('HIGH');
1947
- case 'medium-risk':
1948
- return output.warning('MEDIUM');
1949
- case 'low-risk':
1950
- return output.success('LOW');
1951
- default:
1952
- return risk;
1953
- }
1954
- }
1955
- function getStatusDisplay(status) {
1956
- switch (status) {
1957
- case 'added':
1958
- return output.success('A');
1959
- case 'modified':
1960
- return output.warning('M');
1961
- case 'deleted':
1962
- return output.error('D');
1963
- case 'renamed':
1964
- return output.info('R');
1965
- default:
1966
- return status;
1967
- }
1968
- }
1969
154
  // Main analyze command
1970
155
  export const analyzeCommand = {
1971
156
  name: 'analyze',
@@ -2012,7 +197,7 @@ export const analyzeCommand = {
2012
197
  { command: 'monomind analyze circular src/', description: 'Find circular dependencies' },
2013
198
  { command: 'monomind analyze deps --security', description: 'Check dependency vulnerabilities' },
2014
199
  ],
2015
- action: async (ctx) => {
200
+ action: async (_ctx) => {
2016
201
  // If no subcommand, show help
2017
202
  output.writeln();
2018
203
  output.writeln(output.bold('Analyze Commands'));