break everything
This commit is contained in:
parent
7f695eb9eb
commit
12f4ce9657
67
app/src/helpers.tsx
Normal file
67
app/src/helpers.tsx
Normal 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
|
||||||
|
}
|
||||||
|
|
@ -2,6 +2,6 @@
|
||||||
// Each browser tab is a shell session. This means you can run multiple sessions
|
// Each browser tab is a shell session. This means you can run multiple sessions
|
||||||
// in the same browser.
|
// in the same browser.
|
||||||
|
|
||||||
import { randomID } from "../shared/utils.js"
|
import { randomId } from "../shared/utils.js"
|
||||||
|
|
||||||
export const sessionID = randomID()
|
export const sessionID = randomId()
|
||||||
|
|
@ -4,14 +4,14 @@
|
||||||
import type { Message, CommandResult } from "../shared/types.js"
|
import type { Message, CommandResult } from "../shared/types.js"
|
||||||
import { addInput, setStatus, addOutput } from "./scrollback.js"
|
import { addInput, setStatus, addOutput } from "./scrollback.js"
|
||||||
import { send } from "./websocket.js"
|
import { send } from "./websocket.js"
|
||||||
import { randomID } from "../shared/utils.js"
|
import { randomId } from "../shared/utils.js"
|
||||||
import { addToHistory } from "./history.js"
|
import { addToHistory } from "./history.js"
|
||||||
import { browserCommands, cacheCommands } from "./commands.js"
|
import { browserCommands, cacheCommands } from "./commands.js"
|
||||||
|
|
||||||
export function runCommand(input: string) {
|
export function runCommand(input: string) {
|
||||||
if (!input.trim()) return
|
if (!input.trim()) return
|
||||||
|
|
||||||
const id = randomID()
|
const id = randomId()
|
||||||
|
|
||||||
addToHistory(input)
|
addToHistory(input)
|
||||||
addInput(id, input)
|
addInput(id, input)
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,6 @@ export function countChar(str: string, char: string): number {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate a 6 character random ID
|
// Generate a 6 character random ID
|
||||||
export function randomID(): string {
|
export function randomId(): string {
|
||||||
return Math.random().toString(36).slice(7)
|
return Math.random().toString(36).slice(7)
|
||||||
}
|
}
|
||||||
|
|
@ -1,11 +1,10 @@
|
||||||
////
|
////
|
||||||
// Shell utilities and helper functions.
|
// Shell utilities and helper functions.
|
||||||
|
|
||||||
import { Hono } from "hono"
|
|
||||||
import { statSync } from "fs"
|
import { statSync } from "fs"
|
||||||
import { basename } from "path"
|
import { basename } from "path"
|
||||||
import { stat } from "node:fs/promises"
|
import { stat } from "node:fs/promises"
|
||||||
import { type Handler, toResponse } from "./webapp"
|
|
||||||
import { NOSE_ICON } from "./config"
|
import { NOSE_ICON } from "./config"
|
||||||
|
|
||||||
// End the process with an instructive error if a directory doesn't exist.
|
// 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
|
// Convert /Users/$USER or /home/$USER to ~ for simplicity
|
||||||
export function tilde(path: string): string {
|
export function tilde(path: string): string {
|
||||||
return path.replace(new RegExp(`/(Users|home)/${process.env.USER}`), "~")
|
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 transpiler = new Bun.Transpiler({ loader: 'tsx' })
|
||||||
const transpileCache: Record<string, string> = {}
|
const transpileCache: Record<string, string> = {}
|
||||||
|
|
||||||
|
|
@ -95,45 +88,3 @@ export async function transpile(path: string): Promise<string> {
|
||||||
|
|
||||||
return cached
|
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
|
|
||||||
}
|
|
||||||
|
|
@ -22,7 +22,7 @@
|
||||||
"@types/bun": "latest"
|
"@types/bun": "latest"
|
||||||
},
|
},
|
||||||
"alias": {
|
"alias": {
|
||||||
"@utils": "./app/src/utils.tsx",
|
"@nose": "./app/src/helpers.tsx",
|
||||||
"@config": "./app/src/config.ts",
|
"@config": "./app/src/config.ts",
|
||||||
"@/*": "./app/src/*"
|
"@/*": "./app/src/*"
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user