|
| 1 | +/** |
| 2 | + * sentry debug-files bundle-jvm <path> |
| 3 | + * |
| 4 | + * Create a JVM source bundle from a directory of JVM source files. |
| 5 | + * The bundle is a ZIP archive that can be uploaded to Sentry via |
| 6 | + * `debug-files upload --type jvm` for source context in stack traces. |
| 7 | + * |
| 8 | + * This command is local-only — it makes no API calls. |
| 9 | + */ |
| 10 | + |
| 11 | +import { mkdir, stat } from "node:fs/promises"; |
| 12 | +import { join, resolve } from "node:path"; |
| 13 | +import type { SentryContext } from "../../context.js"; |
| 14 | +import { buildCommand } from "../../lib/command.js"; |
| 15 | +import { ValidationError } from "../../lib/errors.js"; |
| 16 | +import { mdKvTable, renderMarkdown } from "../../lib/formatters/markdown.js"; |
| 17 | +import { CommandOutput } from "../../lib/formatters/output.js"; |
| 18 | +import { buildJvmBundle } from "../../lib/jvm-bundle.js"; |
| 19 | +import { logger } from "../../lib/logger.js"; |
| 20 | + |
| 21 | +const log = logger.withTag("debug-files.bundle-jvm"); |
| 22 | + |
| 23 | +/** UUID format: 8-4-4-4-12 hex with hyphens. */ |
| 24 | +const UUID_RE = |
| 25 | + /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i; |
| 26 | + |
| 27 | +/** Data shape yielded by the command and rendered by both human and JSON modes. */ |
| 28 | +type BundleJvmResult = { |
| 29 | + outputPath: string; |
| 30 | + debugId: string; |
| 31 | + fileCount: number; |
| 32 | + collisionCount: number; |
| 33 | +}; |
| 34 | + |
| 35 | +/** Human-readable formatter for the bundle result. */ |
| 36 | +function formatBundleResult(data: BundleJvmResult): string { |
| 37 | + const rows: [string, string][] = [ |
| 38 | + ["Output", data.outputPath], |
| 39 | + ["Debug ID", data.debugId], |
| 40 | + ["Files bundled", String(data.fileCount)], |
| 41 | + ]; |
| 42 | + |
| 43 | + if (data.collisionCount > 0) { |
| 44 | + rows.push(["URL collisions", String(data.collisionCount)]); |
| 45 | + } |
| 46 | + |
| 47 | + return renderMarkdown(mdKvTable(rows)); |
| 48 | +} |
| 49 | + |
| 50 | +export const bundleJvmCommand = buildCommand({ |
| 51 | + auth: false, |
| 52 | + docs: { |
| 53 | + brief: "Create a JVM source bundle for source context", |
| 54 | + fullDescription: |
| 55 | + "Create a JVM source bundle from a directory of Java, Kotlin, Scala, " + |
| 56 | + "Groovy, or Clojure source files. The bundle is a ZIP archive that " + |
| 57 | + "can be uploaded to Sentry for source context in JVM stack traces.\n\n" + |
| 58 | + "This command is local-only — it makes no network requests.", |
| 59 | + }, |
| 60 | + output: { |
| 61 | + human: formatBundleResult, |
| 62 | + }, |
| 63 | + parameters: { |
| 64 | + positional: { |
| 65 | + kind: "tuple", |
| 66 | + parameters: [ |
| 67 | + { |
| 68 | + brief: "Directory containing JVM source files", |
| 69 | + parse: String, |
| 70 | + placeholder: "path", |
| 71 | + }, |
| 72 | + ], |
| 73 | + }, |
| 74 | + flags: { |
| 75 | + output: { |
| 76 | + kind: "parsed", |
| 77 | + parse: String, |
| 78 | + brief: "Output directory for the bundle ZIP", |
| 79 | + }, |
| 80 | + "debug-id": { |
| 81 | + kind: "parsed", |
| 82 | + parse: String, |
| 83 | + brief: "Debug ID (UUID) to stamp on the bundle", |
| 84 | + }, |
| 85 | + exclude: { |
| 86 | + kind: "parsed", |
| 87 | + parse: String, |
| 88 | + brief: "Additional directory names to exclude (repeatable)", |
| 89 | + optional: true, |
| 90 | + variadic: true, |
| 91 | + }, |
| 92 | + }, |
| 93 | + aliases: { |
| 94 | + o: "output", |
| 95 | + d: "debug-id", |
| 96 | + e: "exclude", |
| 97 | + }, |
| 98 | + }, |
| 99 | + async *func( |
| 100 | + this: SentryContext, |
| 101 | + flags: { |
| 102 | + output: string; |
| 103 | + "debug-id": string; |
| 104 | + exclude?: string[]; |
| 105 | + }, |
| 106 | + sourcePath: string |
| 107 | + ) { |
| 108 | + // 1. Validate debug ID format |
| 109 | + if (!UUID_RE.test(flags["debug-id"])) { |
| 110 | + throw new ValidationError( |
| 111 | + `Invalid debug ID format: '${flags["debug-id"]}'. ` + |
| 112 | + "Expected UUID format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", |
| 113 | + "debug-id" |
| 114 | + ); |
| 115 | + } |
| 116 | + |
| 117 | + // 2. Validate source path exists and is a directory |
| 118 | + const resolvedSource = resolve(sourcePath); |
| 119 | + const srcStat = await stat(resolvedSource).catch(() => { |
| 120 | + throw new ValidationError( |
| 121 | + `Source path '${sourcePath}' does not exist.`, |
| 122 | + "path" |
| 123 | + ); |
| 124 | + }); |
| 125 | + if (!srcStat.isDirectory()) { |
| 126 | + throw new ValidationError( |
| 127 | + `Source path '${sourcePath}' is not a directory.`, |
| 128 | + "path" |
| 129 | + ); |
| 130 | + } |
| 131 | + |
| 132 | + // 3. Create output directory if needed |
| 133 | + const resolvedOutput = resolve(flags.output); |
| 134 | + await mkdir(resolvedOutput, { recursive: true }); |
| 135 | + |
| 136 | + // 4. Build the bundle |
| 137 | + const outputFile = join(resolvedOutput, `${flags["debug-id"]}.zip`); |
| 138 | + |
| 139 | + const result = await buildJvmBundle({ |
| 140 | + sourcePath: resolvedSource, |
| 141 | + outputPath: outputFile, |
| 142 | + debugId: flags["debug-id"], |
| 143 | + excludePatterns: flags.exclude, |
| 144 | + }); |
| 145 | + |
| 146 | + if (result.fileCount === 0) { |
| 147 | + log.warn("No JVM source files found in the given directory."); |
| 148 | + } |
| 149 | + |
| 150 | + // 5. Yield result |
| 151 | + yield new CommandOutput<BundleJvmResult>({ |
| 152 | + outputPath: result.outputPath, |
| 153 | + debugId: flags["debug-id"], |
| 154 | + fileCount: result.fileCount, |
| 155 | + collisionCount: result.collisionCount, |
| 156 | + }); |
| 157 | + |
| 158 | + return { |
| 159 | + hint: `Created ${outputFile} with ${result.fileCount} source file(s)`, |
| 160 | + }; |
| 161 | + }, |
| 162 | +}); |
0 commit comments