generateIconJson.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. 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: 'checkbox',
  18. name: 'iconSet',
  19. choices: choices,
  20. message: 'Select the icon set that needs to be generated?',
  21. default: true,
  22. },
  23. {
  24. type: 'input',
  25. name: 'output',
  26. message: 'Select the icon set that needs to be generated?',
  27. default: 'src/components/Icon/json',
  28. },
  29. ])
  30. .then(async (answers) => {
  31. const { iconSet, output } = answers;
  32. const outputDir = path.resolve(process.cwd(), output);
  33. fs.ensureDir(outputDir);
  34. const genCollections = collections.filter((item) => iconSet.includes(item.id));
  35. const prefixSet: string[] = [];
  36. for (const info of genCollections) {
  37. const data = await fs.readJSON(path.join(dir, 'json', `${info.id}.json`));
  38. if (data) {
  39. const { prefix } = data;
  40. const icons = Object.keys(data.icons).map((item) => `${prefix}:${item}`);
  41. await fs.writeJSON(path.join(output, `${prefix}-info.json`), icons);
  42. prefixSet.push(prefix);
  43. }
  44. }
  45. console.log(
  46. `✨ ${chalk.cyan(`[${pkg.name}]`)}` + ' - Icon generated successfully:' + `[${prefixSet}]`
  47. );
  48. });
  49. }
  50. generateIcon();