File size: 3,762 Bytes
24a2ddf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import re

with open('D:/workAI/OpenCode/WorkSpace/backend-service/server.js', 'r', encoding='utf-8') as f:
    content = f.read()

# 1. Remove dead _rawToFormatted block (lines 1171-1188)
# Find and remove the entire block from comment to closing brace
pattern = r"    // .*?Build format map from chartData.*?\n    var _rawToFormatted = \{\};.*?\n    \}\n\n    // Build series elements"
replacement = "    // Build series elements"
new_content = re.sub(pattern, replacement, content, flags=re.DOTALL)
if new_content != content:
    content = new_content
    print("CLEANED: Removed dead _rawToFormatted block")
else:
    # Try a simpler pattern
    idx = content.find("var _rawToFormatted = {};")
    if idx >= 0:
        # Go back to find the comment before it
        comment_start = content.rfind("    // ", 0, idx)
        # Find the end - next "    // Build series elements"
        end_marker = "    // Build series elements"
        end_idx = content.find(end_marker, idx)
        if end_idx > 0 and comment_start > 0:
            content = content[:comment_start] + "\n" + content[end_idx:]
            print("CLEANED: Removed dead _rawToFormatted block (manual)")
        else:
            print(f"WARNING: Could not find boundaries. comment_start={comment_start}, end_idx={end_idx}")
    else:
        print("INFO: _rawToFormatted not found")

# 2. Remove all [AXIS-DEBUG] console.log from generated HTML
# These are in the client-side code embedded in buildChatGPTChartHtml
axis_debug_count = len(re.findall(r"console\.log\('\[AXIS-DEBUG\]", content))
if axis_debug_count > 0:
    # Remove console.log('[AXIS-DEBUG]...'); lines
    content = re.sub(r"      console\.log\('\[AXIS-DEBUG\].*?\);\n", "", content)
    content = re.sub(r"        console\.log\('\[AXIS-DEBUG\].*?\);\n", "", content)
    print(f"CLEANED: Removed {axis_debug_count} [AXIS-DEBUG] console.log statements")
else:
    print("INFO: No [AXIS-DEBUG] logs found")

# 3. Remove [WIDGET] extractChartConfig FAIL logging
idx = content.find("console.log(`[WIDGET] extractChartConfig: FAIL")
if idx >= 0:
    line_end = content.find(";", idx) + 1
    content = content[:idx] + content[line_end:]
    print("CLEANED: Removed extractChartConfig FAIL logging")
else:
    print("INFO: extractChartConfig FAIL logging not found")

# 4. Remove redundant const fs = require inside renderChatGPTChart
# Check for local fs requires (should not exist since fs is at top)
fs_local_count = len(re.findall(r"const fs = require\('fs'\)", content))
if fs_local_count > 1:
    # Keep the first (top-level), remove duplicates
    first = content.find("const fs = require('fs')")
    content = content[:first] + "__FS_MARKER__" + content[first+len("const fs = require('fs')"):]
    content = content.replace("const fs = require('fs')", "")
    content = content.replace("__FS_MARKER__", "const fs = require('fs')")
    print(f"CLEANED: Removed {fs_local_count - 1} duplicate fs requires")
else:
    print(f"INFO: fs requires count = {fs_local_count} (OK)")

# 5. Clean up the comment about Chart.js detection (remove redundant pattern)
# The new\s+C\s*\( pattern double-counts
pattern = r"\(widgetHtml\.match\(/new\\s\+C\\s\*\\\(/gi\) \|\| \[\]\)\.length \+\s*\n\s*"
if re.search(pattern, content):
    content = re.sub(pattern, "", content)
    print("CLEANED: Removed double-count new C( regex")
else:
    print("INFO: Double-count regex not found")

# 6. Remove the pieActualDataKey dead variable
content = content.replace("var pieActualDataKey = valueKey || chartSeries[0]?.dataKey || 'value';\n", "")
print("CLEANED: Removed dead pieActualDataKey (second pass)")

with open('D:/workAI/OpenCode/WorkSpace/backend-service/server.js', 'w', encoding='utf-8') as f:
    f.write(content)

print("\nDone.")