summaryrefslogtreecommitdiffstats
path: root/build/check-icons.js
blob: 73f1a5bad5a4b2fcc0317b55850f86da65f2028e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env node

'use strict'

const fs = require('node:fs').promises
const path = require('node:path')
const process = require('node:process')
const picocolors = require('picocolors')

const fontJsonPath = path.join(__dirname, '../font/bootstrap-icons.json')
const iconsDir = path.join(__dirname, '../icons/')

;(async () => {
  try {
    const basename = path.basename(__filename)
    const timeLabel = picocolors.cyan(`[${basename}] finished`)

    console.log(picocolors.cyan(`[${basename}] started`))
    console.time(timeLabel)

    const fontJsonString = await fs.readFile(fontJsonPath, 'utf8')
    const fontJson = JSON.parse(fontJsonString)
    const svgFiles = await fs.readdir(iconsDir)

    const jsonIconList = Object.keys(fontJson)
    const svgIconList = svgFiles.map(svg => path.basename(svg, path.extname(svg)))

    const onlyInJson = jsonIconList.filter(icon => !svgIconList.includes(icon))
    const onlyInSvg = svgIconList.filter(icon => !jsonIconList.includes(icon))

    if (onlyInJson.length === 0 || onlyInSvg === 0) {
      console.log(picocolors.green('Success, found no differences!'))
      console.timeEnd(timeLabel)

      return
    }

    if (onlyInJson.length > 0) {
      console.error(picocolors.red(`Found additional icons in ${fontJsonPath}:`))

      for (const icon of onlyInJson) {
        console.log(`  - ${picocolors.red(icon)}`)
      }
    }

    if (onlyInSvg.length > 0) {
      console.error(picocolors.red('Found additional icons in SVG files:'))

      for (const icon of onlyInSvg) {
        console.log(`  - ${picocolors.red(icon)}`)
      }
    }

    process.exit(1)
  } catch (error) {
    console.error(error)
    process.exit(1)
  }
})()