mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-09-14 18:01:20 +08:00
a108ffa1f2
The browser-based `validate` (console/network/HTTP + contrast/clip audits, remote-asset localization, viewport-from-comp) only existed inline in the `validate` CLI command, so external consumers couldn't reuse it — they reimplement a parallel headless harness that drifts from render (missing localize → false CORS; hardcoded viewport → false out-of-frame; hand-maintained runtime parity). Lint doesn't have this problem: it's an importable `@hyperframes/lint` package. This lifts the browser core into a new `@hyperframes/validate` package, exporting `validateHtmlInBrowser(html, opts)`. The CLI `validate` command is behavior-unchanged: it lints + bundles, resolves Chrome via its own `ensureBrowser()`, and delegates. The package takes `opts.browserExecutablePath` so the caller owns browser discovery — the package stays launch-agnostic (no browser/manager), like lint owns rules but not I/O. New `opts.onPage(page)` runs extra checks on the SAME loaded page before teardown (findings merged), so a consumer can layer its own gates (e.g. caption-zone / out-of-frame) without a second browser load. - contrast-audit.browser.js moves to the package (its only consumer); the CLI build copies it into dist and the bundled runtime loads it via the same page.addScriptTag path. - The small low-level helpers validate needs (static file server, comp-viewport parse, linkedom shim, error normalizer) are vendored into the package so it doesn't import from @hyperframes/cli (which depends on it); folding both onto a shared low-level package is a follow-up. Marked in .fallowrc duplicates.ignore, matching the repo's moved-code convention. - Pure-function + contrast-script tests move to the package with the code they cover; the CLI keeps only extractCompositionErrorsFromLint (a CLI-side lint→error shaping concern). Verified: package build + typecheck clean, package tests 13/13, CLI tests 1270/1270 (103 files), fallow audit clean on 20 changed files, and an e2e `hyperframes validate` on a real composition through the package (Chrome launch + contrast audit) returns ok:true. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
140 lines
5.2 KiB
JavaScript
140 lines
5.2 KiB
JavaScript
// Cross-platform replacement for the previous `mkdir -p … && cp -r …` shell
|
|
// chain, which failed on Windows because `cp` doesn't accept `-r` there.
|
|
|
|
import { cpSync, existsSync, mkdirSync, readdirSync } from "node:fs";
|
|
import { dirname, join, resolve } from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
import { setTimeout as sleep } from "node:timers/promises";
|
|
|
|
const HERE = dirname(fileURLToPath(import.meta.url));
|
|
const CLI_ROOT = resolve(HERE, "..");
|
|
const REPO_ROOT = resolve(CLI_ROOT, "..", "..");
|
|
const DIST = join(CLI_ROOT, "dist");
|
|
|
|
// Studio's vite build clears its dist before rewriting it; don't start the
|
|
// copy until both sentinels are present so we never observe a partial tree.
|
|
const STUDIO_WAIT_TIMEOUT_MS = 30_000;
|
|
const STUDIO_POLL_INTERVAL_MS = 250;
|
|
|
|
// fallow-ignore-next-line complexity
|
|
async function waitForStudioDist(dir) {
|
|
const deadline = Date.now() + STUDIO_WAIT_TIMEOUT_MS;
|
|
while (Date.now() < deadline) {
|
|
try {
|
|
const entries = new Set(readdirSync(dir));
|
|
// vite emits `assets/` before rewriting `index.html` at the end of the
|
|
// build — so once both are present, the tree is complete.
|
|
if (entries.has("index.html") && entries.has("assets")) return;
|
|
} catch {
|
|
// dir doesn't exist yet — vite will create it
|
|
}
|
|
await sleep(STUDIO_POLL_INTERVAL_MS);
|
|
}
|
|
throw new Error(`[build-copy] timed out waiting for studio dist at ${dir}`);
|
|
}
|
|
|
|
function copyDir(src, dest) {
|
|
cpSync(src, dest, { recursive: true, force: true });
|
|
}
|
|
|
|
function copyDirContents(src, dest) {
|
|
for (const entry of readdirSync(src)) {
|
|
cpSync(join(src, entry), join(dest, entry), {
|
|
recursive: true,
|
|
force: true,
|
|
});
|
|
}
|
|
}
|
|
|
|
function copyMdFiles(srcDir, destDir) {
|
|
if (!existsSync(srcDir)) return;
|
|
for (const name of readdirSync(srcDir)) {
|
|
if (name.endsWith(".md")) {
|
|
cpSync(join(srcDir, name), join(destDir, name));
|
|
}
|
|
}
|
|
}
|
|
|
|
// fallow-ignore-next-line complexity
|
|
async function main() {
|
|
for (const sub of ["studio", "docs", "templates", "skills", "docker"]) {
|
|
mkdirSync(join(DIST, sub), { recursive: true });
|
|
}
|
|
mkdirSync(join(DIST, "commands"), { recursive: true });
|
|
|
|
const studioDist = resolve(CLI_ROOT, "..", "studio", "dist");
|
|
await waitForStudioDist(studioDist);
|
|
copyDirContents(studioDist, join(DIST, "studio"));
|
|
|
|
for (const tmpl of ["blank", "_shared"]) {
|
|
copyDir(join(CLI_ROOT, "src", "templates", tmpl), join(DIST, "templates", tmpl));
|
|
}
|
|
|
|
// Bundle warm-grain from the repo registry so the built CLI can scaffold it
|
|
// offline and CI smoke tests pick up PR-branch changes before merge to main.
|
|
const warmGrainSrc = join(REPO_ROOT, "registry", "examples", "warm-grain");
|
|
if (existsSync(warmGrainSrc)) {
|
|
copyDir(warmGrainSrc, join(DIST, "templates", "warm-grain"));
|
|
}
|
|
|
|
// Skills bundled into the published CLI. Branches don't all carry the same
|
|
// skills/ tree (it gets restructured), so each entry is existsSync-guarded:
|
|
// a missing skill dir warns + skips instead of crashing the build.
|
|
for (const skill of ["hyperframes", "hyperframes-cli", "gsap"]) {
|
|
const src = join(REPO_ROOT, "skills", skill);
|
|
if (!existsSync(src)) {
|
|
console.warn(`[build-copy] skill not found, skipping: skills/${skill}`);
|
|
continue;
|
|
}
|
|
copyDir(src, join(DIST, "skills", skill));
|
|
}
|
|
|
|
const dockerfile = join(CLI_ROOT, "src", "docker", "Dockerfile.render");
|
|
if (existsSync(dockerfile)) {
|
|
cpSync(dockerfile, join(DIST, "docker", "Dockerfile.render"));
|
|
}
|
|
|
|
const layoutAuditScript = join(CLI_ROOT, "src", "commands", "layout-audit.browser.js");
|
|
if (existsSync(layoutAuditScript)) {
|
|
cpSync(layoutAuditScript, join(DIST, "commands", "layout-audit.browser.js"));
|
|
}
|
|
|
|
// Owned by @hyperframes/validate; bundled CLI loads it from dist/commands via page.addScriptTag.
|
|
const contrastAuditScript = join(CLI_ROOT, "..", "validate", "src", "contrast-audit.browser.js");
|
|
if (existsSync(contrastAuditScript)) {
|
|
cpSync(contrastAuditScript, join(DIST, "commands", "contrast-audit.browser.js"));
|
|
}
|
|
|
|
const motionSampleScript = join(CLI_ROOT, "src", "commands", "motion-sample.browser.js");
|
|
if (existsSync(motionSampleScript)) {
|
|
cpSync(motionSampleScript, join(DIST, "commands", "motion-sample.browser.js"));
|
|
}
|
|
|
|
// Player bundles for the standalone browser player used by `present` and
|
|
// `play`. resolvePlayerPath/resolveSlideshowPath look for these alongside the
|
|
// built CLI (dist/<name>.global.js), so they must ship in the package — the
|
|
// monorepo-dev fallback paths don't exist once installed from npm. Without
|
|
// this, `npx hyperframes present` fails with "@hyperframes/player not found".
|
|
const playerDist = join(REPO_ROOT, "packages", "player", "dist");
|
|
const playerGlobals = [
|
|
[join(playerDist, "hyperframes-player.global.js"), join(DIST, "hyperframes-player.global.js")],
|
|
[
|
|
join(playerDist, "slideshow", "hyperframes-slideshow.global.js"),
|
|
join(DIST, "hyperframes-slideshow.global.js"),
|
|
],
|
|
];
|
|
for (const [src, dest] of playerGlobals) {
|
|
if (existsSync(src)) {
|
|
cpSync(src, dest);
|
|
} else {
|
|
console.warn(`[build-copy] player bundle not found, skipping: ${src}`);
|
|
}
|
|
}
|
|
|
|
copyMdFiles(join(CLI_ROOT, "src", "docs"), join(DIST, "docs"));
|
|
|
|
console.log("[build-copy] done");
|
|
}
|
|
|
|
await main();
|