import glob from 'glob' import { readFileSync, writeFileSync } from 'fs' import { join, basename } from 'path' import { optimizePath, ICONS_SRC_DIR } from './helpers.mjs' glob(join(ICONS_SRC_DIR, '*.svg'), {}, function(er, files) { files.forEach(function(file, i) { console.log(`Optimize ${basename(file)}`); let svgFile = readFileSync(file), svgFileContent = svgFile.toString() svgFileContent = svgFileContent.replace(/><\/(polyline|line|rect|circle|path|ellipse)>/g, '/>') .replace(/rx="([^"]+)"\s+ry="\1"/g, 'rx="$1"') .replace(/]+)?\/>/g, '') .replace(/\s?\/>/g, ' />') .replace(/\n\s*<(line|circle|path|polyline|rect|ellipse)/g, '\n <$1') // .replace(/polyline points="([0-9.]+)\s([0-9.]+)\s([0-9.]+)\s([0-9.]+)"/g, 'line x1="$1" y1="$2" x2="$3" y2="$4"') .replace(//g, function(f, x1, y1, x2, y2) { return `` }) .replace(//g, function(f, cx, cy, r) { return `` }) .replace(//g, function(f, cx, cy, rx) { return `` }) .replace(//g, function(f, cx, cy, rx, ry) { return `` }) .replace(//g, function(f, width, height, x, y, rx) { return `` }) .replace(//g, function(f, x, y, rx, width, height) { return `` }) .replace(//g, function(f, x, y, width, height, rx) { return `` }) .replace(//g, function(f, x, y, width, height) { return `` }) .replace(//g, function(f, points) { const path = points.split(' ').reduce( (accumulator, currentValue, currentIndex) => `${accumulator}${currentIndex % 2 === 0 ? (currentIndex === 0 ? 'M' : 'L') : ''}${currentValue} `, '' ) return `` }) .replace(/ `${m}`) .replace(/([0-9]+)+\.99[4-9]/g, (f, m) => `${parseInt(m) + 1}`) .replace(/\.99[4-9]/g, (f, m) => `1`) .replace(/-\.00[1-6]/g, (f, m) => `0`) .replace(/\.00[1-6]/g, (f, m) => `0`) .replace(/m0 0/g, (f, m) => ``) return `[\n\s\t]+)/g, function() { // return `M` // }) .replace(/[\n\t\s]*[\n\t\s]*<\/svg>/)) { console.log(`Fix ${file}!`); } if (svgFile.toString() !== svgFileContent) { writeFileSync(file, svgFileContent) } }) })