index.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import path from '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: 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(
  50. (item) => `${isLocal ? prefix + ':' : ''}${item}`,
  51. );
  52. await fs.writeFileSync(
  53. path.join(output, `icons.data.ts`),
  54. `export default ${isLocal ? JSON.stringify(icons) : JSON.stringify({ prefix, icons })}`,
  55. );
  56. prefixSet.push(prefix);
  57. }
  58. }
  59. await fs.emptyDir(path.join(process.cwd(), 'node_modules/.vite'));
  60. console.log(
  61. `✨ ${colors.cyan(`[${pkg.name}]`)}` + ' - Icon generated successfully:' + `[${prefixSet}]`,
  62. );
  63. });
  64. }
  65. generateIcon();