load projects

This commit is contained in:
Chris Wanstrath 2025-09-21 11:06:49 -07:00
parent bd82e81972
commit d8f7ed63cd
5 changed files with 43 additions and 3 deletions

12
nose/bin/load.ts Normal file
View File

@ -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}`
}

5
nose/bin/project.ts Normal file
View File

@ -0,0 +1,5 @@
import { Thread } from "@/shell"
export default function () {
return Thread.getStore()?.project || "none"
}

5
nose/bin/projects.ts Normal file
View File

@ -0,0 +1,5 @@
import { apps } from "@/webapp"
export default function () {
return apps().join(" ")
}

View File

@ -73,7 +73,7 @@ app.get("/ws", upgradeWebSocket(async c => {
if (!data) return 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 }) send(ws, { id: data.id, type: "output", data: result })
}, },
onClose: (event, ws) => { onClose: (event, ws) => {

View File

@ -5,8 +5,19 @@ import type { CommandResult } from "./shared/types"
import { join } from "path" import { join } from "path"
import { NOSE_SYS_BIN, NOSE_USR_BIN } from "./config" import { NOSE_SYS_BIN, NOSE_USR_BIN } from "./config"
import { isFile } from "./utils" import { isFile } from "./utils"
import { AsyncLocalStorage } from "async_hooks"
export async function runCommand(input: string): Promise<CommandResult> { export const Thread = new AsyncLocalStorage<State>()
type State = {
id?: string
session?: string
project?: string
}
const sessions: Map<string, State> = new Map()
export async function runCommand(session: string, id: string, input: string): Promise<CommandResult> {
const [cmd = "", ...args] = input.split(" ") const [cmd = "", ...args] = input.split(" ")
if (!commandExists(cmd)) { if (!commandExists(cmd)) {
@ -16,8 +27,15 @@ export async function runCommand(input: string): Promise<CommandResult> {
let status: "ok" | "error" = "ok" let status: "ok" | "error" = "ok"
let output = "" let output = ""
let state = sessions.get(session)
if (!state) {
state = { session, id, project: "" }
sessions.set(session, state)
}
state.id = id
try { try {
[status, output] = await exec(cmd, args) [status, output] = await Thread.run(state, async () => await exec(cmd, args))
} catch (err) { } catch (err) {
status = "error" status = "error"
output = errorMessage(err) output = errorMessage(err)