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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
const fs = require('fs'); const path = require('path');
const TYPE_MAP = { 'Image': 'eui.Image', 'Label': 'eui.Label', 'Button': 'eui.Button', 'Group': 'eui.Group', 'Scroller': 'eui.Scroller', 'List': 'eui.List', 'DataGroup': 'eui.DataGroup', 'ViewStack': 'eui.ViewStack', 'TabBar': 'eui.TabBar', 'TextInput': 'eui.TextInput', 'EditableText': 'eui.EditableText', 'CheckBox': 'eui.CheckBox', 'RadioButton': 'eui.RadioButton', 'ToggleSwitch': 'eui.ToggleSwitch', 'HSlider': 'eui.HSlider', 'VSlider': 'eui.VSlider', 'HScrollBar': 'eui.HScrollBar', 'VScrollBar': 'eui.VScrollBar', 'ProgressBar': 'eui.ProgressBar', 'BitmapLabel': 'eui.BitmapLabel', 'Component': 'eui.Component', 'Panel': 'eui.Panel', 'Rect': 'eui.Rect', };
function inferType(fullTag) { const parts = fullTag.split(':'); const tagName = parts.length > 1 ? parts[1] : parts[0]; const ns = parts.length > 1 ? parts[0] : '';
if (TYPE_MAP[tagName]) return TYPE_MAP[tagName]; if (ns === 'ns1') return tagName; if (ns && ns !== 'e') return 'any'; return 'any'; }
function parseExml(filePath) { const content = fs.readFileSync(filePath, 'utf-8'); const classMatch = content.match(/class\s*=\s*"([^"]+)"/); if (!classMatch) return null;
const className = classMatch[1]; const cleaned = content .replace(/<!--[\s\S]*?-->/g, '') .replace(/<e:Skin>[\s\S]*?<\/e:Skin>/g, ''); const components = []; const seenIds = {}; const idRegex = /<(\w+:)?(\w+)\s[^>]*?\bid\s*=\s*"([^"]+)"[^>]*?\/?>/g; let match;
while ((match = idRegex.exec(cleaned)) !== null) { const tagName = match[2]; const id = match[3]; if (tagName === 'Config') continue; if (seenIds[id]) continue; seenIds[id] = true; components.push({ id: id, type: inferType((match[1] || '') + match[2]) }); }
return { className, components }; }
function walkDir(dir, callback) { if (!fs.existsSync(dir)) return; const items = fs.readdirSync(dir); for (const item of items) { const fullPath = path.join(dir, item); const stat = fs.statSync(fullPath); if (stat.isDirectory()) { walkDir(fullPath, callback); } else if (item.endsWith('.exml')) { callback(fullPath); } } }
function readConfig(projectRoot) { const configPath = path.join(projectRoot, 'egretProperties.json'); let outputPath = 'libs/ExmlIdDef.d.ts'; let exmlRoots = ['resource/skins'];
try { const raw = fs.readFileSync(configPath, 'utf-8'); const config = JSON.parse(raw); const customConfig = config.customConfig || {}; if (customConfig.outputExmlId) { outputPath = customConfig.outputExmlId; } const euiConfig = config.eui || {}; if (euiConfig.exmlRoot) { exmlRoots = euiConfig.exmlRoot; } } catch (e) { }
return { outputPath, exmlRoots }; }
function generate(projectRoot) { const { outputPath, exmlRoots } = readConfig(projectRoot); const skins = {};
for (const root of exmlRoots) { const fullPath = path.join(projectRoot, root); walkDir(fullPath, (filePath) => { const def = parseExml(filePath); if (def && !def.className.includes('.')) skins[def.className] = def; }); }
const sorted = Object.keys(skins).sort();
let content = `/** * 自动生成 - 由 watchExml.js 监听 EXML 变化时更新 * 生成时间: ${new Date().toISOString()} */
`; for (const className of sorted) { const def = skins[className]; content += `interface ${className} {\n`; for (const comp of def.components) { content += ` ${comp.id}: ${comp.type};\n`; } content += `}\n\n`; }
const outPath = path.join(projectRoot, outputPath); const outDir = path.dirname(outPath); if (!fs.existsSync(outDir)) fs.mkdirSync(outDir, { recursive: true }); fs.writeFileSync(outPath, content, 'utf-8');
const time = new Date().toLocaleTimeString(); console.log(`[${time}] ✅ ${outputPath} (${sorted.length} 个皮肤)`); }
const projectRoot = path.resolve(__dirname, '..'); generate(projectRoot);
const { exmlRoots } = readConfig(projectRoot); for (const root of exmlRoots) { const watchDir = path.join(projectRoot, root); if (!fs.existsSync(watchDir)) continue;
console.log(`[watchExml] 监听: ${root}`); fs.watch(watchDir, { recursive: true }, (eventType, filename) => { if (filename && filename.endsWith('.exml')) { console.log(`[watchExml] 检测到变化: ${filename}`); generate(projectRoot); } }); }
|