convertSVG.mjs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import {
  2. cleanupSVG,
  3. // exportJSONPackage,
  4. // exportIconPackage,
  5. exportToDirectory,
  6. importDirectory,
  7. isEmptyColor,
  8. parseColors,
  9. runSVGO,
  10. } from '@iconify/tools';
  11. import fs from 'fs';
  12. import path from 'path';
  13. (async () => {
  14. const singleColorSourceDir = path.resolve(process.cwd(), 'svgs', 'single-color');
  15. const multiColorSourceDir = path.resolve(process.cwd(), 'svgs', 'multi-color');
  16. // const pkgOutputDir = path.resolve(process.cwd(), 'src', 'iconify');
  17. const expOutputDir = path.resolve(process.cwd(), 'src', 'icons');
  18. // Import icons
  19. const singleColorIconSet = await importDirectory(singleColorSourceDir, { prefix: 'local' });
  20. const multiColorIconSet = await importDirectory(multiColorSourceDir, { prefix: 'local' });
  21. const handleIcons = async (iconSet, multiColor) => {
  22. // Validate, clean up, fix palette and optimise
  23. await iconSet.forEach(async (name, type) => {
  24. if (type !== 'icon') {
  25. return;
  26. }
  27. const svg = iconSet.toSVG(name);
  28. if (!svg) {
  29. // Invalid icon
  30. iconSet.remove(name);
  31. return;
  32. }
  33. // Clean up and optimise icons
  34. try {
  35. // Clean up icon code
  36. await cleanupSVG(svg);
  37. if (!multiColor) {
  38. // Assume icon is monotone: replace color with currentColor, add if missing
  39. // If icon is not monotone, remove this code
  40. await parseColors(svg, {
  41. defaultColor: 'currentColor',
  42. callback: (attr, colorStr, color) => {
  43. return !color || isEmptyColor(color) ? colorStr : 'currentColor';
  44. },
  45. });
  46. }
  47. // Optimise
  48. await runSVGO(svg);
  49. } catch (err) {
  50. // Invalid icon
  51. console.error(`Error parsing ${name}:`, err);
  52. iconSet.remove(name);
  53. return;
  54. }
  55. // Update icon
  56. iconSet.fromSVG(name, svg);
  57. });
  58. };
  59. handleIcons(singleColorIconSet, false);
  60. handleIcons(multiColorIconSet, true);
  61. const removeDir = (deletePath) => {
  62. let files = [];
  63. if (fs.existsSync(deletePath)) {
  64. files = fs.readdirSync(deletePath);
  65. files.forEach((file) => {
  66. const curPath = path.join(deletePath, file);
  67. if (fs.statSync(curPath).isDirectory()) {
  68. removeDir(curPath);
  69. } else {
  70. fs.unlinkSync(curPath);
  71. }
  72. });
  73. fs.rmdirSync(deletePath);
  74. }
  75. };
  76. removeDir(expOutputDir);
  77. // removeDir(pkgOutputDir);
  78. await exportToDirectory(singleColorIconSet, {
  79. target: path.join(expOutputDir, '.single-color'),
  80. log: true,
  81. });
  82. // await exportIconPackage(singleColorIconSet, {
  83. // target: path.join(pkgOutputDir, '.single-color'),
  84. // cleanup: true,
  85. // });
  86. await exportToDirectory(multiColorIconSet, {
  87. target: path.join(expOutputDir, '.multi-color'),
  88. log: true,
  89. });
  90. // await exportIconPackage(multiColorIconSet, {
  91. // target: path.join(pkgOutputDir, '.multi-color'),
  92. // cleanup: true,
  93. // });
  94. const mergeFiles = (parentPath) => {
  95. function copyFile(distPath, sourcePath) {
  96. const rs = fs.createReadStream(sourcePath);
  97. rs.on('error', (err) => console.log(err));
  98. const ws = fs.createWriteStream(distPath);
  99. ws.on('error', (err) => console.log(err));
  100. rs.pipe(ws);
  101. }
  102. const copyToParent = (childName) => {
  103. const childPath = path.resolve(parentPath, childName);
  104. if (!fs.existsSync(childPath)) return;
  105. const files = fs.readdirSync(childPath);
  106. files.forEach((file) => {
  107. const curFilePath = path.join(childPath, file);
  108. const fileState = fs.statSync(curFilePath);
  109. if (fileState.isFile() /* && file != 'package.json' */) {
  110. copyFile(path.join(parentPath, file), curFilePath);
  111. }
  112. });
  113. };
  114. copyToParent('.single-color');
  115. copyToParent('.multi-color');
  116. setTimeout(() => {
  117. removeDir(path.resolve(parentPath, '.single-color'));
  118. removeDir(path.resolve(parentPath, '.multi-color'));
  119. }, 3000);
  120. };
  121. mergeFiles(expOutputDir);
  122. // mergeFiles(pkgOutputDir);
  123. })();