validate-icons.mjs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import { globSync } from 'glob'
  2. import fs from 'fs'
  3. import { basename } from 'path'
  4. import { 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')),
  8. filledIconsNames = globSync(join(ICONS_SRC_DIR, 'filled/*.svg')).map(i => basename(i, '.svg')),
  9. argvs = getArgvs();
  10. let unicodes = []
  11. const duplicateExists = (arr) => {
  12. return new Set(arr).size !== arr.length
  13. }
  14. types.forEach(type => {
  15. const icons = globSync(join(ICONS_SRC_DIR, type, '*.svg'))
  16. icons.forEach((icon) => {
  17. const iconContent = fs.readFileSync(icon, 'utf-8')
  18. if (!iconContent.includes(iconTemplate(type))) {
  19. console.log(`Icon ${icon} is not properly formatted`)
  20. error = true
  21. }
  22. if (!iconContent.includes('<!--') || !iconContent.includes('-->')) {
  23. console.log(`Icon ${icon} has no metadata`)
  24. error = true
  25. }
  26. try {
  27. const { data } = parseMatter(icon)
  28. if (data.unicode) {
  29. if (unicodes.indexOf(data.unicode) !== -1) {
  30. console.log(`Icon ${icon} has duplicate unicode "${data.unicode}"`)
  31. error = true
  32. }
  33. if (data.unicode.length !== 4) {
  34. console.log(`Icon ${icon} has invalid unicode "${data.unicode}"`)
  35. error = true
  36. }
  37. // check duplicates in tags
  38. if (duplicateExists(data.tags || [])) {
  39. console.log(`Icon ${icon} has duplicate tags`)
  40. error = true
  41. }
  42. unicodes.push(data.unicode)
  43. } else if (argvs.hard) {
  44. console.log(`Icon ${icon} has no unicode`)
  45. error = true
  46. }
  47. if (argvs.hard && !data.version) {
  48. console.log(`Icon ${icon} has no version`)
  49. error = true
  50. }
  51. } catch (e) {
  52. console.log(`Icon ${icon} has invalid metadata`)
  53. error = true
  54. }
  55. })
  56. filledIconsNames.forEach((icon) => {
  57. if (outlineIconsNames.indexOf(icon) === -1) {
  58. console.log(`Icon ${icon} exists in filled version but doesn't exists in outline`)
  59. error = true
  60. }
  61. })
  62. })
  63. if (error) {
  64. process.exit(1)
  65. } else {
  66. console.log('All icons are valid!')
  67. process.exit(0)
  68. }