build.mjs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import fs from 'fs'
  2. import { readSvgs, HOME_DIR } from '../../.build/helpers.mjs'
  3. import { buildIcons } from '../../.build/build-icons.mjs'
  4. const svgFiles = readSvgs()
  5. const buildSprite = () => {
  6. let svgContent = ''
  7. svgFiles.forEach(function (file, i) {
  8. const svgFileContent = file.contents.replace(/<svg[^>]+>/g, '').replace(/<\/svg>/g, '').replace(/\n+/g, '').replace(/>\s+</g, '><').trim()
  9. svgContent += `<symbol id="tabler-${file.name}" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">${svgFileContent}</symbol>`
  10. })
  11. let svg = `<svg xmlns="http://www.w3.org/2000/svg"><defs>${svgContent}</defs></svg>`
  12. fs.writeFileSync('tabler-sprite.svg', svg)
  13. fs.writeFileSync('tabler-sprite-nostroke.svg', svg.replace(/stroke-width="2"\s/g, ''))
  14. }
  15. const buildNodes = () => {
  16. const iconNodes = svgFiles.reduce((acc, { name, obj }) => {
  17. acc[name] = obj.children.map(({ name, attributes }) => [name, attributes]);
  18. return acc;
  19. }, {});
  20. const iconNodesStringified = JSON.stringify(iconNodes, null, 2);
  21. fs.writeFileSync(`./tabler-nodes.json`, iconNodesStringified);
  22. }
  23. const buildCategories = () => {
  24. const icons = JSON.parse(fs.readFileSync(`${HOME_DIR}/tags.json`))
  25. if(fs.existsSync(`./categories`)) {
  26. fs.rmSync(`./categories`, { recursive: true })
  27. }
  28. fs.mkdirSync(`./categories`)
  29. Object
  30. .entries(icons)
  31. .forEach(([name, content]) => {
  32. const categories = [(content.category || 'other')
  33. .toLowerCase()
  34. .replace(/ /g, '-')]
  35. .flat()
  36. categories.forEach(category => {
  37. if (!fs.existsSync(`./categories/${category}`)) {
  38. fs.mkdirSync(`./categories/${category}`)
  39. }
  40. if (fs.existsSync(`./icons/${name}.svg`)) {
  41. fs.copyFileSync(`./icons/${name}.svg`, `./categories/${category}/${name}.svg`)
  42. }
  43. })
  44. console.log(`Move ${name} icon to ${categories.join(', ')} category`)
  45. })
  46. }
  47. const componentTemplate = ({
  48. namePascal,
  49. svg
  50. }) => `\
  51. export default ${namePascal} => \`${svg.contents}\`;`;
  52. const indexItemTemplate = ({
  53. name,
  54. namePascal
  55. }) => `export { default as ${namePascal} } from './${namePascal}';`
  56. buildSprite()
  57. buildNodes()
  58. buildCategories()
  59. buildIcons({
  60. name: 'icons',
  61. componentTemplate,
  62. indexItemTemplate,
  63. pretty: false,
  64. extension: 'ts',
  65. indexFile: 'icons/index.ts',
  66. })