feat(filetree): add filesystem watch support to WebContainer environment and auto-refresh file tree; improve icon handling and context menu behavior

This commit is contained in:
2025-12-31 08:53:01 +00:00
parent 9e229543eb
commit e193e28fe9
7 changed files with 345 additions and 27 deletions

View File

@@ -1,5 +1,5 @@
import * as webcontainer from '@webcontainer/api';
import type { IExecutionEnvironment, IFileEntry, IProcessHandle } from '../interfaces/IExecutionEnvironment.js';
import type { IExecutionEnvironment, IFileEntry, IFileWatcher, IProcessHandle } from '../interfaces/IExecutionEnvironment.js';
/**
* WebContainer-based execution environment.
@@ -123,6 +123,22 @@ export class WebContainerEnvironment implements IExecutionEnvironment {
}
}
public watch(
path: string,
callback: (event: 'rename' | 'change', filename: string | null) => void,
options?: { recursive?: boolean }
): IFileWatcher {
this.ensureReady();
const watcher = this.container!.fs.watch(
path,
{ recursive: options?.recursive ?? false },
callback
);
return {
stop: () => watcher.close(),
};
}
// ============ Process Execution ============
public async spawn(command: string, args: string[] = []): Promise<IProcessHandle> {

View File

@@ -7,6 +7,14 @@ export interface IFileEntry {
path: string;
}
/**
* Handle to a file system watcher
*/
export interface IFileWatcher {
/** Stop watching for changes */
stop(): void;
}
/**
* Handle to a spawned process with I/O streams
*/
@@ -68,6 +76,19 @@ export interface IExecutionEnvironment {
*/
exists(path: string): Promise<boolean>;
/**
* Watch a file or directory for changes
* @param path - Absolute path to watch
* @param callback - Called when changes occur
* @param options - Optional: { recursive: true } to watch subdirectories
* @returns Watcher handle with stop() method
*/
watch(
path: string,
callback: (event: 'rename' | 'change', filename: string | null) => void,
options?: { recursive?: boolean }
): IFileWatcher;
// ============ Process Execution ============
/**