d31-fix-template-refs.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #!/usr/bin/env node
  2. /**
  3. * D31 修正:撤销模板区(<script> 之前)被误替换的 BrandColors.* —— 模板无法访问模块级变量,
  4. * 统一还原为原 hex 字面量;script/style 区的收编保留。
  5. */
  6. const fs = require('fs');
  7. const { execSync } = require('child_process');
  8. const files = execSync('git -C .. diff --name-only -- cfc-frontend', { encoding: 'utf8' })
  9. .split('\n').map(s => s.trim()).filter(s => s.endsWith('.vue')).map(s => s.replace('cfc-frontend/', ''));
  10. const KEY_TO_HEX = {
  11. success: '#10B981', wealth: '#F59E0B', mind: '#FF6B9D', body: '#FF8C42',
  12. primary: '#F97316', wisdom: '#6366F1', white: '#FFFFFF', infoBlue: '#3B82F6',
  13. danger: '#EF4444', appTextSubtle: '#94A3B8', violet: '#8B5CF6', orangeDeep: '#EA580C',
  14. infoSky: '#0EA5E9', pink: '#EC4899', textSlate: '#6B7280', matGreen: '#4CAF50',
  15. mindAccent: '#FF6B35', brownWarm: '#8D6E63',
  16. };
  17. let touched = 0;
  18. let reverted = 0;
  19. for (const f of files) {
  20. const s = fs.readFileSync(f, 'utf8');
  21. const scriptIdx = s.search(/<script/);
  22. if (scriptIdx === -1) continue;
  23. let tpl = s.slice(0, scriptIdx);
  24. const rest = s.slice(scriptIdx);
  25. if (!/BrandColors\.\w+/.test(tpl)) continue;
  26. tpl = tpl.replace(/BrandColors\.(\w+)/g, (m, key) => {
  27. if (!KEY_TO_HEX[key]) {
  28. console.warn('UNKNOWN KEY ' + key + ' in ' + f);
  29. return m;
  30. }
  31. reverted++;
  32. return KEY_TO_HEX[key];
  33. });
  34. fs.writeFileSync(f, tpl + rest, 'utf8');
  35. touched++;
  36. }
  37. console.log('修正文件:', touched, '还原引用:', reverted);