34 lines
1.2 KiB
JavaScript
34 lines
1.2 KiB
JavaScript
import fs from 'node:fs'
|
|
import path from 'node:path'
|
|
import { fileURLToPath } from 'node:url'
|
|
|
|
const __dirname = path.dirname(fileURLToPath(import.meta.url))
|
|
const iconJs = fs.readFileSync(
|
|
path.join(__dirname, '../../miniprogram/components/icon/icon.js'),
|
|
'utf8'
|
|
)
|
|
const m = iconJs.match(/getFontGlyph\(name\)[\s\S]*?const map = (\{[\s\S]*?)\n return map/)
|
|
if (!m) {
|
|
console.error('parse fail')
|
|
process.exit(1)
|
|
}
|
|
const body = m[1]
|
|
const lines = body.split('\n').filter((l) => l.trim().startsWith("'"))
|
|
const entries = lines
|
|
.map((l) => {
|
|
const mm = l.match(/'([^']+)':\s*'((?:\\u[0-9a-fA-F]+|[\s\S])*)'/)
|
|
if (!mm) return null
|
|
const key = mm[1]
|
|
let ch = mm[2]
|
|
if (ch.startsWith('\\u')) {
|
|
ch = String.fromCharCode(parseInt(ch.slice(2), 16))
|
|
}
|
|
return [key, ch]
|
|
})
|
|
.filter(Boolean)
|
|
|
|
const out = `/** auto-extracted from miniprogram/components/icon/icon.js */
|
|
export const FONT_GLYPH: Record<string, string> = {\n${entries.map(([k, v]) => ` ${JSON.stringify(k)}: ${JSON.stringify(v)},`).join('\n')}\n}\n`
|
|
fs.writeFileSync(path.join(__dirname, '../src/components/Icon/fontGlyphMap.ts'), out, 'utf8')
|
|
console.log('wrote', entries.length, 'keys')
|