code-cache.ts
4.09 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import fs from 'node:fs';
import { WorkspaceStructure } from '../types/workspace-types';
import { logger } from './logger';
/**
* Code generation cache structure
* Tracks which components have been generated and whether App.tsx has been injected
*/
export interface CodeCache {
generatedComponents: string[]; // Array of node IDs that have been generated
appInjected: boolean;
}
/**
* Get the path to the cache file
*/
function getCachePath(workspace: WorkspaceStructure): string {
return workspace.checkpoint;
}
/**
* Load code generation cache from file
* Returns empty cache if file doesn't exist or on error
*/
export function loadCodeCache(workspace: WorkspaceStructure): CodeCache {
const cachePath = getCachePath(workspace);
try {
if (!fs.existsSync(cachePath)) {
logger.printInfoLog('未发现代码缓存,从头开始');
return createEmptyCache();
}
const content = fs.readFileSync(cachePath, 'utf-8');
const cache = JSON.parse(content) as CodeCache;
// Validate cache structure
if (!Array.isArray(cache.generatedComponents) || typeof cache.appInjected !== 'boolean') {
logger.printWarnLog('缓存格式无效,从头开始');
return createEmptyCache();
}
logger.printInfoLog(`已加载代码缓存:${cache.generatedComponents.length} 个组件已缓存`);
return cache;
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
logger.printWarnLog(`加载代码缓存失败:${errorMessage},从头开始。`);
return createEmptyCache();
}
}
/**
* Save code generation cache to file
*/
export function saveCodeCache(workspace: WorkspaceStructure, cache: CodeCache): void {
const cachePath = getCachePath(workspace);
try {
// Ensure process directory exists
if (!fs.existsSync(workspace.process)) {
fs.mkdirSync(workspace.process, { recursive: true });
}
const content = JSON.stringify(cache, null, 2);
fs.writeFileSync(cachePath, content, 'utf-8');
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
logger.printWarnLog(`保存代码缓存失败:${errorMessage}`);
}
}
/**
* Check if a component has already been generated
* @param cache - The code cache object
* @param nodeId - The unique node ID to check
*/
export function isComponentGenerated(cache: CodeCache, nodeId: string): boolean {
return cache.generatedComponents.includes(nodeId);
}
/**
* Mark a component as generated
* @param cache - The code cache object
* @param nodeId - The unique node ID to mark as generated
*/
export function markComponentGenerated(cache: CodeCache, nodeId: string): void {
if (!isComponentGenerated(cache, nodeId)) {
cache.generatedComponents.push(nodeId);
}
}
/**
* Mark a component as generated and save cache immediately
* Combines markComponentGenerated and saveCodeCache for convenience
* @param cache - The code cache object
* @param nodeId - The unique node ID to mark as generated
* @param workspace - The workspace structure containing cache file path
*/
export function saveComponentGenerated(cache: CodeCache, nodeId: string, workspace: WorkspaceStructure): void {
markComponentGenerated(cache, nodeId);
saveCodeCache(workspace, cache);
}
/**
* Check if root component has been injected into App.tsx
*/
export function isAppInjected(cache: CodeCache): boolean {
return cache.appInjected;
}
/**
* Mark App.tsx as injected
*/
export function markAppInjected(cache: CodeCache): void {
cache.appInjected = true;
}
/**
* Mark App.tsx as injected and save cache immediately
* Combines markAppInjected and saveCodeCache for convenience
*/
export function saveAppInjected(cache: CodeCache, workspace: WorkspaceStructure): void {
markAppInjected(cache);
saveCodeCache(workspace, cache);
}
/**
* Create an empty cache
*/
function createEmptyCache(): CodeCache {
return {
generatedComponents: [],
appInjected: false,
};
}