Files
lucide/scripts/render/renderIconsObject.mjs
Eric Fennis 318c024589 Migrate to PNPM (#777)
* add pnpm

* make it work

* fix comamnds in package.jsons

* move some scripts to modules

* workflow fixes

* test workflow

* test #2

* minor fix

* update lockflite

* create workflows

* update workflow

* Add copy license command

* Fix build

* update workflows

* update contributions.md

* migrate site directory to pnpm

* Fix peer dependencies when install

* fix types in lucide-angular

* fix testing
2022-08-10 09:10:53 +02:00

41 lines
1.3 KiB
JavaScript

/* eslint-disable import/no-extraneous-dependencies */
import { basename } from 'path';
import { parseSync } from 'svgson';
import { generateHashedKey, readSvg, hasDuplicatedChildren } from '../helpers.mjs';
/**
* Build an object in the format: `{ <name>: <contents> }`.
* @param {string[]} svgFiles - A list of filenames.
* @param {Function} getSvg - A function that returns the contents of an SVG file given a filename.
* @returns {Object}
*/
export default (svgFiles, iconsDirectory, renderUniqueKey = false) =>
svgFiles
.map(svgFile => {
const name = basename(svgFile, '.svg');
const svg = 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 };
})
.reduce((icons, icon) => {
icons[icon.name] = icon.contents;
return icons;
}, {});