Blame view

build/generate/icon/index.ts 2.22 KB
1
2
3
4
import path from 'path';
import fs from 'fs-extra';
import inquirer from 'inquirer';
import chalk from 'chalk';
Vben authored
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([
      {
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?',
40
        default: 'src/components/Icon/data',
41
42
43
      },
    ])
    .then(async (answers) => {
44
      const { iconSet, output, useType } = answers;
45
46
      const outputDir = path.resolve(process.cwd(), output);
      fs.ensureDir(outputDir);
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;
53
54
          const isLocal = useType === 'local';
          const icons = Object.keys(data.icons).map(
vben authored
55
            (item) => `${isLocal ? prefix + ':' : ''}${item}`,
56
57
58
59
          );

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

generateIcon();