@@ -8,6 +8,7 @@ import { type Project } from '/shared/types/projects';
8
8
9
9
import { UiRPC } from './ui' ;
10
10
import { type Method , type Params , type Result , StorageRPC } from './storage' ;
11
+ import { workspace } from '#preload' ;
11
12
12
13
export type RPCInfo = {
13
14
iframe : HTMLIFrameElement ;
@@ -23,6 +24,14 @@ interface Callbacks {
23
24
) => Promise < Result [ Method . WRITE_FILE ] > ;
24
25
}
25
26
27
+ const getBasePath = async ( path : string , project : Project ) => {
28
+ if ( path === 'custom' || path . startsWith ( 'custom/' ) ) {
29
+ const homePath = await workspace . getPath ( ) ;
30
+ return `${ homePath } ` ;
31
+ }
32
+ return project . path ;
33
+ } ;
34
+
26
35
export function initRpc ( iframe : HTMLIFrameElement , project : Project , cbs : Partial < Callbacks > = { } ) {
27
36
const transport = new MessageTransport ( window , iframe . contentWindow ! ) ;
28
37
const camera = new CameraRPC ( transport ) ;
@@ -31,25 +40,25 @@ export function initRpc(iframe: HTMLIFrameElement, project: Project, cbs: Partia
31
40
const params = { iframe, project, storage, camera } ;
32
41
33
42
storage . handle ( 'read_file' , async ( { path } ) => {
34
- const file = await fs . readFile ( await fs . resolve ( project . path , path ) ) ;
43
+ const file = await fs . readFile ( await fs . resolve ( await getBasePath ( path , project ) , path ) ) ;
35
44
return file ;
36
45
} ) ;
37
46
storage . handle ( 'write_file' , async ( { path, content } ) => {
38
- await fs . writeFile ( await fs . resolve ( project . path , path ) , content as any ) ; // "content as any" since there is a mismatch in typescript's type definitions
47
+ await fs . writeFile ( await fs . resolve ( await getBasePath ( path , project ) , path ) , content as any ) ; // "content as any" since there is a mismatch in typescript's type definitions
39
48
await cbs . writeFile ?.( params , { path, content } ) ;
40
49
} ) ;
41
50
storage . handle ( 'exists' , async ( { path } ) => {
42
- return fs . exists ( await fs . resolve ( project . path , path ) ) ;
51
+ return fs . exists ( await fs . resolve ( await getBasePath ( path , project ) , path ) ) ;
43
52
} ) ;
44
53
storage . handle ( 'delete' , async ( { path } ) => {
45
- await fs . rm ( await fs . resolve ( project . path , path ) ) ;
54
+ await fs . rm ( await fs . resolve ( await getBasePath ( path , project ) , path ) ) ;
46
55
} ) ;
47
56
storage . handle ( 'list' , async ( { path } ) => {
48
- const projectPath = await fs . resolve ( project . path , path ) ;
49
- const files = await fs . readdir ( projectPath ) ;
57
+ const basePath = await fs . resolve ( await getBasePath ( path , project ) , path ) ;
58
+ const files = await fs . readdir ( basePath ) ;
50
59
const list = [ ] ;
51
60
for ( const file of files ) {
52
- const filePath = await fs . resolve ( projectPath , file ) ;
61
+ const filePath = await fs . resolve ( basePath , file ) ;
53
62
list . push ( {
54
63
name : file ,
55
64
isDirectory : await fs . isDirectory ( filePath ) ,
0 commit comments