Files

49 lines
1.2 KiB
TypeScript
Raw Permalink Normal View History

2021-11-21 20:27:15 +01:00
/* eslint-disable import/no-extraneous-dependencies */
import { type INode, stringify } from 'svgson';
2021-11-21 20:27:15 +01:00
import { format } from 'prettier';
import { appendFile } from '@lucide/helpers';
import { type SVGFile } from './readSvgs.mts';
2021-11-21 20:27:15 +01:00
export default async function generateSprite(
svgs: SVGFile[],
packageDir: string,
license: string
) {
const symbols = svgs.map<INode>(({ name, parsedSvg }) => ({
2021-11-21 20:27:15 +01:00
name: 'symbol',
type: 'element',
value: '',
2021-11-21 20:27:15 +01:00
attributes: {
id: name,
viewBox: parsedSvg.attributes.viewBox,
2021-11-21 20:27:15 +01:00
},
children: parsedSvg.children,
}));
const spriteSvgObject: INode = {
2021-11-21 20:27:15 +01:00
name: 'svg',
type: 'element',
attributes: {
xmlns: 'http://www.w3.org/2000/svg',
version: '1.1',
},
value: '',
2021-11-21 20:27:15 +01:00
children: [
{
name: 'defs',
type: 'element',
children: symbols,
value: '',
} as INode,
2021-11-21 20:27:15 +01:00
],
};
const spriteSvg = stringify(spriteSvgObject);
const prettifiedSprite = (await format(spriteSvg, { parser: 'babel' })).replace(/;/g, '');
2021-11-21 20:27:15 +01:00
2023-11-17 11:12:31 +01:00
const xmlMeta = `<?xml version="1.0" encoding="utf-8"?>\n<!-- ${license} -->\n`;
2021-11-21 20:27:15 +01:00
2025-02-10 14:13:52 +01:00
await appendFile(xmlMeta, `sprite.svg`, packageDir);
await appendFile(prettifiedSprite, `sprite.svg`, packageDir);
2021-11-21 20:27:15 +01:00
}