From d8f7ed63cd6dbcf8329f66c279687b02783f5e37 Mon Sep 17 00:00:00 2001 From: Chris Wanstrath <2+defunkt@users.noreply.github.com> Date: Sun, 21 Sep 2025 11:06:49 -0700 Subject: [PATCH] load projects --- nose/bin/load.ts | 12 ++++++++++++ nose/bin/project.ts | 5 +++++ nose/bin/projects.ts | 5 +++++ src/server.tsx | 2 +- src/shell.ts | 22 ++++++++++++++++++++-- 5 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 nose/bin/load.ts create mode 100644 nose/bin/project.ts create mode 100644 nose/bin/projects.ts diff --git a/nose/bin/load.ts b/nose/bin/load.ts new file mode 100644 index 0000000..5ef2eb6 --- /dev/null +++ b/nose/bin/load.ts @@ -0,0 +1,12 @@ +import { apps } from "@/webapp" +import { Thread } from "@/shell" + +export default function (project: string) { + const state = Thread.getStore() + + if (apps().includes(project) && state) { + state.project = project + } + + return state?.project ? `loaded ${project}` : `failed to load ${project}` +} \ No newline at end of file diff --git a/nose/bin/project.ts b/nose/bin/project.ts new file mode 100644 index 0000000..89f0487 --- /dev/null +++ b/nose/bin/project.ts @@ -0,0 +1,5 @@ +import { Thread } from "@/shell" + +export default function () { + return Thread.getStore()?.project || "none" +} \ No newline at end of file diff --git a/nose/bin/projects.ts b/nose/bin/projects.ts new file mode 100644 index 0000000..22993ae --- /dev/null +++ b/nose/bin/projects.ts @@ -0,0 +1,5 @@ +import { apps } from "@/webapp" + +export default function () { + return apps().join(" ") +} \ No newline at end of file diff --git a/src/server.tsx b/src/server.tsx index 9b41cc3..7ec2900 100644 --- a/src/server.tsx +++ b/src/server.tsx @@ -73,7 +73,7 @@ app.get("/ws", upgradeWebSocket(async c => { if (!data) return - const result = await runCommand(data.data as string) + const result = await runCommand(data.session || "", data.id || "", data.data as string) send(ws, { id: data.id, type: "output", data: result }) }, onClose: (event, ws) => { diff --git a/src/shell.ts b/src/shell.ts index 4ef102f..15ba3fa 100644 --- a/src/shell.ts +++ b/src/shell.ts @@ -5,8 +5,19 @@ import type { CommandResult } from "./shared/types" import { join } from "path" import { NOSE_SYS_BIN, NOSE_USR_BIN } from "./config" import { isFile } from "./utils" +import { AsyncLocalStorage } from "async_hooks" -export async function runCommand(input: string): Promise { +export const Thread = new AsyncLocalStorage() + +type State = { + id?: string + session?: string + project?: string +} + +const sessions: Map = new Map() + +export async function runCommand(session: string, id: string, input: string): Promise { const [cmd = "", ...args] = input.split(" ") if (!commandExists(cmd)) { @@ -16,8 +27,15 @@ export async function runCommand(input: string): Promise { let status: "ok" | "error" = "ok" let output = "" + let state = sessions.get(session) + if (!state) { + state = { session, id, project: "" } + sessions.set(session, state) + } + state.id = id + try { - [status, output] = await exec(cmd, args) + [status, output] = await Thread.run(state, async () => await exec(cmd, args)) } catch (err) { status = "error" output = errorMessage(err)