yanjinhui
7 天以前 b9c76ce85e533250cd36de670146530f970859e7
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
63
64
65
66
67
68
69
70
71
72
73
import path from 'path'
import fs from 'fs-extra'
import inquirer from 'inquirer'
import chalk from 'chalk'
import pkg from '../package.json'
import { ICON_PREFIX } from '../src/constants'
 
interface Icon {
  name: string
  prefix: string
  icons: string[]
}
 
async function generateIcon() {
  const dir = path.resolve(process.cwd(), 'node_modules/@iconify/json')
 
  const raw = await fs.readJSON(path.join(dir, 'collections.json'))
 
  const collections = Object.entries(raw).map(([id, v]) => ({
    ...(v as any),
    id
  }))
 
  const choices = collections.map((item) => ({ key: item.id, value: item.id, name: item.name }))
 
  inquirer
    .prompt([
      // {
      //   type: 'list',
      //   name: 'useType',
      //   choices: [
      //     { key: 'local', value: 'local', name: 'Local' },
      //     { key: 'onLine', value: 'onLine', name: 'OnLine' }
      //   ],
      //   message: 'How to use icons?'
      // },
      {
        type: 'list',
        name: 'iconSet',
        choices: choices,
        message: 'Select the icon set that needs to be generated?'
      }
    ])
    // ↓命令行问答的答案
    .then(async (answers) => {
      const { iconSet } = answers
      // const isOnLine = useType === 'onLine'
      const outputDir = path.resolve(process.cwd(), 'src/components/IconPicker/src/data')
      fs.ensureDir(outputDir)
      const genCollections = collections.filter((item) => [iconSet].includes(item.id))
      const prefixSet: string[] = []
      for (const info of genCollections) {
        const data = await fs.readJSON(path.join(dir, 'json', `${info.id}.json`))
        if (data) {
          const { prefix } = data
          const prefixName = `${ICON_PREFIX}${prefix}`
          const icons = Object.keys(data.icons).map((item) => `${prefixName}:${item}`)
 
          await fs.writeFileSync(
            path.join('src/components/IconPicker/src/data', `icons.${prefix}.ts`),
            `export default ${JSON.stringify({ name: info.name, prefix: prefixName, icons })}`
          )
          // ↓分类处理完成,push类型名称
          prefixSet.push(prefix)
        }
      }
      console.log(
        `✨ ${chalk.cyan(`[${pkg.name}]`)}` + ' - Icon generated successfully:' + `[${prefixSet}]`
      )
    })
}
 
generateIcon()