@aprovan/patchwork-editor 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/lib/vfs.ts ADDED
@@ -0,0 +1,106 @@
1
+ import {
2
+ VFSStore,
3
+ LocalFSBackend,
4
+ type VirtualProject,
5
+ type VirtualFile
6
+ } from '@aprovan/patchwork-compiler';
7
+
8
+ /**
9
+ * VFS client for persisting virtual projects to the stitchery server.
10
+ * Uses LocalFSBackend which makes HTTP requests to /vfs routes.
11
+ */
12
+
13
+ // VFS base URL - points to stitchery server's VFS routes
14
+ const VFS_BASE_URL = '/vfs';
15
+
16
+ // Cached VFS config
17
+ let vfsConfigCache: { usePaths: boolean } | null = null;
18
+
19
+ /**
20
+ * Get VFS configuration from the server.
21
+ * Caches the result for subsequent calls.
22
+ */
23
+ export async function getVFSConfig(): Promise<{ usePaths: boolean }> {
24
+ if (vfsConfigCache) return vfsConfigCache;
25
+
26
+ try {
27
+ const res = await fetch(`${VFS_BASE_URL}/config`);
28
+ if (res.ok) {
29
+ vfsConfigCache = await res.json();
30
+ return vfsConfigCache!;
31
+ }
32
+ } catch {
33
+ // Server not available, use default
34
+ }
35
+
36
+ return { usePaths: false };
37
+ }
38
+
39
+ // Create a singleton store instance for dev mode
40
+ let storeInstance: VFSStore | null = null;
41
+
42
+ /**
43
+ * Get the VFS store instance (creates one if needed).
44
+ * Store uses LocalFSBackend to persist to the stitchery server.
45
+ */
46
+ export function getVFSStore(): VFSStore {
47
+ if (!storeInstance) {
48
+ const backend = new LocalFSBackend({ baseUrl: VFS_BASE_URL });
49
+ storeInstance = new VFSStore(backend);
50
+ }
51
+ return storeInstance;
52
+ }
53
+
54
+ /**
55
+ * Save a virtual project to disk via the stitchery server.
56
+ * Projects are saved under their ID in the VFS directory.
57
+ */
58
+ export async function saveProject(project: VirtualProject): Promise<void> {
59
+ const store = getVFSStore();
60
+ await store.saveProject(project);
61
+ }
62
+
63
+ /**
64
+ * Load a project from disk by ID.
65
+ */
66
+ export async function loadProject(id: string): Promise<VirtualProject | null> {
67
+ const store = getVFSStore();
68
+ return store.loadProject(id);
69
+ }
70
+
71
+ /**
72
+ * List all stored project IDs.
73
+ */
74
+ export async function listProjects(): Promise<string[]> {
75
+ const store = getVFSStore();
76
+ const files = await store.listFiles();
77
+
78
+ // Extract unique project IDs (first path segment)
79
+ const projectIds = new Set<string>();
80
+ for (const file of files) {
81
+ const id = file.split('/')[0];
82
+ if (id) projectIds.add(id);
83
+ }
84
+
85
+ return Array.from(projectIds);
86
+ }
87
+
88
+ /**
89
+ * Save a single file to the VFS.
90
+ */
91
+ export async function saveFile(file: VirtualFile): Promise<void> {
92
+ const store = getVFSStore();
93
+ await store.putFile(file);
94
+ }
95
+
96
+ /**
97
+ * Check if VFS is available (stitchery server is running with vfs-dir enabled).
98
+ */
99
+ export async function isVFSAvailable(): Promise<boolean> {
100
+ try {
101
+ const res = await fetch(VFS_BASE_URL, { method: 'HEAD' });
102
+ return res.ok || res.status === 404; // 404 on root is expected if empty
103
+ } catch {
104
+ return false;
105
+ }
106
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,10 @@
1
+ {
2
+ "extends": "../../tsconfig.json",
3
+ "compilerOptions": {
4
+ "outDir": "./dist",
5
+ "rootDir": "./src",
6
+ "jsx": "react-jsx"
7
+ },
8
+ "include": ["src/**/*"],
9
+ "exclude": ["node_modules", "dist"]
10
+ }
package/tsup.config.ts ADDED
@@ -0,0 +1,10 @@
1
+ import { defineConfig } from 'tsup';
2
+
3
+ export default defineConfig({
4
+ entry: ['src/index.ts'],
5
+ format: ['esm'],
6
+ dts: true,
7
+ clean: true,
8
+ external: ['react', 'react-dom'],
9
+ treeshake: true,
10
+ });