Skip to content

The platform for continuously improving AI agents.

3.7 KB
Raw Blame
#!/usr/bin/env bun
// Regenerates src/components/ui/icons/glyphs.ts from the pixelarticons SVG set.
//
//   bun run icons:vendor                     # PIXELARTICONS_LICENSE_KEY set: installs
//                                            # the package in a temp dir, unlocks the
//                                            # full set, vendors from there
//   bun run icons:vendor --source <svg dir>  # an unlocked package's svg/ directory
//
// The glyph list is whatever the registry names in `glyph("...")` calls, so adding an
// icon is one registry line plus one run of this script.
import { execFileSync } from "node:child_process";
import { existsSync, mkdtempSync, readFileSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import path from "node:path";

const VERSION = "2.2.0";
const ROOT = path.resolve(import.meta.dirname, "..");
const REGISTRY = path.join(ROOT, "src/components/ui/icons/index.ts");
const OUT = path.join(ROOT, "src/components/ui/icons/glyphs.ts");

function fail(message) {
  console.error(`vendor-icons: ${message}`);
  process.exit(1);
}

function sourceDir() {
  const flag = process.argv.indexOf("--source");
  if (flag !== -1) {
    const dir = process.argv[flag + 1];
    if (!dir) fail("--source needs a directory");
    return path.resolve(dir);
  }
  const key = process.env.PIXELARTICONS_LICENSE_KEY;
  if (!key) fail("set PIXELARTICONS_LICENSE_KEY or pass --source <svg dir>");
  const dir = mkdtempSync(path.join(tmpdir(), "pixelarticons-"));
  writeFileSync(
    path.join(dir, "package.json"),
    JSON.stringify({
      dependencies: { pixelarticons: VERSION },
      name: "vendor-icons",
      private: true,
    })
  );
  execFileSync("bun", ["install", "--silent"], { cwd: dir, stdio: "inherit" });
  // The package's own upgrade command; it needs `unzip` on PATH.
  execFileSync(
    "bun",
    [path.join(dir, "node_modules/pixelarticons/bin/upgrade.js"), `--key=${key}`],
    {
      cwd: dir,
      stdio: "inherit",
    }
  );
  return path.join(dir, "node_modules/pixelarticons/svg");
}

function registryNames() {
  const source = readFileSync(REGISTRY, "utf8");
  const names = new Set(Array.from(source.matchAll(/glyph\("([a-z0-9-]+)"\)/g), (m) => m[1]));
  if (names.size === 0) fail(`no glyph("...") calls found in ${REGISTRY}`);
  return [...names].sort();
}

// Every pixelarticons glyph is `<svg … viewBox="0 0 24 24">` around bare `<path d>`
// elements; anything else means the format moved and the vendored frame no longer matches.
function pathsOf(file) {
  const svg = readFileSync(file, "utf8").trim();
  const open = svg.match(/^<svg\b([^>]*)>/);
  if (!open || !/\bviewBox="0 0 24 24"/.test(open[1]) || !svg.endsWith("</svg>")) {
    fail(`${file}: not a 24×24 svg`);
  }
  const inner = svg.slice(open[0].length, -"</svg>".length);
  const paths = Array.from(inner.matchAll(/<path\s+d="([^"]+)"\s*\/>/g), (m) => m[1]);
  if (paths.length === 0 || inner.replace(/<path\s+d="[^"]+"\s*\/>/g, "").trim() !== "") {
    fail(`${file}: markup other than <path d> is not supported`);
  }
  return paths;
}

const dir = sourceDir();
const names = registryNames();
const entries = names.map((name) => {
  const file = path.join(dir, `${name}.svg`);
  if (!existsSync(file)) fail(`no ${name}.svg in ${dir}`);
  return `  ${JSON.stringify(name)}: ${JSON.stringify(pathsOf(file))},`;
});

writeFileSync(
  OUT,
  [
    `// Generated by \`bun run icons:vendor\` from pixelarticons SVG sources. Do not edit.`,
    "export const glyphs = {",
    ...entries,
    "} as const;",
    "",
    "export type GlyphName = keyof typeof glyphs;",
    "",
  ].join("\n")
);
execFileSync("bun", ["x", "biome", "check", "--write", OUT], { cwd: ROOT, stdio: "inherit" });
console.log(`vendor-icons: wrote ${names.length} glyphs to ${path.relative(ROOT, OUT)}`);