1 import { readFileSync, writeFileSync } from "fs"
2 import chalk from "chalk"
3 import { Command } from "commander"
4 import { CloudContextBuilder } from "../classes/cloud-context-builder.js"
5 import { AnalyzeCloudOptions, CloudDispatchPayload } from "../types/index.js"
6
7 export function registerAnalyzeCloudCommand(program: Command) {
8 program
9 .command("analyze-cloud")
10 .description(
11 "Build a documentation update prompt for Claude from a cloud deployment dispatch payload"
12 )
13 .requiredOption(
14 "--dispatch-file <path>",
15 "Path to JSON file containing the CloudDispatchPayload"
16 )
17 .option(
18 "--output <path>",
19 "Write the analysis JSON to this file path (default: stdout)"
20 )
21 .option("--dry-run", "Print prompt preview without writing output")
22 .action(async (options: AnalyzeCloudOptions) => {
23 try {
24 await runAnalyzeCloud(options)
25 } catch (error) {
26 console.error(chalk.red("Error during cloud analysis:"), error)
27 process.exit(1)
28 }
29 })
30 }
31
32 async function runAnalyzeCloud(options: AnalyzeCloudOptions) {
33 const { dispatchFile, output, dryRun } = options
34
35 console.error(chalk.blue(`Reading dispatch payload from: ${dispatchFile}`))
36
37 const raw = readFileSync(dispatchFile, "utf8")
38 const payload: CloudDispatchPayload = JSON.parse(raw)
39
40 const hasDescriptions = !!payload.descriptions?.trim()
41 const hasChangelog = !!payload.releaseNotes?.trim() && !!payload.version
42
43 if (!hasDescriptions && !hasChangelog) {
44 console.error(
45 chalk.yellow(
46 "No feature descriptions or changelog found in dispatch payload."
47 )
48 )
49 if (output) {
50 writeFileSync(
51 output,
52 JSON.stringify(
53 {
54 affectedProjects: [],
55 claudePrompt: "",
56 featureFlaggedFeatures: [],
57 },
58 null,
59 2
60 )
61 )
62 }
63 process.exit(2)
64 }
65
66 if (payload.releaseNotes?.trim() && !payload.version) {
67 console.error(
68 chalk.yellow(
69 "Release notes present but no version provided — skipping changelog step."
70 )
71 )
72 }
73
74 console.log(chalk.green(`Building cloud docs prompt`))
75
76 // The automation runs right after a deployment, so the current date is the
77 // release date shown alongside the version in the changelog.
78 const now = new Date()
79 const releaseDate = now.toLocaleDateString("en-US", {
80 year: "numeric",
81 month: "long",
82 day: "numeric",
83 timeZone: "UTC",
84 })
85 // The dashboard changelog stores each date as a `YYYY-MM-DD.mjs` entry file,
86 // so the same date is also needed in ISO form.
87 const isoReleaseDate = now.toISOString().split("T")[0]
88
89 const builder = new CloudContextBuilder()
90 const result = builder.build(payload, releaseDate, isoReleaseDate)
91
92 if (dryRun) {
93 console.log(chalk.cyan("Dry run — prompt preview:"))
94 console.log(result.claudePrompt.slice(0, 500) + "...")
95 return
96 }
97
98 const json = JSON.stringify(result, null, 2)
99
100 if (output) {
101 writeFileSync(output, json)
102 console.log(chalk.green(`Analysis written to: ${output}`))
103 } else {
104 process.stdout.write(json)
105 }
106 }