validate-icons.mjs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import { globSync } from 'glob'
  2. import fs from 'fs'
  3. import { basename } from 'path'
  4. import { HOME_DIR, ICONS_SRC_DIR, iconTemplate, parseMatter, types, getArgvs } from './helpers.mjs'
  5. import { join } from 'path'
  6. let error = false
  7. const outlineIconsNames = globSync(join(ICONS_SRC_DIR, 'outline/*.svg')).map(i => basename(i, '.svg')).sort(),
  8. filledIconsNames = globSync(join(ICONS_SRC_DIR, 'filled/*.svg')).map(i => basename(i, '.svg')).sort(),
  9. argvs = getArgvs(),
  10. aliases = JSON.parse(fs.readFileSync(join(HOME_DIR, 'aliases.json'), 'utf-8'));
  11. let unicodes = []
  12. const duplicateExists = (arr) => {
  13. return new Set(arr).size !== arr.length
  14. }
  15. const getIconName = (icon) => {
  16. //return last two parts of the path
  17. return icon.split('/').slice(-2).join('/')
  18. }
  19. types.forEach(type => {
  20. const icons = globSync(join(ICONS_SRC_DIR, type, '*.svg')).sort()
  21. icons.forEach((icon) => {
  22. const iconContent = fs.readFileSync(icon, 'utf-8'),
  23. iconName = getIconName(icon)
  24. if (!iconContent.includes(iconTemplate(type))) {
  25. console.log(`⛔️ Icon \`${iconName}\` is not properly formatted`)
  26. error = true
  27. }
  28. if (!iconContent.includes('<!--') || !iconContent.includes('-->')) {
  29. console.log(`⛔️ Icon \`${iconName}\` has no metadata`)
  30. error = true
  31. }
  32. try {
  33. const { data } = parseMatter(icon)
  34. if (data.unicode) {
  35. if (unicodes.indexOf(data.unicode) !== -1) {
  36. console.log(`⛔️ Icon \`${iconName}\` has duplicate unicode \`${data.unicode}\``)
  37. error = true
  38. }
  39. if (data.unicode.length !== 4) {
  40. console.log(`⛔️ Icon \`${iconName}\` has invalid unicode \`${data.unicode}\``)
  41. error = true
  42. }
  43. // check duplicates in tags
  44. if (duplicateExists(data.tags || [])) {
  45. console.log(`⛔️ Icon \`${iconName}\` has duplicate tags`)
  46. error = true
  47. }
  48. unicodes.push(data.unicode)
  49. } else if (argvs.hard) {
  50. console.log(`⛔️ Icon \`${iconName}\` has no unicode`)
  51. error = true
  52. }
  53. if (argvs.hard && !data.version) {
  54. console.log(`⛔️ Icon \`${iconName}\` has no version`)
  55. error = true
  56. }
  57. } catch (e) {
  58. console.log(`⛔️ Icon \`${iconName}\` has invalid metadata`)
  59. error = true
  60. }
  61. })
  62. filledIconsNames.forEach((icon) => {
  63. const iconName = getIconName(icon)
  64. if (outlineIconsNames.indexOf(icon) === -1) {
  65. console.log(`⛔️ Icon \`${iconName}\` exists in filled version but doesn't exists in outline`)
  66. error = true
  67. }
  68. })
  69. })
  70. // check aliases
  71. Object.entries(aliases).forEach(([type, replacers]) => {
  72. Object.entries(replacers).forEach(([from, to]) => {
  73. if (!fs.existsSync(join(ICONS_SRC_DIR, type, `${to}.svg`))) {
  74. console.log(`⛔️ Alias \`${type}/${from}\` for \`${type}/${to}\` doesn't exists`)
  75. error = true
  76. }
  77. })
  78. })
  79. if (error) {
  80. process.exit(1)
  81. } else {
  82. console.log('✅ All icons are valid!')
  83. process.exit(0)
  84. }