icon.ts 2.2 KB

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