index.ts 2.2 KB

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