index.ts 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import { join, resolve } from 'path';
  2. import inquirer from 'inquirer';
  3. import fs from 'fs-extra';
  4. import chalk from 'chalk';
  5. async function genreateIcon() {
  6. const answers = await inquirer.prompt([
  7. {
  8. type: 'input',
  9. name: 'entry',
  10. message: 'Set Iconfont.js File Entry Location!',
  11. default: 'src/assets/iconfont/iconfont.js',
  12. },
  13. {
  14. type: 'input',
  15. name: 'output',
  16. message: 'Set The Location For Storing Generated File!',
  17. default: 'src/components/Icon/data',
  18. },
  19. {
  20. type: 'outputFileName',
  21. name: 'outputFileName',
  22. message: 'Set Generate File Name!',
  23. default: 'iconfont.data.ts',
  24. },
  25. ]);
  26. const { entry, output, outputFileName } = answers;
  27. if (![entry, output, outputFileName].every(Boolean)) {
  28. global.console.log(
  29. `${chalk.yellow('Generate:Icon: ')}${chalk.red(
  30. 'There is a problem with the file path you set!'
  31. )}`
  32. );
  33. process.exit(1);
  34. }
  35. const groupReg = /<symbol[^]*?<\/symbol>/g;
  36. const getIdReg = /id=(["'])(.*?)\1/;
  37. const string = await fs.readFileSync(resolve(process.cwd(), entry), { encoding: 'utf-8' });
  38. const iconNameList = Array.from(string.matchAll(groupReg)).map(([string]) => {
  39. const [, , name] = string.match(getIdReg) || [];
  40. return name.replace('iconfont-', '');
  41. });
  42. await fs.writeFileSync(
  43. join(output, outputFileName),
  44. `export default ${JSON.stringify({ prefix: 'iconfont', icons: iconNameList }, null, 2)}`
  45. );
  46. global.console.log(
  47. `${chalk.yellow('Generate:Icon: ')}${chalk.cyan(
  48. 'ICon generated successfully, Store Address: '
  49. )}${chalk.cyan(join(resolve(process.cwd()), output, outputFileName))}`
  50. );
  51. }
  52. genreateIcon();