Skip to content

A one-of-a-kind resume builder that keeps your privacy in mind. Completely secure, customizable, portable, open-source and free forever. Try it out today!

3.9 KB
Raw Blame
import { mkdir, readdir, rm, writeFile } from "node:fs/promises";
import { createRequire } from "node:module";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
import { renderToStaticMarkup } from "react-dom/server";
import { defaultSectionIconNames } from "@reactive-resume/schema/resume/section-icons";

/**
 * Writes `packages/pdf/src/forme/phosphor-icons/`: every regular-weight Phosphor icon as the SVG markup inside its
 * 256×256 viewBox, keyed by its kebab-case name ("address-book"). The PDF engine draws icons from these strings: the
 * common ones load first, the rest only when a render draws one of them.
 */

const require = createRequire(import.meta.url);
const defsDirectory = join(dirname(require.resolve("@phosphor-icons/react")), "defs");
const target = fileURLToPath(new URL("../../packages/pdf/src/forme/phosphor-icons/", import.meta.url));

// Icons most resumes draw, loaded as one file: default section icons, contact icons and network icons. A name
// missing here still draws; it just loads with the rest.
const COMMON = new Set<string>([
	...Object.values(defaultSectionIconNames),
	"at",
	"envelope",
	"globe",
	"map-pin",
	"phone",
	"star",
	"github-logo",
	"linkedin-logo",
	"twitter-logo",
	"x-logo",
	"facebook-logo",
	"instagram-logo",
	"youtube-logo",
	"stack-overflow-logo",
	"medium-logo",
	"dribbble-logo",
	"behance-logo",
	"code",
	"git-branch",
]);

// "AddressBookTabs" → "address-book-tabs", "XLogo" → "x-logo", "NumberCircleOne" → "number-circle-one".
export const kebab = (name: string) =>
	name
		.replace(/([a-z0-9])([A-Z])/g, "$1-$2")
		.replace(/([A-Z])([A-Z][a-z])/g, "$1-$2")
		.toLowerCase();

// Forme 0.25 misreads path numbers written back to back ("-21.11-.13-.06"), drawing some icons as blobs, so every
// number and command gets its own space-separated token.
const spacePathData = (markup: string) =>
	markup.replace(/ d="([^"]*)"/g, (_, data: string) => {
		const tokens = data.match(/[a-zA-Z]|[-+]?(?:\d*\.\d+|\d+\.?)(?:[eE][-+]?\d+)?/g) ?? [];
		return ` d="${tokens.join(" ")}"`;
	});

const files = (await readdir(defsDirectory)).filter((file) => file.endsWith(".es.js")).sort();
const icons: Record<string, string> = {};
for (const file of files) {
	const module = (await import(join(defsDirectory, file))) as { default: Map<string, unknown> };
	const regular = module.default.get("regular");
	if (!regular) continue;
	icons[kebab(file.replace(/\.es\.js$/, ""))] = spacePathData(
		renderToStaticMarkup(regular as Parameters<typeof renderToStaticMarkup>[0]),
	);
}

const common: Record<string, string> = {};
const rest: Record<string, string> = {};
for (const name of Object.keys(icons).sort()) (COMMON.has(name) ? common : rest)[name] = icons[name] as string;

await rm(target, { recursive: true, force: true });
await mkdir(target, { recursive: true });
const json = (value: unknown) => `${JSON.stringify(value, null, "\t")}\n`;
await writeFile(join(target, "common.json"), json(common));
await writeFile(join(target, "rest.json"), json(rest));

const header = "// Generated by `pnpm --filter @reactive-resume/tooling icons:pdf`. Do not edit by hand.\n";
await writeFile(
	join(target, "index.ts"),
	`${header}
/** Icons most resumes draw, in \`common.json\`. */
export const commonIcons: ReadonlySet<string> = new Set(${JSON.stringify(Object.keys(common), null, "\t")});

export const iconFiles = {
	common: (): Promise<{ default: Record<string, string> }> => import("./common.json"),
	rest: (): Promise<{ default: Record<string, string> }> => import("./rest.json"),
};
`,
);
// Every icon at once, for icon pickers.
await writeFile(
	join(target, "all.ts"),
	`${header}import common from "./common.json";
import rest from "./rest.json";

const icons: Record<string, string> = { ...common, ...rest };

export default icons;
`,
);

console.log(`Wrote ${Object.keys(icons).length} icons (${Object.keys(common).length} common) to ${target}`);