| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- #!/usr/bin/env node
- /**
- * D36: 第八轮令牌收割(仅登记 uni.scss 已有等值令牌的残留 hex)
- * 排除 8 位带 alpha 的 hex(如 #F9731622),避免丢失透明度
- */
- const fs = require('fs');
- const path = require('path');
- function norm(h) {
- const x = h.toUpperCase();
- const body = x.slice(1);
- if (body.length === 3) return '#' + body.split('').map(c => c + c).join('');
- return x;
- }
- const scss = fs.readFileSync('uni.scss', 'utf8');
- const byHex = {};
- for (const line of scss.split(/\r?\n/)) {
- const m = line.match(/^\s*(\$[\w-]+)\s*:\s*(#[0-9a-fA-F]{3,8})\s*;/);
- if (m) {
- const k = norm(m[2]);
- (byHex[k] = byHex[k] || []).push(m[1]);
- }
- }
- const RULES_FILE = 'scripts/replace-hex-to-token.js';
- let ruleSrc = fs.readFileSync(RULES_FILE, 'utf8');
- const ruleUpper = ruleSrc.toUpperCase();
- const HEX = /#[0-9a-fA-F]{3,8}\b/g;
- const counts = new Map();
- function walk(d) {
- for (const n of fs.readdirSync(d)) {
- if (['node_modules', 'dist', 'unpackage', '.git', 'scripts', '.tmp_equiv'].includes(n)) continue;
- const p = path.join(d, n);
- const st = fs.statSync(p);
- if (st.isDirectory()) walk(p);
- else if (n.endsWith('.vue')) {
- const s = fs.readFileSync(p, 'utf8');
- const si = s.search(/<style/);
- if (si === -1) continue;
- (s.slice(si).match(HEX) || []).forEach(h => counts.set(h.toUpperCase(), (counts.get(h.toUpperCase()) || 0) + 1));
- }
- }
- }
- walk('.');
- const pairs = [];
- for (const [hex, cnt] of counts) {
- if (hex.slice(1).length === 8) continue; // 带 alpha,跳过
- const tokens = byHex[norm(hex)];
- if (!tokens) continue;
- if (ruleUpper.includes("'" + hex + "'")) continue; // 已登记
- pairs.push({ hex, token: tokens[0], cnt });
- }
- pairs.sort((a, b) => b.cnt - a.cnt);
- if (!pairs.length) {
- console.log('无新增可映射 hex');
- process.exit(0);
- }
- const lines = pairs.map(p =>
- " ['" + p.hex + "', '" + p.token + "'],\n ['" + p.hex.toLowerCase() + "', '" + p.token + "'],"
- ).join('\n');
- ruleSrc = ruleSrc.replace(
- /\n\]\.sort\(\(a, b\) => b\[0\]\.length - a\[0\]\.length\)/,
- '\n // ---- 第八轮(uni.scss 已有令牌补登记,2026-10-21)----\n' + lines + '\n].sort((a, b) => b[0].length - a[0].length)'
- );
- fs.writeFileSync(RULES_FILE, ruleSrc);
- console.log('登记规则数:', pairs.length);
- console.log(pairs.map(p => p.hex + ' x' + p.cnt + ' -> ' + p.token).join('\n'));
|