Skip to content

Commit

Permalink
prompt when webcontainer fails to boot
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyan-dfinity committed Sep 15, 2024
1 parent cf51e1f commit 9b31b24
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
8 changes: 7 additions & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,13 @@ export function App() {
output: false,
});
container.start_shell();
logger.log("Shell started in the terminal tab");
if (container.isDummy) {
logger.log(
"WebContainer is not supported in this browser. Frontend build is disabled.",
);
} else {
logger.log("Shell started in the terminal tab");
}
// fetch code after loading base library
if (hasUrlParams) {
const files = await fetchFromUrlParams(workplaceDispatch);
Expand Down
33 changes: 29 additions & 4 deletions src/webcontainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,36 @@ import { Terminal } from "@xterm/xterm";

export class Container {
public container: WebContainer | null = null;
public isDummy = false;
private terminal: Terminal;

constructor(terminal: Terminal) {
this.terminal = terminal;
}

async init() {
if (this.isDummy) {
return;
}
if (!this.container) {
this.container = await WebContainer.boot({
coep: "credentialless",
workdirName: "playground",
});
try {
this.container = await WebContainer.boot({
coep: "credentialless",
workdirName: "playground",
});
} catch (err) {
this.isDummy = true;
console.error(err);
}
}
return this.container;
}

async initFiles() {
await this.init();
if (this.isDummy) {
return;
}
const nodeFiles = import.meta.glob("./node/*", {
query: "?raw",
import: "default",
Expand Down Expand Up @@ -67,6 +79,9 @@ export class Container {
},
) {
await this.init();
if (this.isDummy) {
return;
}
await this.container!.fs.writeFile(path, contents, options);
}
async mkdir(
Expand All @@ -76,6 +91,9 @@ export class Container {
},
) {
await this.init();
if (this.isDummy) {
return;
}
await this.container!.fs.mkdir(path, options);
}
private async spawn(cmd: string, args: string[], options?: SpawnOptions) {
Expand All @@ -91,6 +109,10 @@ export class Container {
}
async start_shell() {
await this.init();
if (this.isDummy) {
this.terminal.writeln("WebContainer fails to initialize.");
return;
}
const process = await this.spawn("jsh", []);
process.output.pipeTo(
new WritableStream({
Expand All @@ -111,6 +133,9 @@ export class Container {
}
async run_cmd(cmd: string, args: string[], options?: SpawnOptions) {
await this.init();
if (this.isDummy) {
return;
}
if (options?.output ?? true) {
this.terminal.writeln(`/${options?.cwd ?? ""}$ ${cmd} ${args.join(" ")}`);
}
Expand Down

0 comments on commit 9b31b24

Please sign in to comment.