summaryrefslogtreecommitdiffstats
path: root/build/build-pages.js
blob: 911524dc7885fbdd44692ca03149c29fa6ff4009 (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
60
61
62
#!/usr/bin/env node

'use strict'

const fs = require('fs').promises
const path = require('path')
const picocolors = require('picocolors')

const iconsDir = path.join(__dirname, '../icons/')
const pagesDir = path.join(__dirname, '../docs/content/icons/')

const VERBOSE = process.argv.includes('--verbose')

function capitalizeFirstLetter(string) {
  return (string.charAt(0).toUpperCase() + string.slice(1)).split('-').join(' ')
}

async function main(file) {
  const iconBasename = path.basename(file, path.extname(file))
  const iconTitle = capitalizeFirstLetter(iconBasename)
  const pageName = path.join(pagesDir, `${iconBasename}.md`)

  const pageTemplate = `---
title: ${iconTitle}
categories:
tags:
---
`

  try {
    await fs.access(pageName, fs.F_OK)

    if (VERBOSE) {
      console.log(`${picocolors.cyan(iconBasename)}: Page already exists; skipping`)
    }
  } catch {
    await fs.writeFile(pageName, pageTemplate)
    console.log(picocolors.green(`${iconBasename}: Page created`))
  }
}

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

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

    const files = await fs.readdir(iconsDir)

    await Promise.all(files.map(file => main(file)))

    const filesLength = files.length

    console.log(picocolors.green('\nSuccess, %s page%s prepared!'), filesLength, filesLength !== 1 ? 's' : '')
    console.timeEnd(timeLabel)
  } catch (error) {
    console.error(error)
    process.exit(1)
  }
})()