starmusic-mobile
1// 一次性脚本:生成 lx-icon 组件所需的 SVG 图标文件
2// 输出到 static/icons/ 目录。运行:node scripts/gen-icon-svgs.js
3const fs = require('fs')
4const path = require('path')
5
6// 图标 path 数据(viewBox 0 0 24 24)
7const ICONS = {
8 play: { type: 'fill', paths: ['M8 5.5v13a1 1 0 0 0 1.53.85l10.5-6.5a1 1 0 0 0 0-1.7L9.53 4.65A1 1 0 0 0 8 5.5z'] },
9 pause: { type: 'fill', paths: ['M7 4.5a1 1 0 0 0-1 1v13a1 1 0 0 0 1 1h2a1 1 0 0 0 1-1v-13a1 1 0 0 0-1-1H7z', 'M15 4.5a1 1 0 0 0-1 1v13a1 1 0 0 0 1 1h2a1 1 0 0 0 1-1v-13a1 1 0 0 0-1-1h-2z'] },
10 prev: { type: 'fill', paths: ['M6 5a1 1 0 0 0-1 1v12a1 1 0 0 0 1 1h1.5a1 1 0 0 0 1-1V6a1 1 0 0 0-1-1H6z', 'M19.5 5.3a1 1 0 0 0-1.06.05l-9 6.7a1 1 0 0 0 0 1.6l9 6.7A1 1 0 0 0 20.5 19V6a1 1 0 0 0-1-1.7z'] },
11 next: { type: 'fill', paths: ['M16.5 5a1 1 0 0 0-1 1v12a1 1 0 0 0 1 1H18a1 1 0 0 0 1-1V6a1 1 0 0 0-1-1h-1.5z', 'M4.5 5.3a1 1 0 0 0-.5.87v12.66a1 1 0 0 0 1.56.83l9-6.7a1 1 0 0 0 0-1.6l-9-6.7A1 1 0 0 0 4.5 5.3z'] },
12 'play-all': { type: 'fill', paths: ['M8 5.5v13a1 1 0 0 0 1.53.85l10.5-6.5a1 1 0 0 0 0-1.7L9.53 4.65A1 1 0 0 0 8 5.5z'] },
13 heart: { type: 'stroke', paths: ['M12 20.5s-7.5-4.9-9.7-9.2C0.9 8.5 2.6 4.8 6 4.8c2.1 0 3.6 1.2 4.3 2.6l1.7 2.6 1.7-2.6c0.7-1.4 2.2-2.6 4.3-2.6 3.4 0 5.1 3.7 3.7 6.5-2.2 4.3-9.7 9.2-9.7 9.2z'] },
14 'heart-fill': { type: 'fill', paths: ['M12 20.5s-7.5-4.9-9.7-9.2C0.9 8.5 2.6 4.8 6 4.8c2.1 0 3.6 1.2 4.3 2.6l1.7 2.6 1.7-2.6c0.7-1.4 2.2-2.6 4.3-2.6 3.4 0 5.1 3.7 3.7 6.5-2.2 4.3-9.7 9.2-9.7 9.2z'] },
15 search: { type: 'stroke', paths: ['M10.8 4a6.8 6.8 0 1 0 0 13.6 6.8 6.8 0 0 0 0-13.6z', 'M15.9 15.9L21 21'] },
16 close: { type: 'stroke', paths: ['M6 6l12 12', 'M18 6L6 18'] },
17 more: { type: 'stroke', circles: [[12, 5.5, 1.6], [12, 12, 1.6], [12, 18.5, 1.6]] },
18 music: { type: 'stroke', paths: ['M9.5 18.5V6.2a1 1 0 0 1 .77-.97l8-1.8a1 1 0 0 1 1.23.97v11.6', 'M6.5 18.5a3 3 0 1 1-6 0 3 3 0 0 1 6 0z', 'M14.5 16.5a3 3 0 1 1-6 0 3 3 0 0 1 6 0z'] },
19 check: { type: 'stroke', paths: ['M20 6L9 17l-5-5'] },
20 cross: { type: 'stroke', paths: ['M6 6l12 12', 'M18 6L6 18'] },
21 arrow: { type: 'stroke', paths: ['M9 5l7 7-7 7'] },
22 download: { type: 'stroke', paths: ['M12 4v10m0 0l-4-4m4 4l4-4', 'M4 19h16'] },
23 // 节点连线(分享):三个圆点 + 两条连线,与 PC 端 Icon.share 同形
24 share: {
25 type: 'stroke',
26 paths: ['M8.59 13.51L15.42 17.49', 'M15.41 6.51L8.59 10.49'],
27 circles: [[6, 12, 2.6], [18, 5.5, 2.6], [18, 18.5, 2.6]],
28 },
29 // 上升折线 + 箭头:用于「热搜榜」标题
30 trend: { type: 'stroke', paths: ['M3 16.5l5.5-5.5 3.5 3.5 8-8', 'M15.5 6.5H20V11'] },
31}
32
33// 需要的颜色(hex -> 文件名后缀)
34const COLORS = {
35 white: '#ffffff',
36 dark: '#303133',
37 gray: '#606266',
38 gray2: '#909399',
39 light: '#c0c4cc',
40 green: '#31c27c',
41 ok: '#67c23a',
42 fail: '#f56c6c',
43 red: '#ff4d4f',
44}
45
46// 实际使用到的 图标×颜色 组合
47const COMBOS = [
48 ['play', 'white'],
49 ['play', 'green'],
50 ['pause', 'white'],
51 ['pause', 'green'],
52 ['prev', 'dark'],
53 ['next', 'dark'],
54 ['play-all', 'white'],
55 ['heart', 'gray'],
56 ['heart', 'red'],
57 ['heart-fill', 'red'],
58 ['search', 'gray2'],
59 ['close', 'gray'],
60 ['close', 'gray2'],
61 ['more', 'gray2'],
62 ['music', 'light'],
63 ['music', 'green'],
64 ['check', 'green'],
65 ['check', 'ok'],
66 ['cross', 'fail'],
67 ['arrow', 'light'],
68 ['download', 'gray'],
69 ['download', 'green'],
70 ['share', 'gray'],
71 ['share', 'green'],
72 ['check', 'white'],
73 ['arrow', 'white'],
74]
75
76const escapeXml = (s) => s.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"')
77
78const renderSvg = (name, colorHex) => {
79 const icon = ICONS[name]
80 const isStroke = icon.type === 'stroke'
81 const paths = (icon.paths || [])
82 .map((d) => `<path d="${escapeXml(d)}" fill="${isStroke ? 'none' : colorHex}" ${isStroke ? `stroke="${colorHex}" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"` : ''}/>`)
83 .join('')
84 const circles = (icon.circles || [])
85 .map(([cx, cy, r]) => `<circle cx="${cx}" cy="${cy}" r="${r}" fill="none" stroke="${colorHex}" stroke-width="2"/>`)
86 .join('')
87 return `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24">${paths}${circles}</svg>`
88}
89
90const outDir = path.join(__dirname, '..', 'static', 'icons')
91fs.mkdirSync(outDir, { recursive: true })
92
93for (const [name, colorKey] of COMBOS) {
94 const file = path.join(outDir, `${name}-${colorKey}.svg`)
95 fs.writeFileSync(file, renderSvg(name, COLORS[colorKey]))
96 console.log('generated', path.basename(file))
97}
98
99// ============ 主题色图标 ============
100// 生成 {name}-theme-{themeId}.svg,供 lx-icon 的 color="theme" 使用。
101// 主题色从 common/theme.js 提取,避免两处维护。
102const THEME_ICONS = ['play', 'pause', 'music', 'check', 'download', 'trend']
103const themeSrc = fs.readFileSync(path.join(__dirname, '..', 'common', 'theme.js'), 'utf8')
104const THEMES = [...themeSrc.matchAll(/id:\s*'([\w-]+)',\s*name:\s*'[^']*',\s*color:\s*'(#[0-9a-fA-F]{6})'/g)].map(
105 (m) => ({ id: m[1], color: m[2] }),
106)
107if (!THEMES.length) throw new Error('未从 common/theme.js 解析到主题色')
108
109for (const theme of THEMES) {
110 for (const name of THEME_ICONS) {
111 const file = path.join(outDir, `${name}-theme-${theme.id}.svg`)
112 fs.writeFileSync(file, renderSvg(name, theme.color))
113 }
114}
115console.log(`generated ${THEMES.length * THEME_ICONS.length} theme icons`)