Files
lucide/scripts/generateSuperSVG.mjs
T
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

60 lines
1.7 KiB
JavaScript

/* eslint-disable import/no-extraneous-dependencies */
import path from 'path';
import { stringify, parseSync } from 'svgson';
import prettier from 'prettier';
import { appendFile, readSvgDirectory, getCurrentDirPath } from './helpers.mjs';
/* eslint-disable import/no-extraneous-dependencies */
import readSvgs from '../packages/lucide-static/scripts/readSvgs.mjs';
const currentDir = getCurrentDirPath(import.meta.url)
const ICONS_DIR = path.resolve('icons');
const PACKAGE_DIR = path.resolve(currentDir);
export default function generateSprite(svgs, packageDir) {
const symbols = svgs.map(({ name, parsedSvg }, index) => {
const itemsPerRow = 10;
const numInRow = index % itemsPerRow;
const column = numInRow + 1;
const row = (index - numInRow) / itemsPerRow + 1;
return {
name: 'g',
type: 'element',
attributes: {
transform: `translate(${column * 24 - 24},${row * 24 - 24})`,
},
children: [parsedSvg],
};
});
const spriteSvgObject = {
name: 'svg',
type: 'element',
attributes: {
xmlns: 'http://www.w3.org/2000/svg',
version: '1.1',
},
children: symbols,
};
const spriteSvg = stringify(spriteSvgObject);
const prettifiedSprite = prettier.format(spriteSvg, { parser: 'babel' }).replace(/;/g, '');
const xmlMeta = `<?xml version="1.0" encoding="utf-8"?>\n`;
appendFile(xmlMeta, `super-sprite.svg`, packageDir);
appendFile(prettifiedSprite, `super-sprite.svg`, packageDir);
}
const svgFiles = readSvgDirectory(ICONS_DIR);
const svgs = readSvgs(svgFiles, ICONS_DIR);
const parsedSvgs = svgs.map(({ name, contents }) => ({
name,
contents,
parsedSvg: parseSync(contents),
}));
generateSprite(parsedSvgs, PACKAGE_DIR);