mirror of
https://github.com/lucide-icons/lucide.git
synced 2026-11-17 14:28:25 +00:00
fix(build-font): add collision detection to font codepoints & make allocation explicitly sequential (#4300)
Co-authored-by: Eric Fennis <eric.fennis@gmail.com>
This commit is contained in:
@@ -1,6 +1,4 @@
|
||||
import { type IconAliases } from '@lucide/helpers';
|
||||
import { promises as fs } from 'fs';
|
||||
import { cwd } from 'process';
|
||||
import { put } from '@vercel/blob';
|
||||
|
||||
const VERCEL_BLOB_CODEPOINTS_PATH = 'latest/font/codepoints.json';
|
||||
@@ -17,43 +15,91 @@ async function getLatestCodePoints(): Promise<CodePoints> {
|
||||
|
||||
interface AllocateCodePointsOptions {
|
||||
saveCodePoints?: boolean;
|
||||
allowFixes?: boolean;
|
||||
iconsWithAliases: IconAliases;
|
||||
}
|
||||
|
||||
export async function allocateCodePoints({
|
||||
saveCodePoints = false,
|
||||
allowFixes = false,
|
||||
iconsWithAliases,
|
||||
}: AllocateCodePointsOptions): Promise<CodePoints> {
|
||||
const baseCodePoints = await getLatestCodePoints();
|
||||
const latestCodePoints = await getLatestCodePoints();
|
||||
|
||||
const endCodePoint = Math.max(...Object.values(baseCodePoints));
|
||||
let maxCodePoint = Math.max(...Object.values(latestCodePoints));
|
||||
let codePointMap = new Map<number, string>();
|
||||
let newCodePoints: CodePoints = {};
|
||||
|
||||
await Promise.all(
|
||||
iconsWithAliases.map(async ([iconName, aliases]) => {
|
||||
if (!baseCodePoints[iconName]) {
|
||||
console.log('Code point not found creating new one for', iconName);
|
||||
baseCodePoints[iconName] = endCodePoint + 1;
|
||||
}
|
||||
for (const [iconName, aliases] of iconsWithAliases) {
|
||||
let codePoint: number | null = null;
|
||||
|
||||
aliases.forEach((alias, index) => {
|
||||
if (baseCodePoints[alias]) {
|
||||
return;
|
||||
// Check if the icon is already present.
|
||||
if (iconName in latestCodePoints) {
|
||||
codePoint = latestCodePoints[iconName];
|
||||
}
|
||||
|
||||
// Check if one of the aliases of the icon is already present, we need to add this as a legacy codepoint for backwards compatibility
|
||||
for (const alias of aliases) {
|
||||
if (alias in latestCodePoints) {
|
||||
if (codePoint && codePoint !== latestCodePoints[alias]) {
|
||||
if (codePointMap.has(latestCodePoints[alias])) {
|
||||
if (allowFixes) {
|
||||
console.warn(
|
||||
`The code point for alias '${alias}' for '${iconName}' is already allocated to: '${codePointMap.get(latestCodePoints[alias])}', we're simply not adding this custom alias codepoint.`,
|
||||
);
|
||||
} else {
|
||||
throw new Error(
|
||||
`The code point for alias '${alias}' for '${iconName}' is already allocated to: '${codePointMap.get(latestCodePoints[alias])}'.`,
|
||||
);
|
||||
}
|
||||
} else {
|
||||
console.log(
|
||||
`'${alias}' uses a different codepoint than its canonical name '${iconName}', adding a legacy alias codepoint for: ${latestCodePoints[alias]}.`,
|
||||
);
|
||||
codePointMap.set(latestCodePoints[alias], iconName);
|
||||
newCodePoints[alias] = latestCodePoints[alias];
|
||||
}
|
||||
}
|
||||
codePoint ??= latestCodePoints[alias];
|
||||
}
|
||||
}
|
||||
|
||||
console.log('Code point not found creating new one for', alias);
|
||||
// Assign a new codepoint, if no existing code point was found.
|
||||
codePoint ??= ++maxCodePoint;
|
||||
|
||||
baseCodePoints[alias] = endCodePoint + index + 1;
|
||||
});
|
||||
}),
|
||||
);
|
||||
if (allowFixes && codePointMap.has(codePoint)) {
|
||||
console.warn(
|
||||
`Assigning a new codepoint for '${iconName}', since '${codePoint}' was already taken up by '${codePointMap.get(codePoint)}'.`,
|
||||
);
|
||||
codePoint = ++maxCodePoint;
|
||||
}
|
||||
|
||||
if (codePointMap.has(codePoint)) {
|
||||
throw new Error(
|
||||
`We couldn't assign a unique codepoint for '${iconName}', since '${codePoint}' was already taken up by '${codePointMap.get(codePoint)}'`,
|
||||
);
|
||||
}
|
||||
|
||||
codePointMap.set(codePoint, iconName);
|
||||
for (const name of [iconName, ...aliases]) {
|
||||
if (!newCodePoints[name]) {
|
||||
newCodePoints[name] = codePoint;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Write the changes to the original map to keep its order.
|
||||
for (const [name, codePoint] of Object.entries(newCodePoints)) {
|
||||
latestCodePoints[name] = codePoint;
|
||||
}
|
||||
|
||||
if (saveCodePoints) {
|
||||
const content = JSON.stringify(baseCodePoints, null, 2);
|
||||
const content = JSON.stringify(latestCodePoints, null, 2);
|
||||
|
||||
await put(VERCEL_BLOB_CODEPOINTS_PATH, content, { access: 'public', allowOverwrite: true });
|
||||
|
||||
console.log('Code points uploaded to Vercel Blob Storage.');
|
||||
}
|
||||
|
||||
return baseCodePoints;
|
||||
return latestCodePoints;
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ export function hasMissingCodePoints(
|
||||
.map(([iconName, aliases]) => [iconName, ...aliases])
|
||||
.flat()
|
||||
.some((name) => {
|
||||
if (!codePoints?.[name]) {
|
||||
if (!codePoints[name]) {
|
||||
console.log(`Missing code point for icon/alias: ${name}`);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -13,7 +13,8 @@ const classNamePrefix = 'icon';
|
||||
const startUnicode = 57400;
|
||||
const outputDir = 'lucide-font';
|
||||
|
||||
const { saveCodePoints = false } = getArgumentOptions(process.argv.slice(2)) ?? {};
|
||||
const { saveCodePoints = false, allowFixes = false } =
|
||||
getArgumentOptions(process.argv.slice(2)) ?? {};
|
||||
|
||||
const repoRoot = path.join(process.cwd(), '../../');
|
||||
const iconsDir = path.join(repoRoot, 'icons');
|
||||
@@ -36,6 +37,7 @@ await outlineSVG({
|
||||
|
||||
const codePoints = await allocateCodePoints({
|
||||
saveCodePoints,
|
||||
allowFixes,
|
||||
iconsWithAliases,
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user