icon.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import path from 'path'
  2. import fs from 'fs-extra'
  3. import inquirer from 'inquirer'
  4. import chalk from 'chalk'
  5. import pkg from '../package.json'
  6. interface Icon {
  7. name: string
  8. prefix: string
  9. icons: string[]
  10. }
  11. async function generateIcon() {
  12. const dir = path.resolve(process.cwd(), 'node_modules/@iconify/json')
  13. const raw = await fs.readJSON(path.join(dir, 'collections.json'))
  14. const collections = Object.entries(raw).map(([id, v]) => ({
  15. ...(v as any),
  16. id
  17. }))
  18. const choices = collections.map((item) => ({ key: item.id, value: item.id, name: item.name }))
  19. inquirer
  20. .prompt([
  21. // {
  22. // type: 'list',
  23. // name: 'useType',
  24. // choices: [
  25. // { key: 'local', value: 'local', name: 'Local' },
  26. // { key: 'onLine', value: 'onLine', name: 'OnLine' },
  27. // ],
  28. // message: 'How to use icons?',
  29. // },
  30. {
  31. type: 'list',
  32. name: 'iconSet',
  33. choices: choices,
  34. message: 'Select the icon set that needs to be generated?'
  35. }
  36. // {
  37. // type: 'input',
  38. // name: 'output',
  39. // message: 'Select the icon set that needs to be generated?',
  40. // default: 'src/components/Icon/data',
  41. // },
  42. ])
  43. // ↓命令行问答的答案
  44. .then(async (answers) => {
  45. const { iconSet } = answers
  46. const outputDir = path.resolve(process.cwd(), 'src/components/IconPicker/src/data')
  47. fs.ensureDir(outputDir)
  48. const genCollections = collections.filter((item) => [iconSet].includes(item.id))
  49. const prefixSet: string[] = []
  50. for (const info of genCollections) {
  51. const data = await fs.readJSON(path.join(dir, 'json', `${info.id}.json`))
  52. if (data) {
  53. const { prefix } = data
  54. const icons = Object.keys(data.icons).map((item) => `${prefix}:${item}`)
  55. await fs.writeFileSync(
  56. path.join('src/components/IconPicker/src/data', `icons.${prefix}.ts`),
  57. `export default ${JSON.stringify({ name: info.name, prefix, icons })}`
  58. )
  59. // ↓分类处理完成,push类型名称
  60. prefixSet.push(prefix)
  61. }
  62. }
  63. // 将vite的缓存清空
  64. // fs.emptyDir(path.join(process.cwd(), 'node_modules/.vite'))
  65. console.log(
  66. `✨ ${chalk.cyan(`[${pkg.name}]`)}` + ' - Icon generated successfully:' + `[${prefixSet}]`
  67. )
  68. })
  69. }
  70. generateIcon()