|
| 1 | +import { defineCommand } from "citty"; |
| 2 | +import type { Example } from "./_examples.js"; |
| 3 | + |
| 4 | +export const examples: Example[] = [ |
| 5 | + ["List all blocks and components", "hyperframes catalog"], |
| 6 | + ["List blocks only", "hyperframes catalog --type block"], |
| 7 | + ["Filter by tag", "hyperframes catalog --type block --tag social"], |
| 8 | + ["Machine-readable JSON", "hyperframes catalog --json"], |
| 9 | + ["Interactive picker (install on select)", "hyperframes catalog --human-friendly"], |
| 10 | +]; |
| 11 | + |
| 12 | +import * as clack from "@clack/prompts"; |
| 13 | +import { type ItemType } from "@hyperframes/core"; |
| 14 | +import { c } from "../ui/colors.js"; |
| 15 | +import { listRegistryItems, loadAllItems } from "../registry/resolver.js"; |
| 16 | +import { loadProjectConfig, DEFAULT_PROJECT_CONFIG } from "../utils/projectConfig.js"; |
| 17 | +import { resolve } from "node:path"; |
| 18 | +import { runAdd } from "./add.js"; |
| 19 | + |
| 20 | +export default defineCommand({ |
| 21 | + meta: { |
| 22 | + name: "catalog", |
| 23 | + description: "Browse and install blocks and components from the registry", |
| 24 | + }, |
| 25 | + args: { |
| 26 | + type: { |
| 27 | + type: "string", |
| 28 | + description: 'Filter by type: "block" or "component"', |
| 29 | + }, |
| 30 | + tag: { |
| 31 | + type: "string", |
| 32 | + description: "Filter by tag (e.g. social, transition, text)", |
| 33 | + }, |
| 34 | + json: { |
| 35 | + type: "boolean", |
| 36 | + description: "Print matching items as JSON to stdout", |
| 37 | + }, |
| 38 | + "human-friendly": { |
| 39 | + type: "boolean", |
| 40 | + description: "Interactive picker — select an item to install", |
| 41 | + }, |
| 42 | + }, |
| 43 | + async run({ args }) { |
| 44 | + const json = args.json === true; |
| 45 | + const interactive = args["human-friendly"] === true; |
| 46 | + const dir = resolve(process.cwd()); |
| 47 | + const config = loadProjectConfig(dir) ?? DEFAULT_PROJECT_CONFIG; |
| 48 | + |
| 49 | + let typeFilter: ItemType | undefined; |
| 50 | + if (args.type === "block") typeFilter = "hyperframes:block"; |
| 51 | + else if (args.type === "component") typeFilter = "hyperframes:component"; |
| 52 | + else if (args.type) { |
| 53 | + console.error(`Invalid --type: "${args.type}". Use "block" or "component".`); |
| 54 | + process.exit(1); |
| 55 | + } |
| 56 | + |
| 57 | + const entries = await listRegistryItems(typeFilter ? { type: typeFilter } : undefined, { |
| 58 | + baseUrl: config.registry, |
| 59 | + }); |
| 60 | + const filtered = entries.filter((e) => e.type !== "hyperframes:example"); |
| 61 | + |
| 62 | + if (filtered.length === 0) { |
| 63 | + if (json) console.log("[]"); |
| 64 | + else console.log("No items found in registry."); |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + const items = await loadAllItems(filtered, { baseUrl: config.registry }); |
| 69 | + |
| 70 | + const tagFilter = args.tag?.toLowerCase(); |
| 71 | + const matching = tagFilter |
| 72 | + ? items.filter((item) => item.tags?.some((t) => t.toLowerCase() === tagFilter)) |
| 73 | + : items; |
| 74 | + |
| 75 | + if (matching.length === 0) { |
| 76 | + if (json) console.log("[]"); |
| 77 | + else console.log(`No items match tag "${args.tag}".`); |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + if (json) { |
| 82 | + const output = matching.map((item) => ({ |
| 83 | + name: item.name, |
| 84 | + type: item.type.replace("hyperframes:", ""), |
| 85 | + title: item.title, |
| 86 | + description: item.description, |
| 87 | + tags: item.tags ?? [], |
| 88 | + ...("dimensions" in item && item.dimensions ? { dimensions: item.dimensions } : {}), |
| 89 | + ...("duration" in item && item.duration ? { duration: item.duration } : {}), |
| 90 | + })); |
| 91 | + console.log(JSON.stringify(output, null, 2)); |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + if (interactive) { |
| 96 | + const options = matching.map((item) => ({ |
| 97 | + value: item.name, |
| 98 | + label: item.name, |
| 99 | + hint: item.description, |
| 100 | + })); |
| 101 | + |
| 102 | + const selected = await clack.select({ |
| 103 | + message: `${matching.length} items available — pick one to install`, |
| 104 | + options, |
| 105 | + }); |
| 106 | + |
| 107 | + if (clack.isCancel(selected)) { |
| 108 | + clack.cancel("Cancelled."); |
| 109 | + process.exit(0); |
| 110 | + } |
| 111 | + |
| 112 | + const result = await runAdd({ |
| 113 | + name: selected as string, |
| 114 | + projectDir: dir, |
| 115 | + skipClipboard: false, |
| 116 | + }); |
| 117 | + |
| 118 | + console.log(""); |
| 119 | + console.log(`${c.success("✓")} Installed ${c.accent(result.name)} (${result.type})`); |
| 120 | + for (const file of result.written) { |
| 121 | + const rel = file.replace(dir + "/", ""); |
| 122 | + console.log(` ${c.dim(rel)}`); |
| 123 | + } |
| 124 | + if (result.snippet) { |
| 125 | + console.log(""); |
| 126 | + console.log(c.dim("Include snippet:")); |
| 127 | + console.log(` ${result.snippet}`); |
| 128 | + } |
| 129 | + return; |
| 130 | + } |
| 131 | + |
| 132 | + const NAME_COL = 28; |
| 133 | + const TYPE_COL = 12; |
| 134 | + console.log( |
| 135 | + `${c.bold("Name".padEnd(NAME_COL))}${c.bold("Type".padEnd(TYPE_COL))}${c.bold("Description")}`, |
| 136 | + ); |
| 137 | + console.log("-".repeat(80)); |
| 138 | + |
| 139 | + for (const item of matching) { |
| 140 | + const type = item.type.replace("hyperframes:", ""); |
| 141 | + const tags = item.tags?.length ? c.dim(` [${item.tags.join(", ")}]`) : ""; |
| 142 | + console.log( |
| 143 | + `${c.cyan(item.name.padEnd(NAME_COL))}${type.padEnd(TYPE_COL)}${item.description}${tags}`, |
| 144 | + ); |
| 145 | + } |
| 146 | + |
| 147 | + console.log(""); |
| 148 | + console.log(c.dim(`${matching.length} items. Run "hyperframes add <name>" to install.`)); |
| 149 | + }, |
| 150 | +}); |
0 commit comments