|
| 1 | +import { requireComputeAuth } from "../lib/auth/guard"; |
| 2 | +import { authRequiredError, CliError } from "../shell/errors"; |
| 3 | +import { writeJsonEvent } from "../shell/output"; |
| 4 | +import type { CommandContext } from "../shell/runtime"; |
| 5 | + |
| 6 | +/** |
| 7 | + * One line of `GET /v1/builds/{buildId}/logs` (the `BuildLogNdjsonLine` schema). |
| 8 | + * Build logs are a separate system from runtime `app logs`: this stream is keyed |
| 9 | + * by `Build.id` (a git-push / Console build), not a compute version. |
| 10 | + */ |
| 11 | +type BuildLogRecord = |
| 12 | + | { |
| 13 | + type: "log"; |
| 14 | + text: string; |
| 15 | + level: "info" | "error"; |
| 16 | + source: "runner" | "stdout" | "stderr"; |
| 17 | + step?: string; |
| 18 | + cursor: string; |
| 19 | + } |
| 20 | + | { |
| 21 | + type: "terminal"; |
| 22 | + kind: "end" | "error"; |
| 23 | + code: string; |
| 24 | + message: string; |
| 25 | + retryable: boolean; |
| 26 | + cursor: string | null; |
| 27 | + }; |
| 28 | + |
| 29 | +export interface BuildLogsOptions { |
| 30 | + follow?: boolean; |
| 31 | + cursor?: string; |
| 32 | +} |
| 33 | + |
| 34 | +export async function runBuildLogs( |
| 35 | + context: CommandContext, |
| 36 | + buildId: string, |
| 37 | + options: BuildLogsOptions = {}, |
| 38 | +): Promise<void> { |
| 39 | + const client = await requireComputeAuth( |
| 40 | + context.runtime.env, |
| 41 | + context.runtime.signal, |
| 42 | + ); |
| 43 | + if (!client) { |
| 44 | + throw authRequiredError(["prisma-cli auth login"]); |
| 45 | + } |
| 46 | + |
| 47 | + if (!context.flags.json && !context.flags.quiet) { |
| 48 | + context.output.stderr.write( |
| 49 | + `build logs → Streaming logs for build ${buildId}\n\n`, |
| 50 | + ); |
| 51 | + } |
| 52 | + |
| 53 | + const { data, response } = await client.GET("/v1/builds/{buildId}/logs", { |
| 54 | + params: { |
| 55 | + path: { buildId }, |
| 56 | + query: { |
| 57 | + ...(options.follow ? { follow: "true" as const } : {}), |
| 58 | + ...(options.cursor ? { cursor: options.cursor } : {}), |
| 59 | + }, |
| 60 | + }, |
| 61 | + parseAs: "stream", |
| 62 | + signal: context.runtime.signal, |
| 63 | + }); |
| 64 | + |
| 65 | + const body = data as ReadableStream<Uint8Array> | null | undefined; |
| 66 | + if (!response.ok || !body) { |
| 67 | + throw buildLogsRequestError(buildId, response.status); |
| 68 | + } |
| 69 | + |
| 70 | + let sawError = false; |
| 71 | + await forEachNdjsonRecord<BuildLogRecord>(body, (record) => { |
| 72 | + if (record.type === "terminal" && record.kind === "error") { |
| 73 | + sawError = true; |
| 74 | + } |
| 75 | + writeBuildLogRecord(context, record); |
| 76 | + }); |
| 77 | + |
| 78 | + if (sawError) { |
| 79 | + process.exitCode = 1; |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +function writeBuildLogRecord( |
| 84 | + context: CommandContext, |
| 85 | + record: BuildLogRecord, |
| 86 | +): void { |
| 87 | + if (context.flags.json) { |
| 88 | + writeJsonEvent(context.output, { |
| 89 | + type: record.type, |
| 90 | + command: "build.logs", |
| 91 | + timestamp: new Date().toISOString(), |
| 92 | + data: record, |
| 93 | + }); |
| 94 | + return; |
| 95 | + } |
| 96 | + |
| 97 | + if (record.type === "log") { |
| 98 | + context.output.stdout.write(record.text); |
| 99 | + if (!record.text.endsWith("\n")) { |
| 100 | + context.output.stdout.write("\n"); |
| 101 | + } |
| 102 | + return; |
| 103 | + } |
| 104 | + |
| 105 | + // A terminal `end` is the normal stream close — nothing to print. A `no_logs` |
| 106 | + // end or any error terminal carries a message the user should see. |
| 107 | + if (record.code !== "end") { |
| 108 | + context.output.stderr.write(`${record.message}\n`); |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +/** Reads a newline-delimited JSON body line by line, parsing each into a record. */ |
| 113 | +async function forEachNdjsonRecord<T>( |
| 114 | + body: ReadableStream<Uint8Array>, |
| 115 | + onRecord: (record: T) => void, |
| 116 | +): Promise<void> { |
| 117 | + const reader = body.getReader(); |
| 118 | + const decoder = new TextDecoder(); |
| 119 | + let buffer = ""; |
| 120 | + |
| 121 | + for (;;) { |
| 122 | + // biome-ignore lint/performance/noAwaitInLoops: a stream must be read sequentially, chunk by chunk. |
| 123 | + const { done, value } = await reader.read(); |
| 124 | + if (value) { |
| 125 | + buffer += decoder.decode(value, { stream: true }); |
| 126 | + } |
| 127 | + |
| 128 | + let newlineIndex = buffer.indexOf("\n"); |
| 129 | + while (newlineIndex !== -1) { |
| 130 | + const line = buffer.slice(0, newlineIndex).trim(); |
| 131 | + buffer = buffer.slice(newlineIndex + 1); |
| 132 | + if (line) { |
| 133 | + onRecord(JSON.parse(line) as T); |
| 134 | + } |
| 135 | + newlineIndex = buffer.indexOf("\n"); |
| 136 | + } |
| 137 | + |
| 138 | + if (done) { |
| 139 | + const tail = buffer.trim(); |
| 140 | + if (tail) { |
| 141 | + onRecord(JSON.parse(tail) as T); |
| 142 | + } |
| 143 | + return; |
| 144 | + } |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +function buildLogsRequestError(buildId: string, status: number): CliError { |
| 149 | + if (status === 404) { |
| 150 | + return new CliError({ |
| 151 | + code: "BUILD_NOT_FOUND", |
| 152 | + domain: "app", |
| 153 | + summary: `Build ${buildId} was not found`, |
| 154 | + why: "The build does not exist, or your workspace does not have access to it.", |
| 155 | + fix: "Check the build id, or run prisma-cli auth login to switch to the workspace that owns it.", |
| 156 | + exitCode: 1, |
| 157 | + }); |
| 158 | + } |
| 159 | + return new CliError({ |
| 160 | + code: "BUILD_LOGS_FAILED", |
| 161 | + domain: "app", |
| 162 | + summary: `Failed to read logs for build ${buildId}`, |
| 163 | + why: `The Management API returned HTTP ${status}.`, |
| 164 | + fix: "Retry the command, or rerun with --trace for more detail.", |
| 165 | + exitCode: 1, |
| 166 | + }); |
| 167 | +} |
0 commit comments