index.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. 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. await 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. fs.emptyDir(path.join(process.cwd(), 'node_modules/.vite'));
  58. console.log(`✨ ${colors.cyan(`[${pkg.name}]`)}` + ' - Icon generated successfully:' + `[${prefixSet}]`);
  59. });
  60. }
  61. generateIcon();