Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyan-dfinity committed Sep 15, 2024
1 parent d5697c0 commit 763eb5c
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions src/webcontainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,7 @@ export class Container {
};
await this.container!.mount(files);
}
private async run_cmd_inner(
cmd: string,
args: string[],
options?: SpawnOptions,
) {
private async spawn(cmd: string, args: string[], options?: SpawnOptions) {
const new_options = {
...options,
terminal: {
Expand All @@ -70,18 +66,22 @@ export class Container {
},
};
const process = await this.container!.spawn(cmd, args, new_options);
return process;
}
async start_shell() {
await this.init();
const process = await this.spawn("jsh", []);
process.output.pipeTo(
new WritableStream({
write: (data) => {
this.terminal.write(data);
if (data.includes("command not found: moc")) {
this.terminal.writeln("haha, you called moc!");
} else {
this.terminal.write(data);
}
},
}),
);
return process;
}
async start_shell() {
await this.init();
const process = await this.run_cmd_inner("jsh", []);
const input = process.input.getWriter();
this.terminal.onData((data) => {
input.write(data);
Expand All @@ -93,7 +93,14 @@ export class Container {
if (options?.output ?? true) {
this.terminal.writeln(`/${options?.cwd ?? ""}$ ${cmd} ${args.join(" ")}`);
}
const process = await this.run_cmd_inner(cmd, args, options);
const process = await this.spawn(cmd, args, options);
process.output.pipeTo(
new WritableStream({
write: (data) => {
this.terminal.write(data);
},
}),
);
const exitCode = await process.exit;
if (exitCode !== 0) {
this.terminal.writeln(`\r\nexited with code ${exitCode}`);
Expand Down

0 comments on commit 763eb5c

Please sign in to comment.