Skip to content

Commit cce1962

Browse files
committed
feat: shared folder for custom assets
1 parent 3fd6017 commit cce1962

File tree

1 file changed

+16
-7
lines changed
  • packages/renderer/src/modules/rpc

1 file changed

+16
-7
lines changed

packages/renderer/src/modules/rpc/index.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { type Project } from '/shared/types/projects';
88

99
import { UiRPC } from './ui';
1010
import { type Method, type Params, type Result, StorageRPC } from './storage';
11+
import { workspace } from '#preload';
1112

1213
export type RPCInfo = {
1314
iframe: HTMLIFrameElement;
@@ -23,6 +24,14 @@ interface Callbacks {
2324
) => Promise<Result[Method.WRITE_FILE]>;
2425
}
2526

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+
2635
export function initRpc(iframe: HTMLIFrameElement, project: Project, cbs: Partial<Callbacks> = {}) {
2736
const transport = new MessageTransport(window, iframe.contentWindow!);
2837
const camera = new CameraRPC(transport);
@@ -31,25 +40,25 @@ export function initRpc(iframe: HTMLIFrameElement, project: Project, cbs: Partia
3140
const params = { iframe, project, storage, camera };
3241

3342
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));
3544
return file;
3645
});
3746
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
3948
await cbs.writeFile?.(params, { path, content });
4049
});
4150
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));
4352
});
4453
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));
4655
});
4756
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);
5059
const list = [];
5160
for (const file of files) {
52-
const filePath = await fs.resolve(projectPath, file);
61+
const filePath = await fs.resolve(basePath, file);
5362
list.push({
5463
name: file,
5564
isDirectory: await fs.isDirectory(filePath),

0 commit comments

Comments
 (0)