d3-sleep-checkin.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #!/usr/bin/env node
  2. /**
  3. * D3: sleep-checkin.vue 睡眠主题清理 (debt 126)
  4. * - script 4 行质量条渐变 -> BrandColors 模板字符串
  5. * - style 7 色映射全局等值令牌
  6. * - 睡眠主题专属色(雪夜蓝 + 米黄族 + 极浅蓝)收编为页面级局部变量
  7. */
  8. const fs = require('fs');
  9. const F = 'pages/growth/sleep-checkin.vue';
  10. let src = fs.readFileSync(F, 'utf8');
  11. // --- 1. script 渐变 -> BrandColors ---
  12. const SCRIPT_REPL = [
  13. [
  14. "if (score >= 8) item._barBg = 'linear-gradient(90deg,#818CF8,#6366F1)'",
  15. "if (score >= 8) item._barBg = `linear-gradient(90deg, ${BrandColors.wisdomLight}, ${BrandColors.wisdom})`",
  16. ],
  17. [
  18. "else if (score >= 6) item._barBg = 'linear-gradient(90deg,#34D399,#10B981)'",
  19. "else if (score >= 6) item._barBg = `linear-gradient(90deg, ${BrandColors.successLight3}, ${BrandColors.success})`",
  20. ],
  21. [
  22. "else if (score >= 4) item._barBg = 'linear-gradient(90deg,#FBBF24,#F59E0B)'",
  23. "else if (score >= 4) item._barBg = `linear-gradient(90deg, ${BrandColors.warning}, ${BrandColors.wealth})`",
  24. ],
  25. [
  26. "else item._barBg = 'linear-gradient(90deg,#FB7185,#EF4444)'",
  27. "else item._barBg = `linear-gradient(90deg, ${BrandColors.dangerLight2}, ${BrandColors.danger})`",
  28. ],
  29. ];
  30. for (const [from, to] of SCRIPT_REPL) {
  31. if (!src.includes(from)) { console.warn('SCRIPT LINE NOT FOUND:', from.slice(0, 50)); continue; }
  32. src = src.replace(from, to);
  33. }
  34. // --- 2. import BrandColors(若未存在)---
  35. if (!src.includes('brand-colors')) {
  36. src = src.replace(/(<script>\n)/, '$1import { BrandColors } from \'../../utils/brand-colors.js\'\n');
  37. }
  38. // --- 3. style 区块 ---
  39. const styleIdx = src.indexOf('<style');
  40. const before = src.slice(0, styleIdx);
  41. let styleBody = src.slice(styleIdx);
  42. // 3a. 全局令牌映射(等值)
  43. const GLOBAL_MAP = [
  44. ['#EEF2FF', '$dim-wisdom-tint-2'], // x3
  45. ['#E0E7FF', '$accent-indigo-tint'], // x3
  46. ['#B45309', '$status-warning-strong-2'],// x1
  47. ['#4338CA', '$accent-indigo-ink'], // x2
  48. ['#F87171', '$status-danger-light'], // x1
  49. ['#059669', '$status-success-deep'], // x1
  50. ['#4F46E5', '$dim-wisdom-deep'], // x3
  51. ];
  52. // 3b. 专属色 -> 局部变量
  53. const PALETTE_MAP = [
  54. ['#EEF1F8', '$c-night-page'], // 页面底(深夜蓝灰)
  55. ['#3B4A8C', '$c-night-1'], // 雪夜蓝渐变档 1
  56. ['#2A3470', '$c-night-2'], // 雪夜蓝渐变档 2
  57. ['#1B2350', '$c-night-3'], // 雪夜蓝渐变档 3
  58. ['#FFE9A8', '$c-moon-soft'], // 月光浅黄
  59. ['#FFD166', '$c-moon'], // 月光主黄
  60. ['#FFB84D', '$c-moon-deep'], // 月光深黄
  61. ['#FF9F68', '$c-dusk-1'], // 暮色橙渐档 1
  62. ['#FF7A45', '$c-dusk-2'], // 暮色橙渐档 2
  63. ['#4A2A00', '$c-sepia-ink'], // 深棕文字(暖卡内)
  64. ['#F8FAFF', '$c-sky-ghost'], // 极浅蓝(卡底)
  65. ['#F1F5FF', '$c-sky-soft'], // 浅蓝(卡内底)
  66. ['#EDF1F7', '$c-sky-line'], // 浅蓝分隔线
  67. ];
  68. for (const [from, to] of [...GLOBAL_MAP, ...PALETTE_MAP]) {
  69. const esc = from.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
  70. styleBody = styleBody.replace(new RegExp(esc + '(?![0-9a-fA-F])', 'g'), to);
  71. }
  72. // 插入局部调色板
  73. const palette = [
  74. '// ===== 睡眠页专属色阶(雪夜蓝 + 月光米黄,单页主题专用;约定见 uni.scss §14)=====',
  75. '$c-night-page: #EEF1F8; // 页面底(深夜蓝灰)',
  76. '$c-night-1: #3B4A8C; // 雪夜蓝渐变档 1(起点)',
  77. '$c-night-2: #2A3470; // 雪夜蓝渐变档 2(中段)',
  78. '$c-night-3: #1B2350; // 雪夜蓝渐变档 3(终点)',
  79. '$c-moon-soft: #FFE9A8; // 月光浅黄',
  80. '$c-moon: #FFD166; // 月光主黄(星星/月牙)',
  81. '$c-moon-deep: #FFB84D; // 月光深黄',
  82. '$c-dusk-1: #FF9F68; // 暮色橙渐档 1',
  83. '$c-dusk-2: #FF7A45; // 暮色橙渐档 2',
  84. '$c-sepia-ink: #4A2A00; // 深棕文字(暖卡内)',
  85. '$c-sky-ghost: #F8FAFF; // 极浅蓝(卡底)',
  86. '$c-sky-soft: #F1F5FF; // 浅蓝(卡内底)',
  87. '$c-sky-line: #EDF1F7; // 浅蓝分隔线',
  88. ''
  89. ].join('\n');
  90. styleBody = styleBody.replace(/(<style[^>]*>\s*\n)/, '$1' + palette + '\n');
  91. fs.writeFileSync(F, before + styleBody);
  92. console.log('D3 done');