forked from dyad-sh/dyad
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVirtualFilesystem.ts
More file actions
354 lines (308 loc) · 9.86 KB
/
VirtualFilesystem.ts
File metadata and controls
354 lines (308 loc) · 9.86 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
import * as fs from "node:fs";
import * as path from "node:path";
import {
SyncFileSystemDelegate,
SyncVirtualFileSystem,
VirtualChanges,
VirtualFile,
} from "./tsc_types";
import { normalizePath } from "./normalizePath";
export interface AsyncFileSystemDelegate {
fileExists?: (fileName: string) => Promise<boolean>;
readFile?: (fileName: string) => Promise<string | undefined>;
}
/**
* Base class containing shared virtual filesystem functionality
*/
export abstract class BaseVirtualFileSystem {
protected virtualFiles = new Map<string, string>();
protected deletedFiles = new Set<string>();
protected baseDir: string;
constructor(baseDir: string) {
this.baseDir = path.resolve(baseDir);
}
/**
* Normalize path for consistent cross-platform behavior
*/
private normalizePathForKey(filePath: string): string {
const absolutePath = path.isAbsolute(filePath)
? filePath
: path.resolve(this.baseDir, filePath);
// Normalize separators and handle case-insensitive Windows paths
const normalized = normalizePath(path.normalize(absolutePath));
// Intentionally do NOT lowercase for Windows which is case-insensitive
// because this avoids issues with path comparison.
//
// This is a trade-off and introduces a small edge case where
// e.g. foo.txt and Foo.txt are treated as different files by the VFS
// even though Windows treats them as the same file.
//
// This should be a pretty rare occurence and it's not worth the extra
// complexity to handle it.
return normalized;
}
/**
* Convert normalized path back to platform-appropriate format
*/
private denormalizePath(normalizedPath: string): string {
return process.platform === "win32"
? normalizedPath.replace(/\//g, "\\")
: normalizedPath;
}
/**
* Apply changes from a response containing dyad tags
*/
public applyResponseChanges({
deletePaths,
renameTags,
writeTags,
}: VirtualChanges): void {
// Process deletions
for (const deletePath of deletePaths) {
this.deleteFile(deletePath);
}
// Process renames (delete old, create new)
for (const rename of renameTags) {
this.renameFile(rename.from, rename.to);
}
// Process writes
for (const writeTag of writeTags) {
this.writeFile(writeTag.path, writeTag.content);
}
}
/**
* Write a file to the virtual filesystem
*/
protected writeFile(relativePath: string, content: string): void {
const absolutePath = path.resolve(this.baseDir, relativePath);
const normalizedKey = this.normalizePathForKey(absolutePath);
this.virtualFiles.set(normalizedKey, content);
// Remove from deleted files if it was previously deleted
this.deletedFiles.delete(normalizedKey);
}
/**
* Delete a file from the virtual filesystem
*/
protected deleteFile(relativePath: string): void {
const absolutePath = path.resolve(this.baseDir, relativePath);
const normalizedKey = this.normalizePathForKey(absolutePath);
this.deletedFiles.add(normalizedKey);
// Remove from virtual files if it exists there
this.virtualFiles.delete(normalizedKey);
}
/**
* Rename a file in the virtual filesystem
*/
protected renameFile(fromPath: string, toPath: string): void {
const fromAbsolute = path.resolve(this.baseDir, fromPath);
const toAbsolute = path.resolve(this.baseDir, toPath);
const fromNormalized = this.normalizePathForKey(fromAbsolute);
const toNormalized = this.normalizePathForKey(toAbsolute);
// Mark old file as deleted
this.deletedFiles.add(fromNormalized);
// If the source file exists in virtual files, move its content
if (this.virtualFiles.has(fromNormalized)) {
const content = this.virtualFiles.get(fromNormalized)!;
this.virtualFiles.delete(fromNormalized);
this.virtualFiles.set(toNormalized, content);
} else {
// Try to read from actual filesystem
try {
const content = fs.readFileSync(fromAbsolute, "utf8");
this.virtualFiles.set(toNormalized, content);
} catch (error) {
// If we can't read the source file, we'll let the consumer handle it
console.warn(
`Could not read source file for rename: ${fromPath}`,
error,
);
}
}
// Remove destination from deleted files if it was previously deleted
this.deletedFiles.delete(toNormalized);
}
/**
* Get all virtual files (files that have been written or modified)
*/
public getVirtualFiles(): VirtualFile[] {
return Array.from(this.virtualFiles.entries()).map(
([normalizedKey, content]) => {
// Convert normalized key back to relative path
const denormalizedPath = this.denormalizePath(normalizedKey);
return {
path: path.relative(this.baseDir, denormalizedPath),
content,
};
},
);
}
/**
* Get all deleted file paths (relative to base directory)
*/
public getDeletedFiles(): string[] {
return Array.from(this.deletedFiles).map((normalizedKey) => {
// Convert normalized key back to relative path
const denormalizedPath = this.denormalizePath(normalizedKey);
return path.relative(this.baseDir, denormalizedPath);
});
}
/**
* Check if a file is deleted in the virtual filesystem
*/
protected isDeleted(filePath: string): boolean {
const normalizedKey = this.normalizePathForKey(filePath);
return this.deletedFiles.has(normalizedKey);
}
/**
* Check if a file exists in virtual files
*/
protected hasVirtualFile(filePath: string): boolean {
const normalizedKey = this.normalizePathForKey(filePath);
return this.virtualFiles.has(normalizedKey);
}
/**
* Get virtual file content
*/
protected getVirtualFileContent(filePath: string): string | undefined {
const normalizedKey = this.normalizePathForKey(filePath);
return this.virtualFiles.get(normalizedKey);
}
}
/**
* Synchronous virtual filesystem
*/
export class SyncVirtualFileSystemImpl
extends BaseVirtualFileSystem
implements SyncVirtualFileSystem
{
private delegate: SyncFileSystemDelegate;
constructor(baseDir: string, delegate?: SyncFileSystemDelegate) {
super(baseDir);
this.delegate = delegate || {};
}
/**
* Check if a file exists in the virtual filesystem
*/
public fileExists(filePath: string): boolean {
// Check if file is deleted
if (this.isDeleted(filePath)) {
return false;
}
// Check if file exists in virtual files
if (this.hasVirtualFile(filePath)) {
return true;
}
// Delegate to custom fileExists if provided
if (this.delegate.fileExists) {
return this.delegate.fileExists(filePath);
}
// Fall back to actual filesystem
const absolutePath = path.isAbsolute(filePath)
? filePath
: path.resolve(this.baseDir, filePath);
return fs.existsSync(absolutePath);
}
/**
* Read a file from the virtual filesystem
*/
public readFile(filePath: string): string | undefined {
// Check if file is deleted
if (this.isDeleted(filePath)) {
return undefined;
}
// Check virtual files first
const virtualContent = this.getVirtualFileContent(filePath);
if (virtualContent !== undefined) {
return virtualContent;
}
// Delegate to custom readFile if provided
if (this.delegate.readFile) {
return this.delegate.readFile(filePath);
}
// Fall back to actual filesystem
try {
const absolutePath = path.isAbsolute(filePath)
? filePath
: path.resolve(this.baseDir, filePath);
return fs.readFileSync(absolutePath, "utf8");
} catch {
return undefined;
}
}
/**
* Create a custom file system interface for other tools
*/
public createFileSystemInterface() {
return {
fileExists: (fileName: string) => this.fileExists(fileName),
readFile: (fileName: string) => this.readFile(fileName),
writeFile: (fileName: string, content: string) =>
this.writeFile(fileName, content),
deleteFile: (fileName: string) => this.deleteFile(fileName),
};
}
}
/**
* Asynchronous virtual filesystem
*/
export class AsyncVirtualFileSystem extends BaseVirtualFileSystem {
private delegate: AsyncFileSystemDelegate;
constructor(baseDir: string, delegate?: AsyncFileSystemDelegate) {
super(baseDir);
this.delegate = delegate || {};
}
/**
* Check if a file exists in the virtual filesystem
*/
public async fileExists(filePath: string): Promise<boolean> {
// Check if file is deleted
if (this.isDeleted(filePath)) {
return false;
}
// Check if file exists in virtual files
if (this.hasVirtualFile(filePath)) {
return true;
}
// Delegate to custom fileExists if provided
if (this.delegate.fileExists) {
return this.delegate.fileExists(filePath);
}
// Fall back to actual filesystem
try {
const absolutePath = path.isAbsolute(filePath)
? filePath
: path.resolve(this.baseDir, filePath);
await fs.promises.access(absolutePath);
return true;
} catch {
return false;
}
}
/**
* Read a file from the virtual filesystem
*/
public async readFile(filePath: string): Promise<string | undefined> {
// Check if file is deleted
if (this.isDeleted(filePath)) {
return undefined;
}
// Check virtual files first
const virtualContent = this.getVirtualFileContent(filePath);
if (virtualContent !== undefined) {
return virtualContent;
}
// Delegate to custom readFile if provided
if (this.delegate.readFile) {
return this.delegate.readFile(filePath);
}
// Fall back to actual filesystem
try {
const absolutePath = path.isAbsolute(filePath)
? filePath
: path.resolve(this.baseDir, filePath);
return await fs.promises.readFile(absolutePath, "utf8");
} catch {
return undefined;
}
}
}