Files
lucide/scripts/checkIconsAndCategories.mts
Eric Fennis 3e644fda2d chore(scripts): Refactor scripts to typescript (#3316)
* Adjust typescript types

* adjust types

* fix types in all helper files

* Fix types

* Migrate js files to ts files

* Refactor to TS files

* Rename extentions

* Adjust imports

* Fix builds

* Update lockfile

* Fix last typescript migration

* Fix entry path @lucide/outline-svg

* Fix types

* add checkout step

* format files

* Format files
2025-06-18 15:47:24 +02:00

61 lines
1.9 KiB
TypeScript

import path from 'path';
import {
readSvgDirectory,
getCurrentDirPath,
readAllMetadata,
} from '../tools/build-helpers/helpers.ts';
import { type IconMetadata } from '../tools/build-icons/types.ts';
const currentDir = getCurrentDirPath(import.meta.url);
const ICONS_DIR = path.resolve(currentDir, '../icons');
const icons = await readAllMetadata(ICONS_DIR) as Record<string, IconMetadata>;
const CATEGORIES_DIR = path.resolve(currentDir, '../categories');
const categories = await readAllMetadata(CATEGORIES_DIR) as Record<string, {
icon: string;
name: string;
}>;;
console.log('Reading all icons');
const svgFiles = await readSvgDirectory(ICONS_DIR);
const iconNames = svgFiles.map((icon) => icon.split('.')[0]);
let error = false;
iconNames.forEach((iconName) => {
if (typeof icons[iconName] === 'undefined') {
console.error(`'${iconName}.svg' does not have a matching JSON file.`);
error = true;
}
});
Object.keys(icons).forEach((iconName) => {
const icon = icons[iconName];
if (iconNames.indexOf(iconName) === -1) {
console.error(`'${iconName}.svg' does not exist.`);
error = true;
}
icon.categories?.forEach((categoryName) => {
if (typeof categories[categoryName] === 'undefined') {
console.error(`Icon '${iconName}' refers to the non-existing category '${categoryName}'.`);
error = true;
}
});
});
Object.keys(categories).forEach((categoryName) => {
const category = categories[categoryName];
if (!category?.icon) {
console.error(`Category '${categoryName}' does not use an icon '${category.icon}'.`);
error = true;
} else if (typeof icons[category.icon] === 'undefined') {
console.error(`Category '${categoryName}' uses the non-existing icon '${category.icon}'.`);
error = true;
}
});
if (error) {
console.error('At least one error in icon JSONs prevents from committing changes.');
process.exit(1);
}