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