|
1
|
import { readFileSync, writeFileSync } from "fs" |
|
2
|
import chalk from "chalk" |
|
3
|
import { Command } from "commander" |
|
4
|
import { WebhooksContextBuilder } from "../classes/webhooks-context-builder.js" |
|
5
|
import { |
|
6
|
AnalyzeWebhooksOptions, |
|
7
|
WebhooksDispatchPayload, |
|
8
|
} from "../types/index.js" |
|
9
|
|
|
10
|
export function registerAnalyzeWebhooksCommand(program: Command) { |
|
11
|
program |
|
12
|
.command("analyze-webhooks") |
|
13
|
.description( |
|
14
|
"Build a documentation update prompt for Claude from a Cloud webhooks dispatch payload" |
|
15
|
) |
|
16
|
.requiredOption( |
|
17
|
"--dispatch-file <path>", |
|
18
|
"Path to JSON file containing the WebhooksDispatchPayload" |
|
19
|
) |
|
20
|
.option( |
|
21
|
"--output <path>", |
|
22
|
"Write the analysis JSON to this file path (default: stdout)" |
|
23
|
) |
|
24
|
.option("--dry-run", "Print prompt preview without writing output") |
|
25
|
.action(async (options: AnalyzeWebhooksOptions) => { |
|
26
|
try { |
|
27
|
await runAnalyzeWebhooks(options) |
|
28
|
} catch (error) { |
|
29
|
console.error(chalk.red("Error during webhooks analysis:"), error) |
|
30
|
process.exit(1) |
|
31
|
} |
|
32
|
}) |
|
33
|
} |
|
34
|
|
|
35
|
async function runAnalyzeWebhooks(options: AnalyzeWebhooksOptions) { |
|
36
|
const { dispatchFile, output, dryRun } = options |
|
37
|
|
|
38
|
console.error(chalk.blue(`Reading dispatch payload from: ${dispatchFile}`)) |
|
39
|
|
|
40
|
const raw = readFileSync(dispatchFile, "utf8") |
|
41
|
const payload: WebhooksDispatchPayload = JSON.parse(raw) |
|
42
|
|
|
43
|
const events = payload.webhooks ?? [] |
|
44
|
const hasDescriptions = !!payload.descriptions?.trim() |
|
45
|
|
|
46
|
if (!events.length && !hasDescriptions) { |
|
47
|
console.error(chalk.yellow("No webhook changes found in dispatch payload.")) |
|
48
|
if (output) { |
|
49
|
writeFileSync( |
|
50
|
output, |
|
51
|
JSON.stringify( |
|
52
|
{ |
|
53
|
affectedProjects: [], |
|
54
|
claudePrompt: "", |
|
55
|
featureFlaggedFeatures: [], |
|
56
|
changelogDate: "", |
|
57
|
changeSummary: [], |
|
58
|
}, |
|
59
|
null, |
|
60
|
2 |
|
61
|
) |
|
62
|
) |
|
63
|
} |
|
64
|
process.exit(2) |
|
65
|
} |
|
66
|
|
|
67
|
const invalid = events.filter((event) => !event.event || !event.changeType) |
|
68
|
if (invalid.length) { |
|
69
|
throw new Error( |
|
70
|
`Every webhook entry must have an \`event\` and a \`changeType\`. Invalid entries: ${JSON.stringify(invalid)}` |
|
71
|
) |
|
72
|
} |
|
73
|
|
|
74
|
console.log(chalk.green(`Building webhooks docs prompt`)) |
|
75
|
|
|
76
|
const builder = new WebhooksContextBuilder() |
|
77
|
// The automation runs right after the deployment, so today's date is the |
|
78
|
// release date unless the payload says otherwise. |
|
79
|
const result = builder.build(payload, new Date()) |
|
80
|
|
|
81
|
if (dryRun) { |
|
82
|
console.log(chalk.cyan("Dry run — prompt preview:")) |
|
83
|
console.log(result.claudePrompt) |
|
84
|
return |
|
85
|
} |
|
86
|
|
|
87
|
const json = JSON.stringify(result, null, 2) |
|
88
|
|
|
89
|
if (output) { |
|
90
|
writeFileSync(output, json) |
|
91
|
console.log(chalk.green(`Analysis written to: ${output}`)) |
|
92
|
} else { |
|
93
|
process.stdout.write(json) |
|
94
|
} |
|
95
|
} |