/** * AntDv4 兼容:替换 ele-admin-pro Less 中的 colorPalette JS 调用 * AntDv4 移除了 Less 的 colorPalette 函数,此脚本在 postinstall 时 * 将 common.less 中的 ~`colorPalette(...)` 替换为实际颜色值。 */ const fs = require('fs'); const path = require('path'); const rootDir = path.resolve(__dirname, '..'); const primaryColors = { 1: '#e6f4ff', 2: '#bae0ff', 3: '#91caff', 4: '#69b1ff', 5: '#4096ff', 6: '#1677ff', 7: '#0958d9', 8: '#003eb3', 9: '#002c8c', 10: '#001d66' }; const successColors = { 1: '#f6ffed', 2: '#d9f7be', 3: '#b7eb8f', 4: '#95de64', 5: '#73d13d', 6: '#52c41a', 7: '#389e0d', 8: '#237804', 9: '#135200', 10: '#092b00' }; const warningColors = { 1: '#fffbe6', 2: '#fff1b8', 3: '#ffe58f', 4: '#ffd666', 5: '#ffc53d', 6: '#faad14', 7: '#d48806', 8: '#ad6800', 9: '#874d00', 10: '#612500' }; const errorColors = { 1: '#fff2f0', 2: '#ffccc7', 3: '#ffa39e', 4: '#ff7875', 5: '#ff4d4f', 6: '#f5222d', 7: '#cf1322', 8: '#a8071a', 9: '#820014', 10: '#5c0011' }; function collectLessFiles(dir, results = []) { if (!fs.existsSync(dir)) return results; for (const entry of fs.readdirSync(dir, { withFileTypes: true })) { const fullPath = path.join(dir, entry.name); if (entry.isDirectory()) { collectLessFiles(fullPath, results); } else if (entry.isFile() && fullPath.endsWith('.less')) { results.push(fullPath); } } return results; } function replacePalette(content) { let next = content; next = next.replace(/~`colorPalette\('@\{primary-color\}',\s*(\d+)\)\s*`/g, (_m, idx) => primaryColors[idx] || '#1677ff'); next = next.replace(/~`colorPalette\('@\{success-color\}',\s*(\d+)\)\s*`/g, (_m, idx) => successColors[idx] || '#52c41a'); next = next.replace(/~`colorPalette\('@\{warning-color\}',\s*(\d+)\)\s*`/g, (_m, idx) => warningColors[idx] || '#faad14'); next = next.replace(/~`colorPalette\('@\{error-color\}',\s*(\d+)\)\s*`/g, (_m, idx) => errorColors[idx] || '#ff4d4f'); return next; } const eleAdminDir = path.join(rootDir, 'node_modules/ele-admin-pro'); const lessFiles = collectLessFiles(eleAdminDir); if (!lessFiles.length) { console.log('[antd4-compat] ele-admin-pro less files not found, skipping'); process.exit(0); } let changedCount = 0; for (const filePath of lessFiles) { const original = fs.readFileSync(filePath, 'utf8'); const content = replacePalette(original); if (content !== original) { fs.writeFileSync(filePath, content); changedCount += 1; } } if (changedCount > 0) { console.log(`[antd4-compat] Replaced colorPalette calls in ${changedCount} less file(s)`); } else { console.log('[antd4-compat] No colorPalette calls found, skipping'); }