Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/crutchcorn/codewrapper. A modular, framework-agnostic library for building interactive code editors codewrapper.vercel.app
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071import { FileSystemTree, WebContainer, WebContainerProcess,} from "@webcontainer/api";import { Terminal } from "@xterm/xterm";import { Store } from "@tanstack/store";
export async function initCodeExecution(files: FileSystemTree) { const container = await WebContainer.boot(); await container.mount(files);
const serverData = new Store<{ port: number; url: string } | null>(null);
container.on("server-ready", (port, url) => { serverData.setState(() => ({ port, url })); });
return { container, serverData };}
export async function startShellAndAttachToTerminal( webContainer: WebContainer, terminal: Terminal,) { const shellProcess = await webContainer.spawn("jsh", { terminal: { cols: terminal.cols, rows: terminal.rows, }, });
void shellProcess.output.pipeTo( new WritableStream({ write(data) { terminal.write(data); }, }), );
const input = shellProcess.input.getWriter();
terminal.onData((data) => { void input.write(data); });
return shellProcess;}
/** * Requires access to the DOM * Used in conjunction with `fitTerm` * * Must be called **after** `fitTerm` */export function fitShell( shellProcess: WebContainerProcess, terminal: Terminal,) { function resizeShell() { shellProcess.resize({ cols: terminal.cols, rows: terminal.rows, }); }
window.addEventListener("resize", resizeShell);
return { cleanup: () => window.removeEventListener("resize", resizeShell) };}