import fs from 'fs' import path from 'path' import { fileURLToPath } from 'url' import { parseSync } from 'svgson' const svgFiles = fs.readdirSync(path.resolve(path.dirname(fileURLToPath(import.meta.url)), './svg')) .filter((file) => path.extname(file) === '.svg') .map(svgFile => { const name = path.basename(svgFile, '.svg'), contents = fs.readFileSync(path.join(path.resolve(path.dirname(fileURLToPath(import.meta.url)), './svg'), svgFile), 'utf-8').trim().replace('', ''), obj = parseSync(contents.replace('', '')); return { name, contents, obj }; }); // Build sprites (() => { let svgContent = '' svgFiles.forEach(function(file, i) { const svgFileContent = file.contents.replace(/]+>/g, '').replace(/<\/svg>/g, '').replace(/\n+/g, '').replace(/>\s+<').trim() svgContent += `${svgFileContent}` }) let svg = `${svgContent}` fs.writeFileSync('./src/tabler-sprite.svg', svg) fs.writeFileSync('./src/tabler-sprite-nostroke.svg', svg.replace(/stroke-width="2"\s/g, '')) })(); // Build nodes (() => { const iconNodes = svgFiles.reduce((acc, { name, obj }) => { acc[name] = obj.children.map(({ name, attributes }) => [name, attributes]); return acc; }, {}); const iconNodesStringified = JSON.stringify(iconNodes, null, 2); fs.writeFileSync(`./src/tabler-nodes.json`, iconNodesStringified); })(); // Build contents (() => { const iconContents = svgFiles.reduce((acc, { name, contents }) => { var lines = contents.split('\n'); lines.splice(0,1); var trimmedContent = lines.join('\n').replace('', '').replace(/(\r\n|\n|\r)/gm, '').replace(/(\s){2,}/g, ''); acc[name] = trimmedContent; return acc; }, {}); const iconContentsStringified = JSON.stringify(iconContents, null, 2); fs.writeFileSync(`./src/tabler-contents.json`, iconContentsStringified); })();