Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/packages/cli.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -1746,7 +1746,7 @@ npx hyperframes keyframes [dir] --selector "#card" --shot card-motion.png
| `--runtime` | Filter hint: `gsap`, `css`, `anime`, or `all` |
| `--json` | Machine-readable results |
| `--shot` | Write an onion-skin PNG of the real element sampled over the timeline. Pair with `--selector` to focus one element. |
| `--samples` | Onion samples at equal time steps (default 9) |
| `--samples` | Onion samples at equal time steps, 1 to 60 (default 9) |
| `--layout` | `path` — ghosts at real positions plus a path (default) — or `strip`, for in-place or overlapping motion. `strip` is a real per-time pixel filmstrip only when `--selector` targets an SVG element; any other selector (e.g. a DOM/sub-composition host) gets one live frame plus vector position markers instead |
| `--from`, `--to` | Sample only this time range, in seconds |
| `--angle` | Orbit camera: `front`, `iso`, `top`, `side`, `rear-iso`, or `yaw,pitch` in degrees |
Expand Down
55 changes: 55 additions & 0 deletions packages/cli/src/commands/keyframes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import keyframesCommand from "./keyframes.js";
import {
collectCompositions,
collectShotSelectors,
onionShotOptions,
resolveScope,
singleFileStage,
surfaceComposition,
Expand Down Expand Up @@ -647,3 +648,57 @@ describe("keyframes text output", () => {
},
);
});

describe("keyframes --shot numeric flags", () => {
it("keeps the defaults when the flags are unset", () => {
expect(onionShotOptions({})).toMatchObject({ samples: 9, from: null, to: null });
});

it("accepts valid samples and times", () => {
expect(onionShotOptions({ samples: "60", from: "0", to: "1.5" })).toMatchObject({
samples: 60,
from: 0,
to: 1.5,
});
});

it.each(["abc", "0", "-3", "2.5", "61", ""])("rejects --samples %j as a usage error", (value) => {
expect(() => onionShotOptions({ samples: value })).toThrow(
expect.objectContaining({
message: "--samples must be an integer from 1 to 60",
result: expect.objectContaining({ exitCode: 2, kind: "usage_error" }),
}),
);
});

it.each([
["--from", "abc"],
["--from", "-1"],
["--to", "xyz"],
["--to", ""],
["--to", "Infinity"],
] as const)("rejects %s %j as a usage error", (flag, value) => {
const args = flag === "--from" ? { from: value } : { to: value };
expect(() => onionShotOptions(args)).toThrow(
expect.objectContaining({
message: `${flag} must be a number of seconds, 0 or more`,
result: expect.objectContaining({ exitCode: 2, kind: "usage_error" }),
}),
);
});

it("reports a bad flag before checking for animated elements or opening a browser", async () => {
const dir = mkdtempSync(join(tmpdir(), "hf-keyframes-shot-flags-"));
onTestFinished(() => rmSync(dir, { recursive: true, force: true }));
writeFileSync(join(dir, "index.html"), wrap(""));
const log = vi.spyOn(console, "log").mockImplementation(() => {});
onTestFinished(() => log.mockRestore());

await expect(
runCommand(keyframesCommand, {
rawArgs: [dir, "--shot", join(dir, "shot.png"), "--samples", "abc"],
}),
).rejects.toThrow("--samples must be an integer from 1 to 60");
expect(existsSync(join(dir, "shot.png"))).toBe(false);
});
});
30 changes: 21 additions & 9 deletions packages/cli/src/commands/keyframes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import { c } from "../ui/colors.js";
import { ensureDOMParser } from "../utils/dom.js";
import { resolveProject } from "../utils/project.js";
import { leftOutNotice, stageReferencedFiles } from "../utils/stageReferencedFiles.js";
import { CliUsageError } from "../utils/commandResult.js";
import { parsePositiveIntegerArg } from "../utils/integerArg.js";
import { withMeta } from "../utils/updateCheck.js";

export const examples: Example[] = [
Expand Down Expand Up @@ -871,13 +873,22 @@ function onionShotGuardError(
return null;
}

function onionShotOptions(args: ShotArgs & { selector?: string }) {
function shotSeconds(value: string | undefined, flag: string): number | null {
if (value === undefined) return null;
const text = String(value).trim();
const parsed = text === "" ? NaN : Number(text);
if (!Number.isFinite(parsed) || parsed < 0)
throw new CliUsageError(`${flag} must be a number of seconds, 0 or more`);
return parsed;
}

export function onionShotOptions(args: ShotArgs & { selector?: string }) {
return {
samples: num(args.samples) ?? 9,
samples: parsePositiveIntegerArg(args.samples, "--samples", { max: 60 }) ?? 9,
layout: (args.layout === "strip" ? "strip" : "path") as "strip" | "path",
fit: args.fit ?? true,
from: num(args.from),
to: num(args.to),
from: shotSeconds(args.from, "--from"),
to: shotSeconds(args.to, "--to"),
angle: args.angle,
scopeSelector: args.selector ?? null,
ghost: args.ghost ?? false,
Expand Down Expand Up @@ -905,25 +916,26 @@ async function runOnionShot(
// With --selector, sample from the FULL animated set and let the browser scope
// to the selector (or its animated descendants when the selector is a static
// wrapper like `.clip`). Without it, only the (already-filtered) comps qualify.
const options = onionShotOptions(args);
const requests = collectShotSelectors(args.selector ? allComps : comps);
const guardError = onionShotGuardError(scope.projectDir, requests, args.ghost ?? false);
if (guardError) {
console.log(c.dim(guardError));
return true;
}
const saved = await captureOnionShot(scope, requests, args);
const saved = await captureOnionShot(scope, requests, args.shot!, options);
printOnionShotSaved(saved, requests.length);
return false;
}

async function captureOnionShot(
scope: ShotScope,
requests: Array<{ selector: string }>,
args: ShotArgs & { selector?: string },
shot: string,
options: ReturnType<typeof onionShotOptions>,
): Promise<string> {
const { captureMotionPathShot, ensureShotOutputDir } = await import("./motionShot.js");
const outPath = resolve(args.shot!);
const options = onionShotOptions(args);
const outPath = resolve(shot);
if (!scope.singleFile) {
return captureMotionPathShot(scope.projectDir!, requests, outPath, {
...options,
Expand Down Expand Up @@ -1138,7 +1150,7 @@ function createKeyframesCommand(options: Partial<KeyframesCommandOptions> = {})
},
samples: {
type: "string",
description: "Onion samples (equal-time steps) for --shot. Default 9.",
description: "Onion samples (equal-time steps) for --shot, 1 to 60. Default 9.",
},
layout: {
type: "string",
Expand Down
Loading