mirror of
https://github.com/shinyobjectz/raea.git
synced 2026-12-01 12:44:11 -10:00
* The component library: 609 open-source effects sorted by use, searched by `raea make component list WORDS` and copied by `add NAME` sdk/raea/library/components holds 475 pieces from remocn, 79 from the React Video Editor templates and 55 from snapcn, ported to raea's composition API and sorted by what they're for (text, numbers, transitions, screens, HUDs and more), not by who made them. catalog.json lists each piece's category, type, what it does, its files, the shared helpers it needs, its fonts and npm packages and its source; CREDITS.md holds the licences and every file's first line names its source. - `raea make component list` ends with the library's categories; `list WORDS` searches by category, category/type or words (catalog.ts). `add NAME` copies the piece into components/CATEGORY/NAME/ with its helpers under components/_lib/, and installs its npm packages through the engine. - The native host reads the library beside the binary (share/raea, which tools/release.sh already packs from sdk/raea/library), in RAEA_HOME, or in the tree a built raea runs from. - tools/effects-catalog.sh writes the cut-down catalogue raea cloud's dashboard imports (Libraries › Effects). - tools/size.sh leaves the library out: it's other people's code, kept as they wrote it but for imports. - conformance/catalog.test.ts: every listed file exists, imports stay inside what a piece copies, none imports another video framework, and search finds by use. * raea help's line for make component says list WORDS searches the library * no-void leaves the component library out: its gradient shader's own name is void --------- Co-authored-by: shinyobjectz <[email protected]>
81 lines
5.7 KiB
TypeScript
81 lines
5.7 KiB
TypeScript
// The component library (sdk/raea/library/components): other people's open components, sorted by what they're for,
|
|
// not by who wrote them. catalog.json lists each: its category and type, what it does, its own files, the shared files
|
|
// it needs, the fonts it loads and where it came from (CREDITS.md beside it holds the licences). `raea make component
|
|
// list WORDS` searches it and `raea make component add NAME` copies one into the project (index.ts). The host half
|
|
// (the files) runs on the native raea; the rest is plain strings.
|
|
import { existsSync, readFileSync, realpathSync } from "node:fs";
|
|
import { dirname, join } from "node:path";
|
|
|
|
export type Item = { name: string; category: string; type: string; says: string; component: string; default?: boolean; files: string[]; needs: string[]; fonts: string[]; packages?: string[]; from: string };
|
|
type Catalog = { sources: Record<string, { url: string; licence: string }>; items: Item[] };
|
|
export type Got = { item: Item; licence: string; files: Record<string, string> };
|
|
|
|
/** The library's folder: in RAEA_HOME, beside a release's bin/raea (share/raea), or in the tree a built raea is in */
|
|
function home(raea: string, env: Record<string, string | undefined>): string | undefined {
|
|
const bin = dirname(realpathSync(raea));
|
|
const rel = "sdk/raea/library/components";
|
|
return [env.RAEA_HOME && join(env.RAEA_HOME, rel), join(bin, "../share/raea", rel), join(bin, "../..", rel)].find((p) => p && existsSync(join(p, "catalog.json")));
|
|
}
|
|
|
|
/** Host op "components": {op: "list"} is every item, {op: "get", name} one item with its files' text (by its path in
|
|
* the library, which is its path under components/). No library beside this raea: an empty list, and no item. */
|
|
export function componentsHost(raea: string, env: Record<string, string | undefined>, a: Record<string, unknown>): Item[] | Got | null {
|
|
const dir = home(raea, env);
|
|
if (!dir) return a.op === "get" ? null : [];
|
|
const cat = JSON.parse(readFileSync(join(dir, "catalog.json"), "utf8")) as Catalog;
|
|
if (a.op !== "get") return cat.items;
|
|
const want = String(a.name).replace(/^\/+|\/+$/g, "");
|
|
const item = cat.items.find((i) => i.name === want || `${i.category}/${i.name}` === want || `${i.category}/${i.type}/${i.name}` === want);
|
|
if (!item) return null;
|
|
const files = Object.fromEntries([...item.files, ...item.needs].map((f) => [f, readFileSync(join(dir, f), "utf8")]));
|
|
return { item, licence: cat.sources[item.from]?.licence ?? "", files };
|
|
}
|
|
|
|
const row = (i: Item) => `${`${i.category}/${i.name}`.padEnd(34)} ${i.type.padEnd(13)} ${i.says.split(". ")[0].slice(0, 110)}`;
|
|
|
|
/** `raea make component list`'s lines for the library: with no words, each category and its types; with words, the
|
|
* items they name. A word is a category ("text"), a category/type ("text/reveal"), or anything in a name or in what
|
|
* an item does; every word must be found, and a name's own words count first. */
|
|
export function listed(items: Item[], words: string[]): string {
|
|
if (!items.length) return "";
|
|
if (!words.length) {
|
|
const cats = [...new Set(items.map((i) => i.category))].sort();
|
|
const line = (c: string) => {
|
|
const of = items.filter((i) => i.category === c);
|
|
const types = [...new Set(of.map((i) => i.type))].sort().map((t) => `${t} ${of.filter((i) => i.type === t).length}`);
|
|
return `${c.padEnd(14)} ${String(of.length).padStart(3)} ${types.join(", ")}`;
|
|
};
|
|
return `\nThe library, by what a component is for (raea make component list text, list text/reveal, list "count up"; add NAME copies one):\n${cats.map(line).join("\n")}\n`;
|
|
}
|
|
const ws = words.join(" ").toLowerCase().split(/\s+/).filter(Boolean);
|
|
const phrase = ws.join(" ");
|
|
const score = (i: Item) => {
|
|
// the words together, as written, in what it does: ahead of the same words scattered
|
|
let s = ws.length > 1 && i.says.toLowerCase().includes(phrase) ? 3 : 0;
|
|
for (const w of ws) {
|
|
const [c, t] = w.split("/");
|
|
if (w.includes("/") ? i.category === c && (!t || i.type === t) : i.category === w || i.type === w) s += 4;
|
|
else if (i.name.split("-").includes(w)) s += 3;
|
|
else if (i.name.includes(w)) s += 2;
|
|
else if (i.says.toLowerCase().includes(w)) s += 1;
|
|
else return 0;
|
|
}
|
|
return s;
|
|
};
|
|
const found = items.map((i) => [score(i), i] as const).filter(([s]) => s > 0).sort((a, b) => b[0] - a[0] || a[1].name.localeCompare(b[1].name));
|
|
if (!found.length) return `no component in the library for "${words.join(" ")}" (raea make component list shows the categories)\n`;
|
|
return found.map(([, i]) => row(i)).join("\n") + "\n";
|
|
}
|
|
|
|
/** What `add` says of a copied item: where it is, how to import it, the fonts it loads, whose it was */
|
|
export function added(g: Got, home: string, from: string, wrote: number, kept: number, installed = ""): string {
|
|
const i = g.item;
|
|
const pk = i.packages?.length ? `\n npm packages: ${i.packages.join(", ")}${installed ? ` (${installed})` : ` (not installed: raea add ${i.packages.join(" ")})`}` : "";
|
|
const shared = i.needs.length ? `, ${i.needs.length} shared under ${home}/_lib and beside it` : "";
|
|
const fonts = i.fonts.length ? `\n fonts: ${i.fonts.join(", ")} (loaded by name; set your brand's in its props or the file)` : "";
|
|
return `${home}/${i.category}/${i.name}/ ${i.files.length} files${shared}${kept ? ` (${kept} already here, kept)` : ""}; ${wrote} written
|
|
import ${i.default ? i.component : `{ ${i.component} }`} from "${from}/${i.category}/${i.name}"; (${i.says.split(". ")[0]})
|
|
its props and their defaults are at the top of index.tsx (or none: then its words and colours are in the file): the source's samples, make them the brief's${fonts}${pk}
|
|
from ${i.from}, ${g.licence}: yours to change; the first line of each file keeps the credit\n`;
|
|
}
|