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); })();