|
| 1 | +import { IconIndexer } from '../src/mcp/services/iconIndexer.js'; |
| 2 | +import { IconIndexData } from '../src/mcp/types/icon.js'; |
| 3 | +import fs from 'fs'; |
| 4 | +import path from 'path'; |
| 5 | +import { fileURLToPath } from 'url'; |
| 6 | + |
| 7 | +const __filename = fileURLToPath(import.meta.url); |
| 8 | +const __dirname = path.dirname(__filename); |
| 9 | + |
| 10 | +/** |
| 11 | + * 从 OpenTiny Vue Icon 库中提取所有图标名称 |
| 12 | + */ |
| 13 | +function extractIconNames(): string[] { |
| 14 | + const iconLibPath = path.resolve('node_modules/@opentiny/vue-icon/lib'); |
| 15 | + |
| 16 | + if (!fs.existsSync(iconLibPath)) { |
| 17 | + console.error(`Error: Icon library not found at ${iconLibPath}`); |
| 18 | + console.error('Please ensure @opentiny/vue-icon is installed.'); |
| 19 | + process.exit(1); |
| 20 | + } |
| 21 | + |
| 22 | + const files = fs.readdirSync(iconLibPath); |
| 23 | + // 过滤出 .js 文件并去掉扩展名 |
| 24 | + const iconNames = files |
| 25 | + .filter(f => f.endsWith('.js')) |
| 26 | + .map(f => f.replace(/\.js$/, '')); |
| 27 | + |
| 28 | + return iconNames; |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * 主函数:构建图标索引 |
| 33 | + */ |
| 34 | +async function main() { |
| 35 | + console.log('🔍 Building icon index for OpenTiny Vue Icons...\n'); |
| 36 | + |
| 37 | + // 1. 提取图标名称 |
| 38 | + console.log('📦 Extracting icon names from library...'); |
| 39 | + const iconNames = extractIconNames(); |
| 40 | + console.log(` Found ${iconNames.length} icons\n`); |
| 41 | + |
| 42 | + // 2. 构建索引 |
| 43 | + console.log('🔨 Building icon index...'); |
| 44 | + const indexer = new IconIndexer(); |
| 45 | + let iconIndex = indexer.buildIndex(iconNames); |
| 46 | + |
| 47 | + // 3. 填充变体信息 |
| 48 | + console.log('🔗 Filling variant information...'); |
| 49 | + iconIndex = indexer.fillVariants(iconIndex); |
| 50 | + |
| 51 | + // 4. 构建最终数据 |
| 52 | + const indexData: IconIndexData = { |
| 53 | + version: '1.0.0', |
| 54 | + lastUpdated: new Date().toISOString(), |
| 55 | + total: iconIndex.length, |
| 56 | + icons: iconIndex, |
| 57 | + }; |
| 58 | + |
| 59 | + // 5. 写入文件 |
| 60 | + const outputPath = path.resolve(__dirname, '../src/mcp/data/iconIndex.json'); |
| 61 | + fs.writeFileSync(outputPath, JSON.stringify(indexData, null, 2), 'utf-8'); |
| 62 | + |
| 63 | + console.log('\n✅ Index built successfully!'); |
| 64 | + console.log(`📊 Total icons: ${iconIndex.length}`); |
| 65 | + console.log(`📁 Output: ${outputPath}`); |
| 66 | + |
| 67 | + // 6. 显示统计信息 |
| 68 | + const categoryStats = new Map<string, number>(); |
| 69 | + for (const icon of iconIndex) { |
| 70 | + categoryStats.set(icon.category, (categoryStats.get(icon.category) || 0) + 1); |
| 71 | + } |
| 72 | + |
| 73 | + console.log('\n📈 Category distribution:'); |
| 74 | + for (const [category, count] of Array.from(categoryStats.entries()).sort((a, b) => b[1] - a[1])) { |
| 75 | + console.log(` ${category}: ${count}`); |
| 76 | + } |
| 77 | + |
| 78 | + // 7. 显示一些示例 |
| 79 | + console.log('\n📝 Sample icons:'); |
| 80 | + iconIndex.slice(0, 5).forEach(icon => { |
| 81 | + console.log(` - ${icon.name} (${icon.componentName})`); |
| 82 | + console.log(` Keywords: ${icon.keywords.en.slice(0, 3).join(', ')}`); |
| 83 | + console.log(` 中文: ${icon.keywords.zh.slice(0, 2).join(', ') || '无'}`); |
| 84 | + console.log(` 拼音: ${icon.keywords.pinyin.slice(0, 2).join(', ') || '无'}`); |
| 85 | + console.log(` 首字母: ${icon.keywords.pinyinAbbr.join(', ') || '无'}`); |
| 86 | + }); |
| 87 | +} |
| 88 | + |
| 89 | +main().catch(error => { |
| 90 | + console.error('❌ Error building index:', error); |
| 91 | + process.exit(1); |
| 92 | +}); |
0 commit comments