Files
lucide/tools/build-icons/render/renderIconsObject.ts
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

47 lines
1.4 KiB
TypeScript

import { basename } from 'path';
import { type INode, parseSync } from 'svgson';
import { generateHashedKey, readSvg, hasDuplicatedChildren } from '@lucide/helpers';
/**
* Build an object in the format: `{ <name>: <contents> }`.
* @param {string[]} svgFiles - A list of filenames.
* @param {string} iconsDirectory - The directory where the icons are stored.
* @returns {Object}
*/
export default async function generateIconObject(
svgFiles: string[],
iconsDirectory: string,
renderUniqueKey = false,
) {
const svgsContentPromises = svgFiles.map(async (svgFile) => {
const name = basename(svgFile, '.svg');
const svg = await readSvg(svgFile, iconsDirectory);
const contents = parseSync(svg);
if (!(contents.children && contents.children.length)) {
throw new Error(`${name}.svg has no children!`);
}
if (hasDuplicatedChildren(contents.children)) {
throw new Error(`Duplicated children in ${name}.svg`);
}
if (renderUniqueKey) {
contents.children = contents.children.map((child) => {
child.attributes.key = generateHashedKey(child);
return child;
});
}
return { name, contents };
});
const svgsContents = await Promise.all(svgsContentPromises);
return svgsContents.reduce<Record<string, INode>>((icons, icon) => {
icons[icon.name] = icon.contents;
return icons;
}, {});
}