1
+ import * as path from 'path' ;
2
+ import { createHash } from 'crypto' ;
3
+ import { promises as fs } from 'fs' ;
1
4
import traverse , { NodePath } from '@babel/traverse' ;
2
5
import * as t from '@babel/types' ;
3
6
import { EditorAttributes } from '@onlook/models/constants' ;
4
7
import type { DynamicType , TemplateNode } from '@onlook/models/element' ;
5
8
import { generateCode } from '../code/diff/helpers' ;
6
- import { formatContent , readFile } from '../code/files' ;
9
+ import { checkIfCacheDirectoryExists , formatContent , readFile , writeFile } from '../code/files' ;
7
10
import { parseJsxFile } from '../code/helpers' ;
8
11
import {
9
12
GENERATE_CODE_OPTIONS ,
@@ -14,6 +17,7 @@ import {
14
17
isNodeElementArray ,
15
18
isReactFragment ,
16
19
} from './helpers' ;
20
+ import type { HashesJson } from '@onlook/models' ;
17
21
18
22
export async function getFileWithIds ( filePath : string ) : Promise < string | null > {
19
23
const content = await readFile ( filePath ) ;
@@ -170,3 +174,60 @@ function createMapping(ast: t.File, filename: string): Record<string, TemplateNo
170
174
} ) ;
171
175
return mapping ;
172
176
}
177
+
178
+ export async function cacheFile ( filePath : string , projectDir : string ) : Promise < void > {
179
+ await checkIfCacheDirectoryExists ( projectDir ) ;
180
+
181
+ const content = await readFile ( filePath ) ;
182
+
183
+ if ( ! content || content . trim ( ) === '' ) {
184
+ console . error ( `Failed to get content for file: ${ filePath } ` ) ;
185
+ return ;
186
+ }
187
+
188
+ const cacheDir = path . join ( projectDir , '.onlook' , 'cache' ) ;
189
+
190
+ const fileName = path . basename ( filePath ) ;
191
+
192
+ const cacheFilePath = path . join ( cacheDir , fileName ) ;
193
+
194
+ await writeFile ( cacheFilePath , content ) ;
195
+ }
196
+
197
+ export async function generateAndStoreHash ( filePath : string , projectDir : string ) {
198
+ await checkIfCacheDirectoryExists ( projectDir ) ;
199
+
200
+ const cacheDir = path . join ( projectDir , '.onlook' , 'cache' ) ;
201
+ const hashesFilePath = path . join ( cacheDir , 'hashes.json' ) ;
202
+
203
+ const content = await readFile ( filePath ) ;
204
+
205
+ if ( ! content || content . trim ( ) === '' ) {
206
+ console . error ( `Failed to get content for file: ${ filePath } ` ) ;
207
+ return ;
208
+ }
209
+
210
+ const hash = createHash ( 'sha256' ) . update ( content ) . digest ( 'hex' ) ;
211
+
212
+ let hashesJson : HashesJson = { } ;
213
+
214
+ try {
215
+ const existing = await readFile ( hashesFilePath ) ;
216
+ if ( existing ) {
217
+ hashesJson = JSON . parse ( existing ) ;
218
+ }
219
+ } catch ( e ) {
220
+ console . log ( 'No existing hashes.json found, creating new one.' ) ;
221
+ }
222
+
223
+ const fileName = path . basename ( filePath ) ;
224
+
225
+ const cacheFilePath = path . join ( cacheDir , fileName ) ;
226
+
227
+ hashesJson [ filePath ] = {
228
+ hash,
229
+ cache_path : cacheFilePath ,
230
+ } ;
231
+
232
+ await fs . writeFile ( hashesFilePath , JSON . stringify ( hashesJson , null , 2 ) , 'utf8' ) ;
233
+ }
0 commit comments