Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
dist/
# generated types
.astro/
public/snapshot

# dependencies
node_modules/
Expand Down
6 changes: 6 additions & 0 deletions astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ export default defineConfig({
site: "https://bomb.sh/",
base: "/docs",
outDir: "./dist/docs/",
server: {
headers: {
'Cross-Origin-Embedder-Policy': 'require-corp',
'Cross-Origin-Opener-Policy': 'same-origin'
}
},
integrations: [
starlight({
title: "Bombshell",
Expand Down
13 changes: 12 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
"version": "0.0.1",
"scripts": {
"dev": "astro dev",
"dev:snapshot": "",
"start": "astro dev",
"prebuild": "node run snapshot",
"build": "astro build",
"preview": "astro preview",
"astro": "astro"
"astro": "astro",
"snapshot": "node ./scripts/snapshot.ts"
},
"dependencies": {
"@astrojs/starlight": "^0.37.1",
Expand All @@ -17,9 +20,17 @@
"@types/node": "^22.19.3",
"astro": "^5.16.6",
"expressive-code-twoslash": "^0.5.3",
"@webcontainer/api": "^1.6.1",
"@webcontainer/snapshot": "^0.1.0",
"@xterm/addon-fit": "^0.11.0",
"@xterm/addon-web-links": "^0.12.0",
"@xterm/xterm": "^6.0.0",
"sharp": "^0.33.5",
"starlight-sidebar-topics": "^0.6.2"
},
"devDependencies": {
"tinyexec": "^1.0.2"
},
"pnpm": {
"onlyBuiltDependencies": [
"esbuild",
Expand Down
53 changes: 53 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 45 additions & 0 deletions scripts/snapshot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import fs from 'node:fs/promises';
import { fileURLToPath } from 'node:url';
import { snapshot } from '@webcontainer/snapshot';
import {x} from 'tinyexec';
import { createHash } from "node:crypto";

const PACKAGE_JSON = {
name: 'example',
type: 'module',
version: '0.0.0',
dependencies: {
"@bomb.sh/args": "latest",
"@clack/core": "1.0.0-alpha.0",
"@clack/prompts": "1.0.0-alpha.0"
}
}
const IGNORE_FILES = ['*.md', '*.d.*', '*.map', 'LICENSE', 'license'];
const rootDir = new URL('../', import.meta.url);
const snapshotDir = new URL(`./snapshot-${hash()}/`, rootDir);
const outFile = new URL('./public/snapshot', rootDir);

async function run() {
await fs.mkdir(snapshotDir, { recursive: true });
await fs.writeFile(new URL('package.json', snapshotDir), JSON.stringify(PACKAGE_JSON));
await x('npm', ['install'], {
nodeOptions: {
cwd: fileURLToPath(snapshotDir),
}
})
for await (const file of fs.glob(IGNORE_FILES.map(file => `**/${file}`), { cwd: fileURLToPath(snapshotDir) })) {
await fs.rm(new URL(file, snapshotDir));
}
const output = await snapshot(fileURLToPath(snapshotDir));
await fs.writeFile(outFile, output);
await fs.rm(snapshotDir, { recursive: true, force: true });
console.log('snapshot generated');
}

run();

function hash() {
return createHash("shake256", { outputLength: 8 })
.update(Date.now().toString())
.digest("hex");
}
69 changes: 69 additions & 0 deletions src/components/WebContainer/Terminal.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<docs-terminal></docs-terminal>

<script>
import type { Terminal } from "@xterm/xterm";
import { theme } from "./theme.ts";

customElements.define(
"docs-terminal",
class extends HTMLElement {
instance: Terminal | undefined;
ro: ResizeObserver | undefined;
updateSize = () => {};

connectedCallback() {
this.boot();
}
disconnectedCallback() {
this.instance?.dispose();
this.ro?.disconnect();
}
handleResize() {
this.ro = new ResizeObserver(() => this.updateSize());
this.ro.observe(this);
}
async boot() {
if (this.instance) {
this.instance.options.theme = theme;
this.instance.open(this);
return;
}
this.innerHTML = "";
const [{ Terminal }, { FitAddon }, { WebLinksAddon }] =
await Promise.all([
import("@xterm/xterm"),
import("@xterm/addon-fit"),
import("@xterm/addon-web-links"),
]);
this.instance = new Terminal({
convertEol: true,
cursorBlink: false,
disableStdin: false,
theme,
fontSize: 22,
fontFamily: "Menlo, courier-new, courier, monospace",
});

this.instance.open(this);

const fit = new FitAddon();
this.instance.loadAddon(fit);
this.instance.loadAddon(new WebLinksAddon());
this.updateSize = () => fit.fit();
this.updateSize();
this.handleResize();
}
}
);
</script>

<style is:global>
@import "@xterm/xterm/css/xterm.css" layer(xterm);

.xterm {
padding: 12px;
}
.xterm-viewport {
background: #16181D;
}
</style>
103 changes: 103 additions & 0 deletions src/components/WebContainer/WebContainer.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
---
import Terminal from "./Terminal.astro";

interface Props {
file: string;
}
const { file } = Astro.props;
---

<web-container name={file}>
<pre data-content><code><slot /></code></pre>
<Terminal />
</web-container>

<script>
import type { Terminal } from "@xterm/xterm";
import { WebContainer } from "@webcontainer/api";
let host: WebContainer;

host = await WebContainer.boot({ workdirName: 'demo' });
const snapshotResponse = await fetch(`/docs/snapshot`);
const snapshot = await snapshotResponse.arrayBuffer();
await host.mount(snapshot, { mountPoint: "/" });

customElements.define(
"web-container",
class extends HTMLElement {
get dir() {
return `${this.name}/`
}
get file() {
return `${this.dir}index.js`;
}
get fileContent() {
const text = this.querySelector("[data-content]")!.textContent;
return `import { intro, outro } from "@clack/prompts";console.clear();intro("\\x1b[46m\\x1b[30m ${this.name} \\x1b[0m");\n${text};process.on('exit', () => console.log('EOF'));`;
}
get name() {
return this.getAttribute("name")!;
}
get terminal(): Terminal | undefined {
return (this.querySelector("docs-terminal") as any)?.instance;
}
async connectedCallback() {
await host.fs.mkdir(this.dir, { recursive: true });
await host.fs.writeFile(
this.file,
this.fileContent,
{ encoding: "utf-8" }
);
await this.render();
}

async render() {
const { terminal, name, render } = this;
terminal?.reset();
const main = async () => {
// we set an infinite loop so that when the user runs the `exit` command, we restart
while (true) {
const jsh = Promise.withResolvers();
let isJSHReady = false;
const process = await host.spawn("jsh", {
cwd: name,
terminal: { rows: terminal?.rows!, cols: terminal?.cols! },
});
process.output.pipeTo(
new WritableStream({
write(data) {
if (data.includes("❯") && !isJSHReady) {
isJSHReady = true;
jsh.resolve(undefined);
}
if (data.includes("❯") || data.includes('~/demo')) {
return;
}
if (data.includes("EOF")) {
process.kill();
render();
return;
}
terminal?.write(data);
}
})
);
const shell = process.input.getWriter();
await jsh.promise;
await shell.write(`node index.js\n`);
// write the terminal input to the process
const terminalWriter = terminal?.onData((data) => {
shell.write(data);
});
// wait for the process to finish
await process.exit;
terminal?.clear();
terminalWriter?.dispose();
}
}

main();
}
}
);
</script>
22 changes: 22 additions & 0 deletions src/components/WebContainer/theme.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export const theme = {
cursor: '#eff0eb',
cursorAccent: '#00000000',
foreground: "#eff0eb",
background: "#16181D",
red: "#ff5c57",
green: "#5af78e",
yellow: "#f3f99d",
blue: "#57c7ff",
magenta: "#ff6ac1",
cyan: "#9aedfe",
white: "#f1f1f0",
brightBlack: "#686868",
brightRed: "#ff5c57",
brightGreen: "#5af78e",
brightYellow: "#f3f99d",
brightBlue: "#57c7ff",
brightMagenta: "#ff6ac1",
brightCyan: "#9aedfe",
brightWhite: "#f1f1f0",
selectionBackground: "#97979b33",
};
Loading