|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import fs from 'node:fs/promises' |
| 4 | +import path from 'node:path' |
| 5 | +import { fileURLToPath } from 'node:url' |
| 6 | +import picocolors from 'picocolors' |
| 7 | +import { load as yamlLoad } from 'js-yaml' |
| 8 | + |
| 9 | +const __filename = fileURLToPath(import.meta.url) |
| 10 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)) |
| 11 | + |
| 12 | +const docsIconsDir = path.join(__dirname, '../docs/content/icons/') |
| 13 | +const metadataDir = path.join(__dirname, '../metadata/') |
| 14 | +const jsonOutput = path.join(metadataDir, 'icons.json') |
| 15 | +const categoriesOutput = path.join(metadataDir, 'categories.json') |
| 16 | + |
| 17 | +async function main(file, iconsMetadata, categories) { |
| 18 | + const filePath = path.join(docsIconsDir, file) |
| 19 | + const content = await fs.readFile(filePath, 'utf8') |
| 20 | + const frontmatterMatch = content.match(/^---\n([\s\S]*?)\n---/) |
| 21 | + |
| 22 | + if (frontmatterMatch && frontmatterMatch[1]) { |
| 23 | + try { |
| 24 | + const frontmatterJson = yamlLoad(frontmatterMatch[1]) |
| 25 | + delete frontmatterJson.title |
| 26 | + |
| 27 | + const iconName = file.replace('.md', '') |
| 28 | + iconsMetadata[iconName] = frontmatterJson |
| 29 | + |
| 30 | + if (frontmatterJson.categories && frontmatterJson.categories.length) { |
| 31 | + for (const category of frontmatterJson.categories) { |
| 32 | + if (!categories[category]) { |
| 33 | + categories[category] = [] |
| 34 | + } |
| 35 | + categories[category].push(iconName) |
| 36 | + } |
| 37 | + } |
| 38 | + } catch (yamlError) { |
| 39 | + console.error(picocolors.red(`Error parsing YAML in ${file}:`), yamlError) |
| 40 | + } |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +(async () => { |
| 45 | + try { |
| 46 | + const basename = path.basename(__filename) |
| 47 | + const timeLabel = picocolors.cyan(`[${basename}] finished`) |
| 48 | + |
| 49 | + console.log(picocolors.cyan(`[${basename}] started`)) |
| 50 | + console.time(timeLabel) |
| 51 | + |
| 52 | + const files = await fs.readdir(docsIconsDir) |
| 53 | + const iconsMetadata = {} |
| 54 | + const categories = {} |
| 55 | + |
| 56 | + await Promise.all(files.map(file => main(file, iconsMetadata, categories))) |
| 57 | + |
| 58 | + await fs.writeFile(jsonOutput, JSON.stringify(iconsMetadata, null, 2)) |
| 59 | + await fs.writeFile(categoriesOutput, JSON.stringify(categories, null, 2)) |
| 60 | + |
| 61 | + const iconsMetadataLength = Object.keys(iconsMetadata).length |
| 62 | + const categoriesLength = Object.keys(categories).length |
| 63 | + |
| 64 | + console.log( |
| 65 | + picocolors.green('\nSuccess, prepared metadata for %s icon%s and %s categor%s!'), |
| 66 | + iconsMetadataLength, |
| 67 | + iconsMetadataLength === 1 ? '' : 's', |
| 68 | + categoriesLength, |
| 69 | + categoriesLength === 1 ? 'y' : 'ies' |
| 70 | + ) |
| 71 | + console.timeEnd(timeLabel) |
| 72 | + } catch (error) { |
| 73 | + console.error(error) |
| 74 | + process.exit(1) |
| 75 | + } |
| 76 | +})() |
0 commit comments