Skills › HyperFrames › media-use › index-gen.mjs
index-gen.mjs
63 lines · skills/media-use/scripts/lib/index-gen.mjs at a595755
1import { writeFileSync, mkdirSync } from "node:fs";
2import { dirname } from "node:path";
3import { currentRecords, indexPath } from "./manifest.mjs";
5function pad(str, len) {
6 return String(str ?? "").padEnd(len);
7}
9function formatDur(record) {
10 if (record.duration == null) return "—";
11 return `${record.duration}s`;
12}
14function formatDims(record) {
15 if (record.width && record.height) return `${record.width}×${record.height}`;
16 if (record.type === "icon" && record.transparent) return "svg";
17 return "—";
18}
20export function generateIndexContent(records) {
21 const count = records.length;
22 const header = `# .media · ${count} asset${count === 1 ? "" : "s"}\n`;
23 if (count === 0) return header;
25 const cols = { id: 4, type: 5, dur: 4, dims: 5, path: 5, desc: 11 };
26 for (const r of records) {
27 cols.id = Math.max(cols.id, (r.id ?? "").length);
28 cols.type = Math.max(cols.type, (r.type ?? "").length);
29 cols.dur = Math.max(cols.dur, formatDur(r).length);
30 cols.dims = Math.max(cols.dims, formatDims(r).length);
31 cols.path = Math.max(cols.path, (r.path ?? "").length);
32 }
34 const heading =
35 pad("id", cols.id + 2) +
36 pad("type", cols.type + 2) +
37 pad("dur", cols.dur + 2) +
38 pad("dims", cols.dims + 2) +
39 pad("path", cols.path + 2) +
40 "description";
42 const lines = [header, heading];
43 for (const r of records) {
44 lines.push(
45 pad(r.id, cols.id + 2) +
46 pad(r.type, cols.type + 2) +
47 pad(formatDur(r), cols.dur + 2) +
48 pad(formatDims(r), cols.dims + 2) +
49 pad(r.path, cols.path + 2) +
50 (r.description ?? ""),
51 );
52 }
53 return lines.join("\n") + "\n";
54}
56export function regenerateIndex(projectDir) {
57 const records = currentRecords(projectDir);
58 const content = generateIndexContent(records);
59 const p = indexPath(projectDir);
60 mkdirSync(dirname(p), { recursive: true });
61 writeFileSync(p, content);
62 return content;
63}