index.ts 2.2 KB
Newer Older
1 2 3 4
import path from 'path';
import fs from 'fs-extra';
import inquirer from 'inquirer';
import chalk from 'chalk';
V
Vben 已提交
5
import pkg from '../../../package.json';
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

async function generateIcon() {
  const dir = path.resolve(process.cwd(), 'node_modules/@iconify/json');

  const raw = await fs.readJSON(path.join(dir, 'collections.json'));

  const collections = Object.entries(raw).map(([id, v]) => ({
    ...(v as any),
    id,
  }));

  const choices = collections.map((item) => ({ key: item.id, value: item.id, name: item.name }));

  inquirer
    .prompt([
      {
V
Vben 已提交
22 23 24 25 26 27 28 29 30 31
        type: 'list',
        name: 'useType',
        choices: [
          { key: 'local', value: 'local', name: 'Local' },
          { key: 'onLine', value: 'onLine', name: 'OnLine' },
        ],
        message: 'How to use icons?',
      },
      {
        type: 'list',
32 33 34 35 36 37 38 39
        name: 'iconSet',
        choices: choices,
        message: 'Select the icon set that needs to be generated?',
      },
      {
        type: 'input',
        name: 'output',
        message: 'Select the icon set that needs to be generated?',
V
Vben 已提交
40
        default: 'src/components/Icon/data',
41 42 43
      },
    ])
    .then(async (answers) => {
V
Vben 已提交
44
      const { iconSet, output, useType } = answers;
45 46
      const outputDir = path.resolve(process.cwd(), output);
      fs.ensureDir(outputDir);
V
Vben 已提交
47
      const genCollections = collections.filter((item) => [iconSet].includes(item.id));
48 49 50 51 52
      const prefixSet: string[] = [];
      for (const info of genCollections) {
        const data = await fs.readJSON(path.join(dir, 'json', `${info.id}.json`));
        if (data) {
          const { prefix } = data;
V
Vben 已提交
53 54 55 56 57 58 59 60 61
          const isLocal = useType === 'local';
          const icons = Object.keys(data.icons).map(
            (item) => `${isLocal ? prefix + ':' : ''}${item}`
          );

          await fs.writeFileSync(
            path.join(output, `icons.data.ts`),
            `export default ${isLocal ? JSON.stringify(icons) : JSON.stringify({ prefix, icons })}`
          );
62 63 64
          prefixSet.push(prefix);
        }
      }
V
Vben 已提交
65
      fs.emptyDir(path.join(process.cwd(), 'node_modules/.vite'));
66 67 68 69 70 71 72
      console.log(
        `✨ ${chalk.cyan(`[${pkg.name}]`)}` + ' - Icon generated successfully:' + `[${prefixSet}]`
      );
    });
}

generateIcon();