mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 16:23:36 +00:00
Abstract WebContainer into "JSRuntime" type
This commit is contained in:
1 parent
0986dd4e9d
commit
78398bcd7d
8 files changed
+316
-102
No files matched your search
@@ -0,0 +1,40 @@
|
||||
import type { WebContainerProcess } from '@webcontainer/api';
|
||||
|
||||
/**
|
||||
* Directory entry with file type information
|
||||
*/
|
||||
export interface DirectoryEntry {
|
||||
name: string;
|
||||
isDirectory(): boolean;
|
||||
isFile(): boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filesystem interface compatible with both WebContainer and LightningFS
|
||||
*/
|
||||
export interface JSRuntimeFS {
|
||||
readFile(path: string, encoding?: string): Promise<string | Uint8Array>;
|
||||
writeFile(path: string, data: string | Uint8Array): Promise<void>;
|
||||
readdir(path: string, options?: { withFileTypes?: boolean }): Promise<string[] | DirectoryEntry[]>;
|
||||
mkdir(path: string, options?: { recursive?: boolean }): Promise<void>;
|
||||
stat(path: string): Promise<{ isDirectory(): boolean; isFile(): boolean }>;
|
||||
}
|
||||
|
||||
/**
|
||||
* JavaScript runtime interface for executing code and managing files
|
||||
*/
|
||||
export interface JSRuntime {
|
||||
/**
|
||||
* Filesystem access
|
||||
*/
|
||||
readonly fs: JSRuntimeFS;
|
||||
|
||||
/**
|
||||
* Spawn a process in the runtime environment
|
||||
* @param command Command to execute
|
||||
* @param args Command arguments
|
||||
* @param options Spawn options
|
||||
* @returns Process handle
|
||||
*/
|
||||
spawn(command: string, args?: string[], options?: Record<string, unknown>): Promise<WebContainerProcess>;
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
import { WebContainer, type WebContainerProcess, type FileSystemTree } from '@webcontainer/api';
|
||||
import type { JSRuntime, JSRuntimeFS, DirectoryEntry } from './JSRuntime';
|
||||
|
||||
/**
|
||||
* WebContainer adapter that implements the JSRuntime interface
|
||||
*/
|
||||
export class WebContainerAdapter implements JSRuntime {
|
||||
private webcontainer: WebContainer | null = null;
|
||||
private bootPromise: Promise<WebContainer> | null = null;
|
||||
private _fs: JSRuntimeFS | null = null;
|
||||
|
||||
/**
|
||||
* Get the filesystem interface
|
||||
*/
|
||||
get fs(): JSRuntimeFS {
|
||||
if (!this._fs) {
|
||||
throw new Error('WebContainer not initialized. Call spawn() first to initialize.');
|
||||
}
|
||||
return this._fs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure WebContainer is booted
|
||||
*/
|
||||
private async ensureBooted(): Promise<WebContainer> {
|
||||
if (this.webcontainer) {
|
||||
return this.webcontainer;
|
||||
}
|
||||
|
||||
if (!this.bootPromise) {
|
||||
this.bootPromise = WebContainer.boot();
|
||||
}
|
||||
|
||||
this.webcontainer = await this.bootPromise;
|
||||
this._fs = new WebContainerFSAdapter(this.webcontainer);
|
||||
return this.webcontainer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Spawn a process in the WebContainer
|
||||
* Automatically boots the container on first call
|
||||
*/
|
||||
async spawn(command: string, args: string[] = [], options: Record<string, unknown> = {}): Promise<WebContainerProcess> {
|
||||
const webcontainer = await this.ensureBooted();
|
||||
return webcontainer.spawn(command, args, options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Mount a file tree in the WebContainer
|
||||
* This is a convenience method for setting up the container
|
||||
*/
|
||||
async mount(tree: FileSystemTree): Promise<void> {
|
||||
const webcontainer = await this.ensureBooted();
|
||||
return webcontainer.mount(tree);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* WebContainer filesystem adapter that implements JSRuntimeFS
|
||||
*/
|
||||
class WebContainerFSAdapter implements JSRuntimeFS {
|
||||
constructor(private webcontainer: WebContainer) {}
|
||||
|
||||
async readFile(path: string, encoding?: string): Promise<string | Uint8Array> {
|
||||
if (encoding) {
|
||||
return this.webcontainer.fs.readFile(path, encoding as 'utf8');
|
||||
} else {
|
||||
return this.webcontainer.fs.readFile(path);
|
||||
}
|
||||
}
|
||||
|
||||
async writeFile(path: string, data: string | Uint8Array): Promise<void> {
|
||||
return this.webcontainer.fs.writeFile(path, data);
|
||||
}
|
||||
|
||||
async readdir(path: string, options?: { withFileTypes?: boolean }): Promise<string[] | DirectoryEntry[]> {
|
||||
if (options?.withFileTypes) {
|
||||
return this.webcontainer.fs.readdir(path, { withFileTypes: true }) as Promise<DirectoryEntry[]>;
|
||||
} else {
|
||||
return this.webcontainer.fs.readdir(path) as Promise<string[]>;
|
||||
}
|
||||
}
|
||||
|
||||
async mkdir(path: string, options?: { recursive?: boolean }): Promise<void> {
|
||||
if (options?.recursive) {
|
||||
await this.webcontainer.fs.mkdir(path, { recursive: true });
|
||||
} else {
|
||||
await this.webcontainer.fs.mkdir(path);
|
||||
}
|
||||
}
|
||||
|
||||
async stat(path: string): Promise<{ isDirectory(): boolean; isFile(): boolean }> {
|
||||
// WebContainer doesn't have a stat method, so we'll implement a workaround
|
||||
try {
|
||||
// Try to read as directory first
|
||||
await this.webcontainer.fs.readdir(path);
|
||||
return {
|
||||
isDirectory: () => true,
|
||||
isFile: () => false,
|
||||
};
|
||||
} catch {
|
||||
// If readdir fails, assume it's a file
|
||||
return {
|
||||
isDirectory: () => false,
|
||||
isFile: () => true,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
import type { JSRuntimeFS, DirectoryEntry } from './JSRuntime';
|
||||
import type LightningFS from '@isomorphic-git/lightning-fs';
|
||||
|
||||
/**
|
||||
* Copy files and directories between two filesystem instances
|
||||
* @param sourceFS Source filesystem (e.g., WebContainer fs)
|
||||
* @param targetFS Target filesystem (e.g., LightningFS)
|
||||
* @param sourcePath Source directory path
|
||||
* @param targetPath Target directory path
|
||||
*/
|
||||
export async function copyFiles(
|
||||
sourceFS: JSRuntimeFS,
|
||||
targetFS: LightningFS.PromisifiedFS,
|
||||
sourcePath: string,
|
||||
targetPath: string
|
||||
): Promise<void> {
|
||||
try {
|
||||
console.log(`Copying: ${sourcePath} -> ${targetPath}`);
|
||||
|
||||
// Get source directory contents
|
||||
const items = await sourceFS.readdir(sourcePath, { withFileTypes: true }) as DirectoryEntry[];
|
||||
console.log(`Found ${items.length} items in ${sourcePath}`);
|
||||
|
||||
// Ensure target directory exists
|
||||
try {
|
||||
await targetFS.mkdir(targetPath);
|
||||
} catch {
|
||||
// Directory might already exist, that's fine
|
||||
}
|
||||
|
||||
// Copy each item
|
||||
for (const item of items) {
|
||||
const sourceItemPath = `${sourcePath}/${item.name}`;
|
||||
const targetItemPath = `${targetPath}/${item.name}`;
|
||||
|
||||
try {
|
||||
if (item.isDirectory()) {
|
||||
console.log(`Creating directory: ${targetItemPath}`);
|
||||
try {
|
||||
await targetFS.mkdir(targetItemPath);
|
||||
} catch {
|
||||
// Directory might already exist
|
||||
}
|
||||
// Recursively copy directory contents
|
||||
await copyFiles(sourceFS, targetFS, sourceItemPath, targetItemPath);
|
||||
} else {
|
||||
console.log(`Copying file: ${sourceItemPath} -> ${targetItemPath}`);
|
||||
const content = await sourceFS.readFile(sourceItemPath);
|
||||
await targetFS.writeFile(targetItemPath, content);
|
||||
}
|
||||
} catch (itemError) {
|
||||
console.warn(`Failed to copy item ${item.name}:`, itemError);
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`Successfully copied ${sourcePath} to ${targetPath}`);
|
||||
} catch (error) {
|
||||
console.error(`Failed to copy ${sourcePath}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy a directory from JSRuntime filesystem to LightningFS
|
||||
* Convenience wrapper for copyFiles with better error handling
|
||||
*/
|
||||
export async function copyDirectory(
|
||||
sourceFS: JSRuntimeFS,
|
||||
targetFS: LightningFS.PromisifiedFS,
|
||||
sourcePath: string,
|
||||
targetPath: string
|
||||
): Promise<void> {
|
||||
try {
|
||||
// Check if source exists and is a directory
|
||||
const sourceStat = await sourceFS.stat(sourcePath);
|
||||
if (!sourceStat.isDirectory()) {
|
||||
throw new Error(`Source path ${sourcePath} is not a directory`);
|
||||
}
|
||||
|
||||
await copyFiles(sourceFS, targetFS, sourcePath, targetPath);
|
||||
} catch (error) {
|
||||
console.error(`Failed to copy directory ${sourcePath} to ${targetPath}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user