Skip to content

#1 PDF Application on GitHub that lets you edit PDFs on any device anywhere

2.1 KB
Raw Blame
/** Which icons source references. A heuristic, and only ever a report: nothing is dropped on its say-so. */
import fs from "node:fs";
import path from "node:path";

// Icon-shaped positions only: a bare literal scan sweeps in "grape" (a Mantine colour) and "cat" (a language code).
const PATTERNS = [
  /\bname\s*[:=]\s*"([a-z0-9-]+)"/g,
  /\bname\s*=\s*\{([^}]*)\}/g,
  /\b\w*icon\w*\s*[:=]\s*"([a-z0-9-]+)"/gi,
  /\b\w*icon\w*\s*=\s*\{([^}]*)\}/gi,
];

/** The names src/editor/core/icons/icons.ts maps. */
export function iconNames(file: string): string[] {
  const src = fs.readFileSync(file, "utf8");
  return [...src.matchAll(/^ {2}"?([a-z0-9][a-z0-9-]*)"?: \{/gm)].map(
    (m) => m[1],
  );
}

/** The members of `known` that anything under `srcDir` references. */
export function scanUsedIcons(
  srcDir: string,
  known: ReadonlySet<string>,
): Set<string> {
  const found = new Set<string>();
  // core/icons writes every name down, so reading it back reports the whole set as used.
  const iconsDir = path.join(srcDir, "editor/core/icons");
  const collect = (chunk: string): void => {
    for (const m of chunk.matchAll(/"([a-z0-9][a-z0-9-]*)"/g))
      if (known.has(m[1])) found.add(m[1]);
    if (known.has(chunk)) found.add(chunk);
  };
  const walk = (dir: string): void => {
    for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
      const p = path.join(dir, entry.name);
      if (entry.isDirectory()) {
        if (entry.name !== "node_modules") walk(p);
        continue;
      }
      if (!/\.(tsx?|json)$/.test(entry.name)) continue;
      if (p.startsWith(iconsDir)) continue;
      const text = fs.readFileSync(p, "utf8");
      if (entry.name.endsWith(".json")) {
        for (const m of text.matchAll(/"icon(?:Name)?"\s*:\s*"([^"]+)"/g))
          if (known.has(m[1])) found.add(m[1]);
        continue;
      }
      // A file naming IconName is a lookup table, so every literal in it counts.
      if (text.includes("IconName")) {
        collect(text);
        continue;
      }
      for (const re of PATTERNS)
        for (const m of text.matchAll(re)) collect(m[1]);
    }
  };
  walk(srcDir);
  return found;
}