Skip to content

244 pixel art effects, every one a small piece of code. Plus a Claude Code skill to make your own.

2.9 KB
Raw Blame
#!/usr/bin/env node
// Regenerate the registry (src/fx/index.js) and the catalog (docs/CATALOG.md) from the effect files.
//
//   node tools/index.mjs

import { writeFileSync, readFileSync } from "node:fs";
import { join } from "node:path";
import { ROOT, FX_DIR, allIds, loadEffect } from "./lib/fxlib.mjs";
import { categories } from "../src/fx/categories.js";

const metas = [];
for (const id of allIds()) {
  try {
    metas.push({ id, ...(await loadEffect(id)).meta });
  } catch (e) {
    console.error(`skipping ${id}: ${String(e.message ?? e).split("\n")[0]}`);
  }
}
const ids = metas.map((m) => m.id);
const order = new Map(categories.map((c, i) => [c.id, i]));
const rank = (id) => order.get(id.split("/")[0]) ?? 99;
ids.sort((a, b) => rank(a) - rank(b) || a.localeCompare(b));
metas.sort((a, b) => rank(a.id) - rank(b.id) || a.id.localeCompare(b.id));

const ident = (id) => id.replace(/[^a-z0-9]+/gi, "_");
const lines = [
  "// Generated by tools/index.mjs: every effect, by id. Don't edit; run `node tools/index.mjs`.",
  "",
  ...ids.map((id) => `import * as ${ident(id)} from "./${id}.js";`),
  "",
  `export { categories } from "./categories.js";`,
  "",
  "export const effects = {",
  ...ids.map((id) => `  "${id}": ${ident(id)},`),
  "};",
  "",
];
writeFileSync(join(FX_DIR, "index.js"), lines.join("\n"));

const md = [
  "# Catalog",
  "",
  `${ids.length} effects in ${categories.filter((c) => metas.some((m) => m.id.startsWith(c.id + "/"))).length} categories. Every one is a file in`,
  "`src/fx/<category>/<name>.js`; render any of them with `node tools/render.mjs <id> --gif`.",
  "",
];
for (const c of categories) {
  const list = metas.filter((m) => m.id.startsWith(c.id + "/"));
  if (!list.length) continue;
  md.push(`## ${c.name}`, "", `${c.blurb}.`, "", "| effect | id | | size | what it does |", "|---|---|---|---|---|");
  for (const m of list)
    md.push(`| ${m.name} | \`${m.id}\` | ${m.loop ? "loop" : "one-shot"} ${+m.duration.toFixed(2)}s | ${m.size[0]}×${m.size[1]} | ${m.desc.replace(/\|/g, "\\|")} |`);
  md.push("");
}
writeFileSync(join(ROOT, "docs", "CATALOG.md"), md.join("\n"));
console.log(`${ids.length} effects -> src/fx/index.js, docs/CATALOG.md`);

// README: the count and the category table
const readme = join(ROOT, "README.md");
const rows = categories
  .map((c) => [c, metas.filter((m) => m.id.startsWith(c.id + "/"))])
  .filter(([, list]) => list.length)
  .map(([c, list]) => `| **${c.name}** | ${list.length} | ${list.slice(0, 6).map((m) => m.name.toLowerCase()).join(", ")}${list.length > 6 ? "…" : ""} |`);
const table = ["| category | effects | some of them |", "|---|---|---|", ...rows].join("\n");
let text = readFileSync(readme, "utf8");
text = text.replace(/<!-- count -->[\s\S]*?<!-- \/count -->/, `<!-- count -->${ids.length}<!-- /count -->`);
text = text.replace(/<!-- catalog -->[\s\S]*?<!-- \/catalog -->/, `<!-- catalog -->\n${table}\n<!-- /catalog -->`);
writeFileSync(readme, text);