| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- import {
- cleanupSVG,
- // exportJSONPackage,
- // exportIconPackage,
- exportToDirectory,
- importDirectory,
- isEmptyColor,
- parseColors,
- runSVGO,
- } from '@iconify/tools';
- import fs from 'fs';
- import path from 'path';
- (async () => {
- const singleColorSourceDir = path.resolve(process.cwd(), 'svgs', 'single-color');
- const multiColorSourceDir = path.resolve(process.cwd(), 'svgs', 'multi-color');
- // const pkgOutputDir = path.resolve(process.cwd(), 'src', 'iconify');
- const expOutputDir = path.resolve(process.cwd(), 'src', 'icons');
- // Import icons
- const singleColorIconSet = await importDirectory(singleColorSourceDir, { prefix: 'local' });
- const multiColorIconSet = await importDirectory(multiColorSourceDir, { prefix: 'local' });
- const handleIcons = async (iconSet, multiColor) => {
- // Validate, clean up, fix palette and optimise
- await iconSet.forEach(async (name, type) => {
- if (type !== 'icon') {
- return;
- }
- const svg = iconSet.toSVG(name);
- if (!svg) {
- // Invalid icon
- iconSet.remove(name);
- return;
- }
- // Clean up and optimise icons
- try {
- // Clean up icon code
- await cleanupSVG(svg);
- if (!multiColor) {
- // Assume icon is monotone: replace color with currentColor, add if missing
- // If icon is not monotone, remove this code
- await parseColors(svg, {
- defaultColor: 'currentColor',
- callback: (attr, colorStr, color) => {
- return !color || isEmptyColor(color) ? colorStr : 'currentColor';
- },
- });
- }
- // Optimise
- await runSVGO(svg);
- } catch (err) {
- // Invalid icon
- console.error(`Error parsing ${name}:`, err);
- iconSet.remove(name);
- return;
- }
- // Update icon
- iconSet.fromSVG(name, svg);
- });
- };
- handleIcons(singleColorIconSet, false);
- handleIcons(multiColorIconSet, true);
- const removeDir = (deletePath) => {
- let files = [];
- if (fs.existsSync(deletePath)) {
- files = fs.readdirSync(deletePath);
- files.forEach((file) => {
- const curPath = path.join(deletePath, file);
- if (fs.statSync(curPath).isDirectory()) {
- removeDir(curPath);
- } else {
- fs.unlinkSync(curPath);
- }
- });
- fs.rmdirSync(deletePath);
- }
- };
- removeDir(expOutputDir);
- // removeDir(pkgOutputDir);
- await exportToDirectory(singleColorIconSet, {
- target: path.join(expOutputDir, '.single-color'),
- log: true,
- });
- // await exportIconPackage(singleColorIconSet, {
- // target: path.join(pkgOutputDir, '.single-color'),
- // cleanup: true,
- // });
- await exportToDirectory(multiColorIconSet, {
- target: path.join(expOutputDir, '.multi-color'),
- log: true,
- });
- // await exportIconPackage(multiColorIconSet, {
- // target: path.join(pkgOutputDir, '.multi-color'),
- // cleanup: true,
- // });
- const mergeFiles = (parentPath) => {
- function copyFile(distPath, sourcePath) {
- const rs = fs.createReadStream(sourcePath);
- rs.on('error', (err) => console.log(err));
- const ws = fs.createWriteStream(distPath);
- ws.on('error', (err) => console.log(err));
- rs.pipe(ws);
- }
- const copyToParent = (childName) => {
- const childPath = path.resolve(parentPath, childName);
- if (!fs.existsSync(childPath)) return;
- const files = fs.readdirSync(childPath);
- files.forEach((file) => {
- const curFilePath = path.join(childPath, file);
- const fileState = fs.statSync(curFilePath);
- if (fileState.isFile() /* && file != 'package.json' */) {
- copyFile(path.join(parentPath, file), curFilePath);
- }
- });
- };
- copyToParent('.single-color');
- copyToParent('.multi-color');
- setTimeout(() => {
- removeDir(path.resolve(parentPath, '.single-color'));
- removeDir(path.resolve(parentPath, '.multi-color'));
- }, 3000);
- };
- mergeFiles(expOutputDir);
- // mergeFiles(pkgOutputDir);
- })();
|