Skip to content

Commit 9402771

Browse files
committed
refactor(cli): decompose render phases
1 parent 98c631f commit 9402771

7 files changed

Lines changed: 874 additions & 785 deletions

File tree

packages/cli/src/commands/batchRender.test.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,6 @@ function expectBatchError(fn: () => unknown, title: string): BatchRenderInputErr
4747
throw new Error("Expected BatchRenderInputError");
4848
}
4949

50-
function eventType(value: unknown): string | undefined {
51-
if (value === null || typeof value !== "object" || Array.isArray(value)) return undefined;
52-
return "type" in value && typeof value.type === "string" ? value.type : undefined;
53-
}
54-
5550
describe("parseBatchRows", () => {
5651
it("parses a JSON array of variable rows", () => {
5752
expect(parseBatchRows('[{"name":"Alice"},{"name":"Bob"}]', "rows.json")).toEqual([
@@ -192,7 +187,7 @@ describe("runBatchRender", () => {
192187
expect(readFileSync(prepared.manifestPath, "utf8")).toContain('"status": "completed"');
193188
});
194189

195-
it("emits JSON progress events when json mode is enabled", async () => {
190+
it("emits exactly one final JSON document when json mode is enabled", async () => {
196191
const prepared = prepareBatchRender({
197192
batchPath: writeJson("rows.json", '[{"name":"Alice"}]'),
198193
outputTemplate: join(tmpDir, "renders/{name}.mp4"),
@@ -212,12 +207,14 @@ describe("runBatchRender", () => {
212207
renderOne: async () => ({ renderTimeMs: 10 }),
213208
});
214209

215-
const events = log.mock.calls.map((call): unknown => JSON.parse(String(call[0])));
216-
expect(events.map(eventType)).toEqual([
217-
"batch-row-start",
218-
"batch-row-complete",
219-
"batch-complete",
220-
]);
210+
expect(log).toHaveBeenCalledTimes(1);
211+
const result = JSON.parse(String(log.mock.calls[0]?.[0])) as {
212+
type: string;
213+
completed: number;
214+
rows: Array<{ status: string }>;
215+
};
216+
expect(result).toMatchObject({ type: "batch-complete", completed: 1 });
217+
expect(result.rows).toEqual([expect.objectContaining({ status: "completed" })]);
221218
});
222219

223220
it("continues after row failure by default", async () => {

packages/cli/src/commands/batchRender.ts

Lines changed: 13 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -305,10 +305,6 @@ function writeManifest(manifest: BatchManifest): void {
305305
writeFileSync(manifest.manifestPath, JSON.stringify(manifest, null, 2) + "\n", "utf8");
306306
}
307307

308-
function emitJsonEvent(event: Record<string, unknown>, json: boolean): void {
309-
if (json) console.log(JSON.stringify(event));
310-
}
311-
312308
async function renderBatchRow(
313309
row: PreparedBatchRow,
314310
manifest: BatchManifest,
@@ -322,11 +318,6 @@ async function renderBatchRow(
322318
manifestRow.status = "running";
323319
manifestRow.startedAt = new Date().toISOString();
324320
writeManifest(manifest);
325-
emitJsonEvent(
326-
{ type: "batch-row-start", index: row.index, outputPath: row.outputPath },
327-
options.json,
328-
);
329-
330321
if (!options.quiet && !options.json) {
331322
console.log(c.dim(`Batch row ${row.index}: ${row.outputPath}`));
332323
}
@@ -339,31 +330,12 @@ async function renderBatchRow(
339330
manifestRow.renderTimeMs = result.renderTimeMs;
340331
manifestRow.completedAt = new Date().toISOString();
341332
writeManifest(manifest);
342-
emitJsonEvent(
343-
{
344-
type: "batch-row-complete",
345-
index: row.index,
346-
outputPath: row.outputPath,
347-
durationMs: manifestRow.durationMs,
348-
renderTimeMs: manifestRow.renderTimeMs,
349-
},
350-
options.json,
351-
);
352333
return true;
353334
} catch (error: unknown) {
354335
manifestRow.status = "failed";
355336
manifestRow.error = errorMessage(error);
356337
manifestRow.completedAt = new Date().toISOString();
357338
writeManifest(manifest);
358-
emitJsonEvent(
359-
{
360-
type: "batch-row-error",
361-
index: row.index,
362-
outputPath: row.outputPath,
363-
error: manifestRow.error,
364-
},
365-
options.json,
366-
);
367339
if (!options.quiet && !options.json) {
368340
console.log(c.error(` Row ${row.index} failed: ${manifestRow.error}`));
369341
}
@@ -423,17 +395,19 @@ export async function runBatchRender(options: RunBatchRenderOptions): Promise<Ba
423395

424396
if (stopLaunching) markUnstartedRowsSkipped(manifest);
425397
writeManifest(manifest);
426-
emitJsonEvent(
427-
{
428-
type: "batch-complete",
429-
manifestPath: manifest.manifestPath,
430-
total: manifest.total,
431-
completed: manifest.completed,
432-
failed: manifest.failed,
433-
skipped: manifest.skipped,
434-
},
435-
options.json,
436-
);
398+
if (options.json) {
399+
console.log(
400+
JSON.stringify({
401+
type: "batch-complete",
402+
manifestPath: manifest.manifestPath,
403+
total: manifest.total,
404+
completed: manifest.completed,
405+
failed: manifest.failed,
406+
skipped: manifest.skipped,
407+
rows: manifest.rows,
408+
}),
409+
);
410+
}
437411

438412
if (!options.quiet && !options.json) {
439413
console.log("");

0 commit comments

Comments
 (0)