Skip to content

Commit

Permalink
enable jsh
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyan-dfinity committed Sep 14, 2024
1 parent 8125a4f commit 7b90932
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ export function App() {
await container.run_cmd("npm", ["install"], {
cwd: "utils",
});
container.start_shell();
// fetch code after loading base library
if (hasUrlParams) {
const files = await fetchFromUrlParams(workplaceDispatch);
Expand Down
39 changes: 29 additions & 10 deletions src/webcontainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ export class Container {

async init() {
if (!this.container) {
this.container = await WebContainer.boot({ coep: "credentialless" });
this.container = await WebContainer.boot({
coep: "credentialless",
workdirName: "playground",
});
}
return this.container;
}
Expand Down Expand Up @@ -54,21 +57,37 @@ export class Container {
};
await this.container!.mount(files);
}

async run_cmd(cmd: string, args: string[], options?: SpawnOptions) {
await this.init();
if (options?.output ?? true) {
this.terminal.writeln(`/${options?.cwd ?? ""}$ ${cmd} ${args.join(" ")}`);
}
const installProcess = await this.container!.spawn(cmd, args, options);
installProcess.output.pipeTo(
private async run_cmd_inner(
cmd: string,
args: string[],
options?: SpawnOptions,
) {
const process = await this.container!.spawn(cmd, args, options);
process.output.pipeTo(
new WritableStream({
write: (data) => {
this.terminal.write(data);
},
}),
);
const exitCode = await installProcess.exit;
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);
});
return process;
}
async run_cmd(cmd: string, args: string[], options?: SpawnOptions) {
await this.init();
if (options?.output ?? true) {
this.terminal.writeln(`/${options?.cwd ?? ""}$ ${cmd} ${args.join(" ")}`);
}
const process = await this.run_cmd_inner(cmd, args, options);
const exitCode = await process.exit;
if (exitCode !== 0) {
this.terminal.writeln(`\r\nexited with code ${exitCode}`);
throw new Error(`Command failed with code ${exitCode}`);
Expand Down

0 comments on commit 7b90932

Please sign in to comment.