|
1
|
import { writeFileSync } from "fs" |
|
2
|
import chalk from "chalk" |
|
3
|
import { Command } from "commander" |
|
4
|
import { ContextBuilder } from "../classes/context-builder.js" |
|
5
|
import { DiffAnalyzer } from "../classes/diff-analyzer.js" |
|
6
|
import { DocMapper } from "../classes/doc-mapper.js" |
|
7
|
import { AnalyzeOptions } from "../types/index.js" |
|
8
|
|
|
9
|
export function registerAnalyzeCommand(program: Command) { |
|
10
|
program |
|
11
|
.command("analyze") |
|
12
|
.description( |
|
13
|
"Analyze a commit's changes to packages/ and build documentation update context for Claude" |
|
14
|
) |
|
15
|
.requiredOption("--commit-sha <sha>", "The commit SHA to analyze") |
|
16
|
.option( |
|
17
|
"--output <path>", |
|
18
|
"Write the analysis JSON to this file path (default: stdout)" |
|
19
|
) |
|
20
|
.option("--dry-run", "Print analysis without writing output") |
|
21
|
.action(async (options: AnalyzeOptions) => { |
|
22
|
try { |
|
23
|
await runAnalyze(options) |
|
24
|
} catch (error) { |
|
25
|
console.error(chalk.red("Error during analysis:"), error) |
|
26
|
process.exit(1) |
|
27
|
} |
|
28
|
}) |
|
29
|
} |
|
30
|
|
|
31
|
async function runAnalyze(options: AnalyzeOptions) { |
|
32
|
const { commitSha, output, dryRun } = options |
|
33
|
|
|
34
|
console.error(chalk.blue(`Analyzing commit: ${commitSha}`)) |
|
35
|
|
|
36
|
const analyzer = new DiffAnalyzer() |
|
37
|
const changedPackages = await analyzer.analyze(commitSha) |
|
38
|
|
|
39
|
if (changedPackages.length === 0) { |
|
40
|
console.error( |
|
41
|
chalk.yellow("No doc-relevant package changes found in this commit.") |
|
42
|
) |
|
43
|
if (output) { |
|
44
|
writeFileSync( |
|
45
|
output, |
|
46
|
JSON.stringify( |
|
47
|
{ |
|
48
|
changedPackages: [], |
|
49
|
affectedProjects: [], |
|
50
|
claudePrompt: "", |
|
51
|
triggerCommitSha: commitSha, |
|
52
|
}, |
|
53
|
null, |
|
54
|
2 |
|
55
|
) |
|
56
|
) |
|
57
|
} |
|
58
|
process.exit(2) |
|
59
|
} |
|
60
|
|
|
61
|
console.error( |
|
62
|
chalk.green(`Found ${changedPackages.length} changed package(s):`) + |
|
63
|
changedPackages.map((p) => `\n - ${p.path} (${p.kind})`).join("") |
|
64
|
) |
|
65
|
|
|
66
|
const mapper = new DocMapper() |
|
67
|
const affectedProjects = mapper.map(changedPackages) |
|
68
|
|
|
69
|
console.error( |
|
70
|
chalk.green(`Affected doc projects:`) + |
|
71
|
affectedProjects.map((p) => `\n - ${p.project}`).join("") |
|
72
|
) |
|
73
|
|
|
74
|
const builder = new ContextBuilder() |
|
75
|
const result = builder.build(changedPackages, affectedProjects, commitSha) |
|
76
|
|
|
77
|
if (dryRun) { |
|
78
|
console.error(chalk.cyan("Dry run — prompt preview:")) |
|
79
|
console.error(result.claudePrompt.slice(0, 500) + "...") |
|
80
|
return |
|
81
|
} |
|
82
|
|
|
83
|
const json = JSON.stringify(result, null, 2) |
|
84
|
|
|
85
|
if (output) { |
|
86
|
writeFileSync(output, json) |
|
87
|
console.error(chalk.green(`Analysis written to: ${output}`)) |
|
88
|
} else { |
|
89
|
process.stdout.write(json) |
|
90
|
} |
|
91
|
} |