Compare commits

...

2 Commits

Author SHA1 Message Date
ad04ab79cd update shrimp 2025-11-05 09:00:50 -08:00
bf52c1a593 nice error messages 2025-11-05 09:00:48 -08:00
4 changed files with 20 additions and 6 deletions

View File

@ -4,6 +4,7 @@
"": {
"name": "foam",
"dependencies": {
"ansi_up": "^6.0.6",
"hono": "^4.10.4",
"shrimp": "git+https://git.nose.space/probablycorey/shrimp",
},
@ -46,6 +47,8 @@
"@types/react": ["@types/react@19.2.2", "", { "dependencies": { "csstype": "^3.0.2" } }, "sha512-6mDvHUFSjyT2B2yeNx2nUgMxh9LtOWvkhIU3uePn2I2oyNymUAX1NIsdgviM4CH+JSrp2D2hsMvJOkxY+0wNRA=="],
"ansi_up": ["ansi_up@6.0.6", "", {}, "sha512-yIa1x3Ecf8jWP4UWEunNjqNX6gzE4vg2gGz+xqRGY+TBSucnYp6RRdPV4brmtg6bQ1ljD48mZ5iGSEj7QEpRKA=="],
"bun-plugin-tailwind": ["bun-plugin-tailwind@0.0.15", "", { "peerDependencies": { "typescript": "^5.0.0" } }, "sha512-qtAXMNGG4R0UGGI8zWrqm2B7BdXqx48vunJXBPzfDOHPA5WkRUZdTSbE7TFwO4jLhYqSE23YMWsM9NhE6ovobw=="],
"bun-types": ["bun-types@1.3.1", "", { "dependencies": { "@types/node": "*" }, "peerDependencies": { "@types/react": "^19" } }, "sha512-NMrcy7smratanWJ2mMXdpatalovtxVggkj11bScuWuiOoXTiKIu2eVS1/7qbyI/4yHedtsn175n4Sm4JcdHLXw=="],
@ -60,7 +63,7 @@
"reefvm": ["reefvm@git+https://git.nose.space/defunkt/reefvm#0f39e9401eb7a0a7c906e150127f9829458a79b6", { "peerDependencies": { "typescript": "^5" } }, "0f39e9401eb7a0a7c906e150127f9829458a79b6"],
"shrimp": ["shrimp@git+https://git.nose.space/probablycorey/shrimp#d707ee7e6b074cc0d64179004e5b6cc8250e0c91", { "dependencies": { "@codemirror/view": "^6.38.3", "@lezer/generator": "^1.8.0", "bun-plugin-tailwind": "^0.0.15", "codemirror": "^6.0.2", "hono": "^4.9.8", "reefvm": "git+https://git.nose.space/defunkt/reefvm", "tailwindcss": "^4.1.11" } }, "d707ee7e6b074cc0d64179004e5b6cc8250e0c91"],
"shrimp": ["shrimp@git+https://git.nose.space/probablycorey/shrimp#e0e5e828692713fcaf5d62f88d3ad2c3a43802d4", { "dependencies": { "@codemirror/view": "^6.38.3", "@lezer/generator": "^1.8.0", "bun-plugin-tailwind": "^0.0.15", "codemirror": "^6.0.2", "hono": "^4.9.8", "reefvm": "git+https://git.nose.space/defunkt/reefvm", "tailwindcss": "^4.1.11" } }, "e0e5e828692713fcaf5d62f88d3ad2c3a43802d4"],
"style-mod": ["style-mod@4.1.3", "", {}, "sha512-i/n8VsZydrugj3Iuzll8+x/00GH2vnYsk1eomD8QiRrSAeW6ItbCQDtfXCeJHd0iwiNagqjQkvpvREEPtW3IoQ=="],

View File

@ -17,6 +17,7 @@
"typescript": "^5"
},
"dependencies": {
"ansi_up": "^6.0.6",
"hono": "^4.10.4",
"shrimp": "git+https://git.nose.space/probablycorey/shrimp"
}

View File

View File

@ -2,13 +2,14 @@ import { Hono } from 'hono'
import { serveStatic } from 'hono/bun'
import { join, resolve } from 'path'
import { wrapAndRunCode } from './ribbit'
import { AnsiUp } from 'ansi_up'
export function startWeb(rootPath: string) {
const root = resolve(rootPath)
const app = new Hono()
// console logging
app.use("*", async (c, next) => {
app.use('*', async (c, next) => {
const start = Date.now()
await next()
const end = Date.now()
@ -18,13 +19,22 @@ export function startWeb(rootPath: string) {
// static files get served out of pub/
app.use('/*', serveStatic({ root: join(root, 'pub') }))
app.on(['GET', 'POST'], ['/', '/:page{.+}'], async c => {
app.on(['GET', 'POST'], ['/', '/:page{.+}'], async (c) => {
const page = c.req.param('page') || 'index'
const params = c.req.query()
const file = Bun.file(join(root, `${page}.sh`))
const path = join(root, `${page}.sh`)
const file = Bun.file(path)
if (await file.exists()) {
return c.html(await wrapAndRunCode(await file.text(), { params }))
try {
return c.html(await wrapAndRunCode(await file.text(), { params }))
} catch (e) {
let error = e instanceof Error ? e.message : String(e)
const ansiUp = new AnsiUp()
const errorHtml = ansiUp.ansi_to_html(error)
const blue = '#42A5F5'
return c.html(`<h1 style='color:${blue}'>🫧 Error in <a href='vscode://file/${path}' style='text-decoration:none;border-bottom:1px dotted ${blue};color:${blue}'>${path}</a></h1><pre>${errorHtml}</pre>`)
}
} else {
return c.text('404 Not Found', 404)
}
@ -35,4 +45,4 @@ export function startWeb(rootPath: string) {
fetch: app.fetch,
port: 3000,
})
}
}