diff --git a/app/src/commands.ts b/app/src/commands.ts index 3a1e76f..144d641 100644 --- a/app/src/commands.ts +++ b/app/src/commands.ts @@ -3,6 +3,8 @@ import { Glob } from "bun" import { watch } from "fs" +import { join } from "path" +import { isFile } from "./utils" import { sendAll } from "./websocket" import { expectDir } from "./utils" import { NOSE_SYS_BIN, NOSE_BIN } from "./config" @@ -29,4 +31,24 @@ export async function findCommands(path: string): Promise { } return list +} + +export function commandPath(cmd: string): string | undefined { + return [ + join(NOSE_SYS_BIN, cmd + ".ts"), + join(NOSE_SYS_BIN, cmd + ".tsx"), + join(NOSE_BIN, cmd + ".ts"), + join(NOSE_BIN, cmd + ".tsx") + ].find((path: string) => isFile(path)) +} + +export function commandExists(cmd: string): boolean { + return commandPath(cmd) !== undefined +} + + +export async function commandSource(name: string): Promise { + const path = commandPath(name) + if (!path) return "" + return Bun.file(path).text() } \ No newline at end of file diff --git a/app/src/server.tsx b/app/src/server.tsx index f1061ba..3692c0e 100644 --- a/app/src/server.tsx +++ b/app/src/server.tsx @@ -9,9 +9,9 @@ import color from "kleur" import type { Message } from "./shared/types" import { NOSE_ICON, NOSE_BIN, NOSE_WWW } from "./config" import { transpile, isFile, tilde } from "./utils" -import { apps, serveApp } from "./webapp" +import { serveApp } from "./webapp" import { initDNS } from "./dns" -import { commands } from "./commands" +import { commands, commandSource, commandPath } from "./commands" import { send, addWebsocket, removeWebsocket, closeWebsockets } from "./websocket" import { Layout } from "./html/layout" @@ -76,16 +76,15 @@ app.use("*", async (c, next) => { return next() }) -app.get("/apps", c => { - const url = new URL(c.req.url) - const domain = url.hostname - let port = url.port - port = port && port !== "80" ? `:${port}` : "" - - return c.html(<> -

apps

- - ) +app.get("/command/:name", async c => { + const name = c.req.param("name") + const path = commandPath(name) + if (!path) return c.text("Command not found", 404) + return new Response(await transpile(path), { + headers: { + "Content-Type": "text/javascript" + } + }) }) app.get("/", c => c.html()) diff --git a/app/src/shell.ts b/app/src/shell.ts index 8cc4eab..5f08558 100644 --- a/app/src/shell.ts +++ b/app/src/shell.ts @@ -2,11 +2,9 @@ // Runs commands and such on the server. // This is the "shell" - the "terminal" is the browser UI. -import { join } from "path" import type { CommandResult, CommandOutput } from "./shared/types" import type { Session } from "./session" -import { NOSE_SYS_BIN, NOSE_BIN } from "./config" -import { isFile } from "./utils" +import { commandExists, commandPath } from "./commands" import { ALS } from "./session" const sessions: Map = new Map() @@ -69,19 +67,6 @@ function getState(sessionId: string, taskId: string, ws?: any): Session { return state } -function commandPath(cmd: string): string | undefined { - return [ - join(NOSE_SYS_BIN, cmd + ".ts"), - join(NOSE_SYS_BIN, cmd + ".tsx"), - join(NOSE_BIN, cmd + ".ts"), - join(NOSE_BIN, cmd + ".tsx") - ].find((path: string) => isFile(path)) -} - -function commandExists(cmd: string): boolean { - return commandPath(cmd) !== undefined -} - function errorMessage(error: Error | any): string { if (!(error instanceof Error)) return String(error)