-
Notifications
You must be signed in to change notification settings - Fork 45
/
generate-custom-svg.js
41 lines (33 loc) · 1.5 KB
/
generate-custom-svg.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/* 一个自动生成 svg 的工具函数 */
const fs = require('fs');
const path = require('path');
// 要读取的目录路径
const iconsDir = path.join(__dirname, 'public', 'icons');
// 递归读取指定目录下的所有 SVG 文件
function readSVGFiles(dir, prefix = '') {
const files = fs.readdirSync(dir);
const result = {};
files.forEach((file) => {
const filePath = path.join(dir, file);
const stat = fs.statSync(filePath);
if (stat.isDirectory()) {
// 如果是目录,则递归读取
const subdir = path.join(dir, file);
const subPrefix = prefix ? `${prefix}:${file}` : file;
Object.assign(result, readSVGFiles(subdir, subPrefix));
} else if (path.extname(file).toLowerCase() === '.svg') {
const iconName = prefix ? `app:${path.basename(file, '.svg')}` : path.basename(file, '.svg');
result[iconName] = path.relative(iconsDir, filePath).replace(/\\/g, '/');
}
});
return result;
}
const newData = readSVGFiles(iconsDir);
// 生成 TypeScript 模块的内容
const comment = `/* eslint-disable */\n// This file is auto-generated by generate-custom-svg.js\n`;
const moduleContent = `export const myIcons: { [key: string]: string } = ${JSON.stringify(newData, null, 2).replace(/"/g, "'")};\n`;
const content = comment + moduleContent;
// 将内容写入文件
const outputFilePath = path.join(__dirname, 'src', 'utils', 'custom-svg.ts');
fs.writeFileSync(outputFilePath, content);
console.log(`Icons module has been generated at: ${outputFilePath}`);