mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-15 22:08:03 +00:00
- Fix stale docstring in `fetchRemoteTemplate`: said "examples/<id>" after the rename, actual path is "registry/examples/<id>" - Add regression test pinning TEMPLATES_DIR and MANIFEST_FILENAME so accidentally reverting either constant fails a test instead of silently breaking init for every installed CLI - Export the two constants from remote.ts for test visibility (no public API change — internal module only) - Add a clarifying comment on getStaticTemplateDir / getSharedTemplateDir in init.ts noting these resolve to the CLI's bundled templates dir, not the newly-relocated registry/examples/ dir fetched remotely. Prevents future-reader confusion now that root templates/ is gone No behavior change. 2 new tests, 57 CLI tests passing (was 55, +2). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
105 lines
3.2 KiB
TypeScript
105 lines
3.2 KiB
TypeScript
/**
|
|
* Remote Template Fetching
|
|
*
|
|
* Downloads templates from the hyperframes GitHub repository using giget.
|
|
* Templates live in the `registry/examples/` directory of the repo.
|
|
*/
|
|
|
|
import { existsSync, readFileSync, writeFileSync, mkdirSync } from "node:fs";
|
|
import { join } from "node:path";
|
|
import { homedir } from "node:os";
|
|
|
|
const REPO = "heygen-com/hyperframes";
|
|
// Exported for regression testing — see remote.test.ts.
|
|
export const TEMPLATES_DIR = "registry/examples";
|
|
export const MANIFEST_FILENAME = "templates.json";
|
|
|
|
/** Cache directory for remote template metadata. */
|
|
const CACHE_DIR = join(homedir(), ".hyperframes", "cache");
|
|
const MANIFEST_CACHE_PATH = join(CACHE_DIR, "remote-templates.json");
|
|
const CACHE_TTL_MS = 24 * 60 * 60 * 1000; // 24 hours
|
|
|
|
export interface RemoteTemplateInfo {
|
|
id: string;
|
|
label: string;
|
|
hint: string;
|
|
bundled: boolean;
|
|
}
|
|
|
|
interface ManifestCache {
|
|
fetchedAt: number;
|
|
templates: RemoteTemplateInfo[];
|
|
}
|
|
|
|
/**
|
|
* Fetch the remote template manifest from GitHub.
|
|
* Caches the result for 24 hours to avoid rate limits.
|
|
*/
|
|
export async function listRemoteTemplates(): Promise<RemoteTemplateInfo[]> {
|
|
// Check cache first
|
|
if (existsSync(MANIFEST_CACHE_PATH)) {
|
|
try {
|
|
const cached: ManifestCache = JSON.parse(readFileSync(MANIFEST_CACHE_PATH, "utf-8"));
|
|
if (Date.now() - cached.fetchedAt < CACHE_TTL_MS) {
|
|
return cached.templates;
|
|
}
|
|
} catch {
|
|
// Cache corrupt — refetch
|
|
}
|
|
}
|
|
|
|
// Fetch from GitHub raw content
|
|
const url = `https://raw.githubusercontent.com/${REPO}/main/${TEMPLATES_DIR}/${MANIFEST_FILENAME}`;
|
|
try {
|
|
const res = await fetch(url, { signal: AbortSignal.timeout(5_000) });
|
|
if (!res.ok) {
|
|
throw new Error(`HTTP ${res.status}`);
|
|
}
|
|
const data = (await res.json()) as { templates: RemoteTemplateInfo[] };
|
|
const templates = data.templates;
|
|
|
|
// Write cache
|
|
mkdirSync(CACHE_DIR, { recursive: true });
|
|
const cache: ManifestCache = { fetchedAt: Date.now(), templates };
|
|
writeFileSync(MANIFEST_CACHE_PATH, JSON.stringify(cache), "utf-8");
|
|
|
|
return templates;
|
|
} catch {
|
|
// Offline or rate-limited — return empty (caller should fall back to bundled)
|
|
return [];
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Download a template from GitHub into destDir using giget.
|
|
* Fetches from `registry/examples/<templateId>` in the hyperframes repo.
|
|
*/
|
|
export async function fetchRemoteTemplate(
|
|
templateId: string,
|
|
destDir: string,
|
|
options?: { ref?: string },
|
|
): Promise<void> {
|
|
// Validate against manifest before downloading
|
|
const known = await listRemoteTemplates();
|
|
if (known.length > 0 && !known.some((t) => t.id === templateId)) {
|
|
const available = known.map((t) => t.id).join(", ");
|
|
throw new Error(`Template "${templateId}" not found. Available: ${available}`);
|
|
}
|
|
|
|
const { downloadTemplate } = await import("giget");
|
|
const ref = options?.ref ?? "main";
|
|
const source = `github:${REPO}/${TEMPLATES_DIR}/${templateId}#${ref}`;
|
|
|
|
await downloadTemplate(source, {
|
|
dir: destDir,
|
|
force: true,
|
|
});
|
|
|
|
// Safety check — giget can succeed with empty dir if path doesn't exist
|
|
if (!existsSync(join(destDir, "index.html"))) {
|
|
throw new Error(
|
|
`Template "${templateId}" downloaded but missing index.html. The template may be malformed.`,
|
|
);
|
|
}
|
|
}
|