break everything

This commit is contained in:
Chris Wanstrath 2025-09-26 10:16:59 -07:00
parent 7f695eb9eb
commit 12f4ce9657
6 changed files with 74 additions and 56 deletions

67
app/src/helpers.tsx Normal file
View File

@ -0,0 +1,67 @@
////
// Helpers for writing NOSE webapps & cli commands
//
// Be *very careful* modifying this file, as people's scripts and www's will depend
// on the API. We need to eventually version it and provide backwards compat.
//
// Access them in your command or webapp:
// import { css } from "@nose"
import { Hono } from "hono"
import { type Handler, toResponse } from "./webapp"
//
// command helpers
//
// (none for now)
//
// webapp helpers
//
const transpiler = new Bun.Transpiler({ loader: 'tsx' })
export function css(strings: TemplateStringsArray, ...values: any[]) {
return <style dangerouslySetInnerHTML={
{
__html: strings.reduce((result, str, i) => {
return result + str + (values[i] || '')
}, '')
}
} />
}
export function js(strings: TemplateStringsArray, ...values: any[]) {
return <script dangerouslySetInnerHTML={
{
__html: strings.reduce((result, str, i) => {
return transpiler.transformSync(result + str + (values[i] || ''))
}, '')
}
} />
}
// for defining routes in your NOSE webapp
// example:
// export default routes({
// "GET /": index,
// "GET /pets": pets
// })
export function routes(def: Record<string, Handler>): Hono {
const app = new Hono
for (const key in def) {
const parts = key.split(" ") // GET /path
const method = parts[0] || "GET"
const path = parts[1] || "/"
console.log(method, path, def[key])
//@ts-ignore
app.on(method, path, async c => toResponse(await def[key](c)))
}
return app
}

View File

@ -2,6 +2,6 @@
// Each browser tab is a shell session. This means you can run multiple sessions
// in the same browser.
import { randomID } from "../shared/utils.js"
import { randomId } from "../shared/utils.js"
export const sessionID = randomID()
export const sessionID = randomId()

View File

@ -4,14 +4,14 @@
import type { Message, CommandResult } from "../shared/types.js"
import { addInput, setStatus, addOutput } from "./scrollback.js"
import { send } from "./websocket.js"
import { randomID } from "../shared/utils.js"
import { randomId } from "../shared/utils.js"
import { addToHistory } from "./history.js"
import { browserCommands, cacheCommands } from "./commands.js"
export function runCommand(input: string) {
if (!input.trim()) return
const id = randomID()
const id = randomId()
addToHistory(input)
addInput(id, input)

View File

@ -4,6 +4,6 @@ export function countChar(str: string, char: string): number {
}
// Generate a 6 character random ID
export function randomID(): string {
export function randomId(): string {
return Math.random().toString(36).slice(7)
}

View File

@ -1,11 +1,10 @@
////
// Shell utilities and helper functions.
import { Hono } from "hono"
import { statSync } from "fs"
import { basename } from "path"
import { stat } from "node:fs/promises"
import { type Handler, toResponse } from "./webapp"
import { NOSE_ICON } from "./config"
// End the process with an instructive error if a directory doesn't exist.
@ -66,17 +65,11 @@ export async function isBinaryFile(path: string): Promise<boolean> {
}
}
// Convert /Users/$USER or /home/$USER to ~ for simplicity
export function tilde(path: string): string {
return path.replace(new RegExp(`/(Users|home)/${process.env.USER}`), "~")
}
// Generate a random 8 character string
export function randomID(): string {
return Math.random().toString(36).slice(2, 10)
}
const transpiler = new Bun.Transpiler({ loader: 'tsx' })
const transpileCache: Record<string, string> = {}
@ -95,45 +88,3 @@ export async function transpile(path: string): Promise<string> {
return cached
}
//
// webapp utils (for writing webapps)
//
export function css(strings: TemplateStringsArray, ...values: any[]) {
return <style dangerouslySetInnerHTML={{
__html: strings.reduce((result, str, i) => {
return result + str + (values[i] || '')
}, '')
}} />
}
export function js(strings: TemplateStringsArray, ...values: any[]) {
return <script dangerouslySetInnerHTML={{
__html: strings.reduce((result, str, i) => {
return transpiler.transformSync(result + str + (values[i] || ''))
}, '')
}} />
}
// for defining routes in your NOSE webapp
// example:
// export default routes({
// "GET /": index,
// "GET /pets": pets
// })
export function routes(def: Record<string, Handler>): Hono {
const app = new Hono
for (const key in def) {
const parts = key.split(" ") // GET /path
const method = parts[0] || "GET"
const path = parts[1] || "/"
console.log(method, path, def[key])
//@ts-ignore
app.on(method, path, async c => toResponse(await def[key](c)))
}
return app
}

View File

@ -22,7 +22,7 @@
"@types/bun": "latest"
},
"alias": {
"@utils": "./app/src/utils.tsx",
"@nose": "./app/src/helpers.tsx",
"@config": "./app/src/config.ts",
"@/*": "./app/src/*"
}