Skip to content

Commit 8d773fc

Browse files
feat(cli): lambda render --width/--height at a whole-number multiple renders at that scale (#6390)
1 parent 3ff8cd8 commit 8d773fc

8 files changed

Lines changed: 135 additions & 113 deletions

File tree

‎packages/cli/src/commands/lambda.ts‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,15 @@ export default defineCommand({
130130

131131
// sites / render
132132
"site-id": { type: "string", description: "Explicit site id (overrides content hash)" },
133-
width: { type: "string", description: "Render width in pixels" },
134-
height: { type: "string", description: "Render height in pixels" },
133+
width: {
134+
type: "string",
135+
description:
136+
"Output width in pixels: the composition's width, or a whole-number multiple of it (3840 on a 1920x1080 composition renders at scale 2)",
137+
},
138+
height: {
139+
type: "string",
140+
description: "Output height in pixels, at the same multiple as --width",
141+
},
135142
"output-resolution": {
136143
type: "string",
137144
description:
Lines changed: 42 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,105 +1,73 @@
11
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
22
import { tmpdir } from "node:os";
33
import { join } from "node:path";
4-
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
5-
import { warnOnDimensionMismatch } from "./_dimensions.js";
4+
import { afterEach, beforeEach, describe, expect, it } from "vitest";
5+
import { resolveOutputResolutionFromSize } from "./_dimensions.js";
66

77
const indexHtml = (width: number, height: number) =>
88
`<!doctype html><html><body><div data-composition-id="root" data-width="${width}" data-height="${height}"></div></body></html>`;
99

10-
describe("warnOnDimensionMismatch", () => {
10+
describe("resolveOutputResolutionFromSize", () => {
1111
let dir: string;
12-
let warnSpy: ReturnType<typeof vi.fn>;
13-
let originalWarn: typeof console.warn;
1412

1513
beforeEach(() => {
16-
dir = mkdtempSync(join(tmpdir(), "hf-dim-mismatch-"));
17-
originalWarn = console.warn;
18-
warnSpy = vi.fn();
19-
console.warn = warnSpy as unknown as typeof console.warn;
14+
dir = mkdtempSync(join(tmpdir(), "hf-dim-size-"));
2015
});
2116

2217
afterEach(() => {
23-
console.warn = originalWarn;
2418
rmSync(dir, { recursive: true, force: true });
2519
});
2620

27-
function writeIndex(html: string): void {
28-
writeFileSync(join(dir, "index.html"), html);
29-
}
30-
31-
it("warns when the CLI dimensions don't match the composition", () => {
32-
writeIndex(indexHtml(1920, 1080));
33-
warnOnDimensionMismatch({
21+
const resolve = (compositionSize: [number, number], size: [number, number]) => {
22+
writeFileSync(join(dir, "index.html"), indexHtml(...compositionSize));
23+
return resolveOutputResolutionFromSize({
3424
projectDir: dir,
35-
cliWidth: 3840,
36-
cliHeight: 2160,
25+
cliWidth: size[0],
26+
cliHeight: size[1],
3727
outputResolution: undefined,
38-
quiet: false,
3928
});
40-
expect(warnSpy).toHaveBeenCalledTimes(1);
41-
const call = (warnSpy.mock.calls[0]?.[0] as string) ?? "";
42-
expect(call).toContain("3840×2160");
43-
expect(call).toContain("1920×1080");
44-
expect(call).toContain("--output-resolution");
29+
};
30+
31+
it("renders a whole-number multiple of the composition at that scale", () => {
32+
expect(resolve([1920, 1080], [3840, 2160])).toBe("landscape-4k");
33+
expect(resolve([1080, 1920], [2160, 3840])).toBe("portrait-4k");
4534
});
4635

47-
it("is silent when CLI and composition agree", () => {
48-
writeIndex(indexHtml(1920, 1080));
49-
warnOnDimensionMismatch({
50-
projectDir: dir,
51-
cliWidth: 1920,
52-
cliHeight: 1080,
53-
outputResolution: undefined,
54-
quiet: false,
55-
});
56-
expect(warnSpy).not.toHaveBeenCalled();
36+
it("leaves the composition's own size alone", () => {
37+
expect(resolve([1920, 1080], [1920, 1080])).toBeUndefined();
5738
});
5839

59-
it("is silent when --output-resolution is set (the supersampling path)", () => {
60-
writeIndex(indexHtml(1920, 1080));
61-
warnOnDimensionMismatch({
62-
projectDir: dir,
63-
cliWidth: 3840,
64-
cliHeight: 2160,
65-
outputResolution: "landscape-4k",
66-
quiet: false,
67-
});
68-
expect(warnSpy).not.toHaveBeenCalled();
40+
it("refuses a size that is not a supported multiple, naming the composition", () => {
41+
expect(() => resolve([1920, 1080], [2560, 1440])).toThrow(/1920×1080/);
42+
expect(() => resolve([1920, 1080], [3840, 1080])).toThrow(/1920×1080/);
6943
});
7044

71-
it("is silent when quiet=true (--json)", () => {
72-
writeIndex(indexHtml(1920, 1080));
73-
warnOnDimensionMismatch({
74-
projectDir: dir,
75-
cliWidth: 3840,
76-
cliHeight: 2160,
77-
outputResolution: undefined,
78-
quiet: true,
79-
});
80-
expect(warnSpy).not.toHaveBeenCalled();
45+
it("refuses a size smaller than the composition", () => {
46+
expect(() => resolve([1920, 1080], [1280, 720])).toThrow(
47+
/smaller than the composition \(1920×1080\)/,
48+
);
8149
});
8250

83-
it("is silent when index.html is missing (typical with --site-id)", () => {
84-
warnOnDimensionMismatch({
85-
projectDir: dir,
86-
cliWidth: 3840,
87-
cliHeight: 2160,
88-
outputResolution: undefined,
89-
quiet: false,
90-
});
91-
expect(warnSpy).not.toHaveBeenCalled();
51+
it("keeps an explicit --output-resolution", () => {
52+
writeFileSync(join(dir, "index.html"), indexHtml(1920, 1080));
53+
expect(
54+
resolveOutputResolutionFromSize({
55+
projectDir: dir,
56+
cliWidth: 1234,
57+
cliHeight: 567,
58+
outputResolution: "landscape-4k",
59+
}),
60+
).toBe("landscape-4k");
9261
});
9362

94-
it("is silent when the composition has no data-composition-id root", () => {
95-
writeIndex("<body><h1>just a comp</h1></body>");
96-
warnOnDimensionMismatch({
97-
projectDir: dir,
98-
cliWidth: 3840,
99-
cliHeight: 2160,
100-
outputResolution: undefined,
101-
quiet: false,
102-
});
103-
expect(warnSpy).not.toHaveBeenCalled();
63+
it("passes through when the composition is not on disk", () => {
64+
expect(
65+
resolveOutputResolutionFromSize({
66+
projectDir: join(dir, "missing"),
67+
cliWidth: 3840,
68+
cliHeight: 2160,
69+
outputResolution: undefined,
70+
}),
71+
).toBeUndefined();
10472
});
10573
});

‎packages/cli/src/commands/lambda/_dimensions.ts‎

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,40 +8,34 @@
88

99
import { readFileSync } from "node:fs";
1010
import { join } from "node:path";
11-
import type { CanvasResolution } from "@hyperframes/core";
12-
import { c } from "../../ui/colors.js";
11+
import { outputResolutionForSize, type CanvasResolution } from "@hyperframes/core";
1312
import { findCompositionDimensions } from "../../utils/compositionViewport.js";
1413

1514
export interface DimensionMismatchArgs {
1615
projectDir: string;
1716
cliWidth: number;
1817
cliHeight: number;
1918
outputResolution: CanvasResolution | undefined;
20-
/** Suppress the warning when stdout is reserved for machine output (--json). */
21-
quiet: boolean;
2219
}
2320

24-
// fallow-ignore-next-line complexity
25-
export function warnOnDimensionMismatch(args: DimensionMismatchArgs): void {
26-
if (args.quiet) return;
27-
if (args.outputResolution) return;
21+
export function resolveOutputResolutionFromSize(
22+
args: DimensionMismatchArgs,
23+
): CanvasResolution | undefined {
24+
if (args.outputResolution) return args.outputResolution;
2825
let html: string;
2926
try {
3027
html = readFileSync(join(args.projectDir, "index.html"), "utf-8");
3128
} catch {
32-
return;
29+
return undefined;
3330
}
3431
const composition = findCompositionDimensions(html);
35-
if (!composition) return;
36-
if (composition.width === args.cliWidth && composition.height === args.cliHeight) return;
37-
console.warn(
38-
c.warn(
39-
`--width/--height (${args.cliWidth}×${args.cliHeight}) disagrees with the composition's ` +
40-
`data-width/data-height (${composition.width}×${composition.height}). The runtime lays out ` +
41-
`the page at the composition's authored dimensions, so your output will be ` +
42-
`${composition.width}×${composition.height}, not ${args.cliWidth}×${args.cliHeight}.\n` +
43-
` To supersample to a higher resolution, pass --output-resolution (e.g. \`--output-resolution=4k\`).\n` +
44-
` To truly change layout dimensions, edit the composition's data-width/data-height in index.html.`,
45-
),
46-
);
32+
if (!composition) return undefined;
33+
const size = outputResolutionForSize({
34+
compositionWidth: composition.width,
35+
compositionHeight: composition.height,
36+
width: args.cliWidth,
37+
height: args.cliHeight,
38+
});
39+
if (!size.ok) throw new Error(`--width/--height: ${size.message}`);
40+
return size.outputResolution;
4741
}

‎packages/cli/src/commands/lambda/render-batch.ts‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import {
3838
validateVariablesAgainstSchema,
3939
} from "../../utils/variables.js";
4040
import { normalizeErrorMessage } from "../../utils/errorMessage.js";
41-
import { warnOnDimensionMismatch } from "./_dimensions.js";
41+
import { resolveOutputResolutionFromSize } from "./_dimensions.js";
4242
import { runWithConcurrencyLimit } from "../../utils/concurrency.js";
4343
import { requireStack } from "./state.js";
4444

@@ -177,13 +177,15 @@ export async function runRenderBatch(args: RenderBatchArgs): Promise<void> {
177177
failCommand();
178178
}
179179

180-
warnOnDimensionMismatch({
181-
projectDir,
182-
cliWidth: args.width,
183-
cliHeight: args.height,
184-
outputResolution: args.outputResolution,
185-
quiet: args.json,
186-
});
180+
args = {
181+
...args,
182+
outputResolution: resolveOutputResolutionFromSize({
183+
projectDir,
184+
cliWidth: args.width,
185+
cliHeight: args.height,
186+
outputResolution: args.outputResolution,
187+
}),
188+
};
187189

188190
// Pre-validate every entry's variables against the composition's
189191
// schema. Mismatches print as warnings; strict mode aborts before any

‎packages/cli/src/commands/lambda/render.ts‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
resolveVariablesArg,
1919
validateVariablesAgainstProject,
2020
} from "../../utils/variables.js";
21-
import { warnOnDimensionMismatch } from "./_dimensions.js";
21+
import { resolveOutputResolutionFromSize } from "./_dimensions.js";
2222
import { videoArchitecture, requireStack, type StackOutputs, stateFilePath } from "./state.js";
2323

2424
// Dynamic-import the SDK so tsup keeps it out of the static-import head of
@@ -84,13 +84,15 @@ export async function runRender(args: RenderArgs): Promise<void> {
8484
const stack = requireStack(args.stackName);
8585
const projectDir = resolvePath(args.projectDir);
8686

87-
warnOnDimensionMismatch({
88-
projectDir,
89-
cliWidth: args.width,
90-
cliHeight: args.height,
91-
outputResolution: args.outputResolution,
92-
quiet: args.json,
93-
});
87+
args = {
88+
...args,
89+
outputResolution: resolveOutputResolutionFromSize({
90+
projectDir,
91+
cliWidth: args.width,
92+
cliHeight: args.height,
93+
outputResolution: args.outputResolution,
94+
}),
95+
};
9496

9597
// Resolve --variables / --variables-file using the same parser the local
9698
// `hyperframes render` uses. `resolveVariablesArg` exits(1) with a friendly

‎packages/core/src/core.types.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ export {
182182
isAspectAgnosticResolutionAlias,
183183
resolveResolutionFlagPair,
184184
checkOutputResolutionCompatibility,
185+
outputResolutionForSize,
185186
suggestMatchingPreset,
186187
COMPOSITION_VARIABLE_TYPES,
187188
TIMELINE_COLORS,

‎packages/core/src/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ export {
6666
isAspectAgnosticResolutionAlias,
6767
resolveResolutionFlagPair,
6868
checkOutputResolutionCompatibility,
69+
outputResolutionForSize,
6970
suggestMatchingPreset,
7071
parseFps,
7172
parseFpsWithDefault,

‎packages/parsers/src/outputResolutionCompatibility.ts‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,3 +185,50 @@ export function checkOutputResolutionCompatibility(input: {
185185

186186
return OK;
187187
}
188+
189+
export type OutputSizeResolution =
190+
| { ok: true; outputResolution: CanvasResolution | undefined }
191+
| { ok: false; message: string };
192+
193+
/**
194+
* The preset a requested pixel size stands for on a composition: none when it is the composition's own size,
195+
* else the preset of exactly that size when it is a whole-number multiple (3840×2160 on 1920×1080 is scale 2).
196+
*/
197+
export function outputResolutionForSize(input: {
198+
compositionWidth: number;
199+
compositionHeight: number;
200+
width: number;
201+
height: number;
202+
}): OutputSizeResolution {
203+
const { compositionWidth, compositionHeight, width, height } = input;
204+
if (width === compositionWidth && height === compositionHeight) {
205+
return { ok: true, outputResolution: undefined };
206+
}
207+
const composition = `${compositionWidth}×${compositionHeight}`;
208+
const scale = width / compositionWidth;
209+
if (width < compositionWidth || height < compositionHeight) {
210+
return {
211+
ok: false,
212+
message: `${width}×${height} is smaller than the composition (${composition}). Use the composition's size, or a whole-number multiple of it.`,
213+
};
214+
}
215+
const preset = VALID_CANVAS_RESOLUTIONS.find(
216+
(name) => CANVAS_DIMENSIONS[name].width === width && CANVAS_DIMENSIONS[name].height === height,
217+
);
218+
if (
219+
!Number.isInteger(scale) ||
220+
height !== compositionHeight * scale ||
221+
!preset ||
222+
!checkOutputResolutionCompatibility({
223+
compositionWidth,
224+
compositionHeight,
225+
outputResolution: preset,
226+
}).ok
227+
) {
228+
return {
229+
ok: false,
230+
message: `${width}×${height} is not a whole-number multiple of the composition (${composition}) at a supported size. Use ${composition}, or a multiple such as ${compositionWidth * 2}×${compositionHeight * 2} when that is a preset size (${VALID_CANVAS_RESOLUTIONS.map((name) => `${CANVAS_DIMENSIONS[name].width}×${CANVAS_DIMENSIONS[name].height}`).join(", ")}).`,
231+
};
232+
}
233+
return { ok: true, outputResolution: preset };
234+
}

0 commit comments

Comments
 (0)