| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- #!/usr/bin/env node
- /**
- * D33: 修复 C 批遗留 bug —— 脚本/样式中的 'BrandColors.x' 字符串字面量
- * (字符串不是变量引用,作为颜色值无效:canvas setFillStyle、渐变拼接、色板数组等)
- * 统一改为真正的变量引用 BrandColors.x(键均已在 utils/brand-colors.js 存在)
- */
- const fs = require('fs');
- const path = require('path');
- const ROOT = path.resolve(__dirname, '..');
- const EXCLUDE_DIRS = new Set(['node_modules', 'dist', 'unpackage', '.git', 'scripts', '.tmp_equiv']);
- function walk(dir, out = []) {
- for (const name of fs.readdirSync(dir)) {
- if (EXCLUDE_DIRS.has(name)) continue;
- const p = path.join(dir, name);
- const st = fs.statSync(p);
- if (st.isDirectory()) walk(p, out);
- else if (name.endsWith('.vue')) out.push(p);
- }
- return out;
- }
- let files = 0;
- let count = 0;
- for (const abs of walk(ROOT)) {
- const src = fs.readFileSync(abs, 'utf8');
- if (!/['"]BrandColors\.\w+['"]/.test(src)) continue;
- let n = 0;
- let out = src.replace(/['"]BrandColors\.(\w+)['"]/g, (m, key) => {
- n++;
- return 'BrandColors.' + key;
- });
- if (!/import\s*\{[^}]*\bBrandColors\b[^}]*\}\s*from\s*['"][^'"]*brand-colors/.test(out) &&
- !/import\s*\{[^}]*\bBrandColors\b[^}]*\}\s*from\s*['"][^'"]*brand-colors/.test(src)) {
- const rel = path.relative(path.dirname(abs), path.join(ROOT, 'utils/brand-colors.js')).split(path.sep).join('/');
- const existing = out.match(/import\s*\{([^}]*)\}\s*from\s*(['"][^'"]*brand-colors(?:\.js)?['"])/);
- if (existing) {
- out = out.replace(existing[0], 'import {' + existing[1].replace(/\s*$/, '') + (existing[1].trim().endsWith(',') ? ' ' : ', ') + 'BrandColors } from ' + existing[2]);
- } else {
- const t = out.match(/<script[^>]*>\r?\n/);
- if (t) out = out.replace(t[0], t[0] + "import { BrandColors } from '" + rel + "'" + (t[0].includes('\r\n') ? '\r\n' : '\n'));
- }
- }
- fs.writeFileSync(abs, out, 'utf8');
- files++;
- count += n;
- console.log(String(n).padStart(3) + ' ' + path.relative(ROOT, abs).split(path.sep).join('/'));
- }
- console.log('文件:', files, '修复字符串引用:', count);
|