#!/usr/bin/env node // ── EPIPE suppression (must run before ANY stdout/stderr write) ──────────── // When the CLI runs inside a piped agent environment (Claude Code, Codex, // Cursor, etc.), the reader may close the pipe before we finish writing. // Node treats EPIPE on stdout/stderr as an uncaughtException, which crashes // the process. This is a normal lifecycle event — suppress it. // // commandFailed must be declared here (before the handlers) so the EPIPE // stream-error path can set it before it exits. The telemetry exit // handler reads this flag to determine success/failure — an EPIPE that // interrupts a command should NOT score as success:true, but one that // arrives after the render artifact was validated is the normal agent-pipe // teardown and must stay success:true (see handleStreamEpipe). let commandFailed = false; // A cli_error went out for this run: its failure is already named. let errorReported = false; for (const stream of [process.stdout, process.stderr]) { stream.on("error", (err) => { if ((err as NodeJS.ErrnoException).code === "EPIPE") { handleStreamEpipe(); } }); } // ── Worker entry path bootstrap (must run before any producer/engine load) ── // The shaderTransitionWorkerPool lives in the producer package and resolves // its worker entry by probing for a sibling `.js` file next to // `import.meta.url`. When this CLI is bundled by tsup, the producer code is // bundled into chunks beside cli.js, but `import.meta.url` resolves to the producer's // own dist path (NOT cli.js) on some module-graph layouts — so the sibling // probe lands in a directory that does not contain the bundled worker. // We emit the worker entry next to cli.js (see tsup.config.ts) and tell // the pool where to find it via the published env-var override. import "./utils/hideChildConsoles.js"; import "./utils/windowsExeSearch.js"; import { dirname, join, resolve } from "node:path"; import { fileURLToPath } from "node:url"; import { existsSync } from "node:fs"; (() => { const here = dirname(fileURLToPath(import.meta.url)); const shader = join(here, "shaderTransitionWorker.js"); if (!process.env.HF_SHADER_WORKER_ENTRY && existsSync(shader)) { process.env.HF_SHADER_WORKER_ENTRY = shader; } })(); // ── Fast-path exits ───────────────────────────────────────────────────────── // Check --version before importing anything heavy. This makes // `hyperframes --version` near-instant (~10ms vs ~80ms). import { VERSION } from "./version.js"; let argv = process.argv.slice(2); if (argv[0] === "help") { process.argv = [...process.argv.slice(0, 2), ...argv.slice(1), "--help"]; argv = process.argv.slice(2); } const commandArg = argv[0]; const rootVersionRequested = commandArg === "--version" || commandArg === "-V" || (commandArg === undefined && (argv.includes("--version") || argv.includes("-V"))); if (rootVersionRequested) { console.log(VERSION); process.exit(0); } // The working directory is project data: load only the settings a project `.env` may supply, // before browser selection can observe the environment. const { loadProjectDotEnv } = await import("./utils/dotEnv.js"); const captureEnv = argv[commandWordIndex(argv)] === "capture"; if (!captureEnv || process.env.HYPERFRAMES_NO_CAPTURE_DOTENV !== "1") { loadProjectDotEnv(process.cwd(), process.env, (line) => console.error(line)); } // ── Lazy imports ──────────────────────────────────────────────────────────── // Telemetry, update checks, and heavy modules are imported only when needed. // For --help we skip telemetry entirely. import { runCommand } from "citty"; import { defineCommand } from "./utils/defineCommand.js"; import type { ArgsDef, CommandDef } from "citty"; import { getRunId } from "./telemetry/runId.js"; import { reportCommandFailure, trackCommandFailures } from "./utils/command-failure-tracking.js"; import { isRenderSucceeded, isRunDelivered, markCommandDelivered, } from "./utils/render-success-state.js"; import { resolveCommandUsage, type ResolvedCommandUsage } from "./utils/commandUsageResolution.js"; import { closestMatch, closestNames, commandRedirect, fileHint } from "./utils/didYouMean.js"; import { errorBox } from "./ui/format.js"; import { failureShown, jsonErrorBody, watchOutput, writeJsonFailure } from "./utils/jsonError.js"; import { isDevMode } from "./utils/env.js"; import { terminalLines, terminalText } from "@hyperframes/core"; import { CliResultSignal, CliRuntimeError, CliUsageError, USAGE_EXIT_CODE, consumeCommandResult, endProcess, failureOrigin, outputWritten, registerRootExitCodeSanitizer, registerRootExitRequester, type CommandResult, } from "./utils/commandResult.js"; import { registerRunningCli } from "./utils/runningCli.js"; import { commandWordIndex, measureProbeShortcutArgv, openShortcutArgv, registryAliasArgv, } from "./utils/projectShortcut.js"; registerRunningCli(); const isHelp = process.argv.includes("--help") || process.argv.includes("-h"); // Runs before commands/preview.js is imported, so a missing or stale // package is named here instead of crashing deep inside that import. async function assertStudioWorkspaceBuilt(): Promise { if (!isDevMode()) return; const repoRoot = resolve(dirname(fileURLToPath(import.meta.url)), "..", "..", ".."); const { checkStudioWorkspaceBuild, formatWorkspaceBuildProblems } = await import("./utils/workspaceBuildCheck.js"); const problems = checkStudioWorkspaceBuild(repoRoot); if (problems.length === 0) return; console.error(formatWorkspaceBuildProblems(problems)); throw new CliRuntimeError("Studio workspace build check failed", { exitCode: 1, presented: true, }); } // --------------------------------------------------------------------------- // CLI definition — all commands are lazy-loaded via dynamic import() // --------------------------------------------------------------------------- const commandLoaders = { init: () => import("./commands/init.js").then((m) => m.default), add: () => import("./commands/add.js").then((m) => m.default), catalog: () => import("./commands/catalog.js").then((m) => m.default), "media-use": () => import("./commands/media-use.js").then((m) => m.default), play: () => import("./commands/play.js").then((m) => m.default), present: () => import("./commands/present.js").then((m) => m.default), preview: () => assertStudioWorkspaceBuilt().then(() => import("./commands/preview.js").then((m) => m.default)), share: () => import("./commands/share.js").then((m) => m.default), publish: () => import("./commands/publish.js").then((m) => m.default), open: () => import("./commands/open.js").then((m) => m.default), "catch-up": () => import("./commands/catch-up.js").then((m) => m.default), render: () => import("./commands/render.js").then((m) => m.default), lint: () => import("./commands/lint.js").then((m) => m.default), check: () => import("./commands/check.js").then((m) => m.default), beats: () => import("./commands/beats.js").then((m) => m.default), audio: () => import("./commands/audio.js").then((m) => m.default), "normalize-audio": () => import("./commands/normalize-audio.js").then((m) => m.default), inspect: () => import("./commands/inspect.js").then((m) => m.default), keyframes: () => import("./commands/keyframes.js").then((m) => m.default), layout: () => import("./commands/layout.js").then((m) => m.default), info: () => import("./commands/info.js").then((m) => m.default), compositions: () => import("./commands/compositions.js").then((m) => m.default), timeline: () => import("./commands/timeline.js").then((m) => m.default), history: () => import("./commands/history.js").then((m) => m.default), benchmark: () => import("./commands/benchmark.js").then((m) => m.default), browser: () => import("./commands/browser.js").then((m) => m.default), "remove-background": () => import("./commands/remove-background.js").then((m) => m.default), transcribe: () => import("./commands/transcribe.js").then((m) => m.default), usage: () => import("./commands/usage.js").then((m) => m.default), models: () => import("./commands/models.js").then((m) => m.default), tts: () => import("./commands/tts.js").then((m) => m.default), docs: () => import("./commands/docs.js").then((m) => m.default), doctor: () => import("./commands/doctor.js").then((m) => m.default), clean: () => import("./commands/clean.js").then((m) => m.default), upgrade: () => import("./commands/upgrade.js").then((m) => m.default), update: () => import("./commands/update.js").then((m) => m.default), skills: () => import("./commands/skills.js").then((m) => m.default), feedback: () => import("./commands/feedback.js").then((m) => m.default), telemetry: () => import("./commands/telemetry.js").then((m) => m.default), events: () => import("./commands/events.js").then((m) => m.default), validate: () => import("./commands/validate.js").then((m) => m.default), snapshot: () => import("./commands/snapshot.js").then((m) => m.default), "media-treatment": () => import("./commands/media-treatment.js").then((m) => m.mediaTreatmentCommand), "grade-compare": () => import("./commands/grade-compare.js").then((m) => m.default), compare: () => import("./commands/compare.js").then((m) => m.default), diff: () => import("./commands/diff.js").then((m) => m.default), measure: () => import("./commands/measure.js").then((m) => m.default), reverse: () => import("./commands/reverse.js").then((m) => m.default), billing: () => import("./commands/billing.js").then((m) => m.default), templates: () => import("./commands/templates.js").then((m) => m.default), capture: () => import("./commands/capture.js").then((m) => m.default), lambda: () => import("./commands/lambda.js").then((m) => m.default), cloudrun: () => import("./commands/cloudrun.js").then((m) => m.default), cloud: () => import("./commands/cloud.js").then((m) => m.default), auth: () => import("./commands/auth.js").then((m) => m.default), figma: () => import("./commands/figma.js").then((m) => m.default), }; // `hyperframes ` is `hyperframes open `; every later read of the command sees `open`. process.argv = [ ...process.argv.slice(0, 2), ...registryAliasArgv(openShortcutArgv(argv, new Set(Object.keys(commandLoaders)))), ]; argv = process.argv.slice(2); // `hyperframes measure