From 312abc11d8215ee113b3900c49e011b1a848e8c4 Mon Sep 17 00:00:00 2001 From: Chris Wanstrath Date: Mon, 29 Sep 2025 19:08:04 -0700 Subject: [PATCH] sessionGet/Set --- app/nose/bin/cat.ts | 4 ++-- app/nose/bin/cd.ts | 10 +++++----- app/nose/bin/edit.tsx | 4 ++-- app/nose/bin/load.ts | 4 ++-- app/nose/bin/ls.tsx | 4 ++-- app/nose/bin/mkdir.ts | 4 ++-- app/nose/bin/project.ts | 4 ++-- app/nose/bin/projects.tsx | 4 ++-- app/nose/bin/pwd.ts | 4 ++-- app/nose/bin/rm.ts | 4 ++-- app/nose/bin/rmdir.ts | 4 ++-- app/nose/bin/touch.ts | 4 ++-- app/src/project.ts | 4 ++-- app/src/session.ts | 4 ++-- app/src/stream.ts | 4 ++-- 15 files changed, 33 insertions(+), 33 deletions(-) diff --git a/app/nose/bin/cat.ts b/app/nose/bin/cat.ts index 48bacfc..43d5778 100644 --- a/app/nose/bin/cat.ts +++ b/app/nose/bin/cat.ts @@ -9,11 +9,11 @@ import { NOSE_WWW } from "app/src/config" import { isBinaryFile } from "app/src/utils" import { highlight } from "../lib/highlight" import { projectName, projectDir } from "@/project" -import { getState } from "@/session" +import { sessionGet } from "@/session" export default async function (path: string) { const project = projectName() - const root = getState("cwd") || projectDir() + const root = sessionGet("cwd") || projectDir() let files: string[] = [] diff --git a/app/nose/bin/cd.ts b/app/nose/bin/cd.ts index 19b14cd..d9dc0f0 100644 --- a/app/nose/bin/cd.ts +++ b/app/nose/bin/cd.ts @@ -3,14 +3,14 @@ import { dirname, resolve, isAbsolute } from "path" import { statSync } from "fs" import { projectDir } from "@/project" -import { getState, setState } from "@/session" +import { sessionGet, sessionSet } from "@/session" export default async function (path?: string) { const root = projectDir() - const cwd = getState("cwd") || root + const cwd = sessionGet("cwd") || root if (!path || path.trim() === "") { - setState("cwd", root) + sessionSet("cwd", root) return } @@ -21,7 +21,7 @@ export default async function (path?: string) { if (cwd !== root) { const parent = dirname(cwd) if (parent.startsWith(root)) { - setState("cwd", parent) + sessionSet("cwd", parent) } } return @@ -35,7 +35,7 @@ export default async function (path?: string) { try { const stat = statSync(target) if (stat.isDirectory()) { - setState("cwd", target) + sessionSet("cwd", target) return } } catch { diff --git a/app/nose/bin/edit.tsx b/app/nose/bin/edit.tsx index 0d0633f..fde8db3 100644 --- a/app/nose/bin/edit.tsx +++ b/app/nose/bin/edit.tsx @@ -8,11 +8,11 @@ import { NOSE_WWW } from "app/src/config" import { isBinaryFile } from "app/src/utils" import { countChar } from "app/src/shared/utils" import { projectName, projectDir } from "@/project" -import { getState } from "@/session" +import { sessionGet } from "@/session" export default async function (path: string) { const project = projectName() - const root = getState("cwd") || projectDir() + const root = sessionGet("cwd") || projectDir() let files: string[] = [] diff --git a/app/nose/bin/load.ts b/app/nose/bin/load.ts index 12377c6..95f70ca 100644 --- a/app/nose/bin/load.ts +++ b/app/nose/bin/load.ts @@ -1,10 +1,10 @@ // Load a project so you can work on it. import { apps } from "app/src/webapp" -import { getState } from "@/session" +import { sessionGet } from "@/session" export default function (project: string) { - const state = getState() + const state = sessionGet() if (!project) throw `usage: load ` if (state && apps().includes(project)) { diff --git a/app/nose/bin/ls.tsx b/app/nose/bin/ls.tsx index 44a1756..f7bf217 100644 --- a/app/nose/bin/ls.tsx +++ b/app/nose/bin/ls.tsx @@ -3,11 +3,11 @@ import { readdirSync } from "fs" import { NOSE_WWW } from "app/src/config" import { projectName, projectDir } from "@/project" -import { getState } from "@/session" +import { sessionGet } from "@/session" export default function () { const project = projectName() - const root = getState("cwd") || projectDir() + const root = sessionGet("cwd") || projectDir() let files: string[] = [] diff --git a/app/nose/bin/mkdir.ts b/app/nose/bin/mkdir.ts index 6732727..b24c904 100644 --- a/app/nose/bin/mkdir.ts +++ b/app/nose/bin/mkdir.ts @@ -6,12 +6,12 @@ import { mkdirSync } from "fs" import { join } from "path" import { projectDir } from "@/project" import { readdirSync } from "fs" -import { getState } from "@/session" +import { sessionGet } from "@/session" export default async function (path: string) { if (path.endsWith("/")) path = path.slice(0, path.length - 1) - const root = getState("cwd") || projectDir() + const root = sessionGet("cwd") || projectDir() for (const file of readdirSync(root, { withFileTypes: true })) if (file.name === path) throw `${path} exists` diff --git a/app/nose/bin/project.ts b/app/nose/bin/project.ts index f49e27f..e1154ac 100644 --- a/app/nose/bin/project.ts +++ b/app/nose/bin/project.ts @@ -1,9 +1,9 @@ // Print the currently loaded project. -import { getState } from "@/session" +import { sessionGet } from "@/session" export default function () { - const state = getState() + const state = sessionGet() if (!state) return { error: "no state" } return state?.project || "none" diff --git a/app/nose/bin/projects.tsx b/app/nose/bin/projects.tsx index 1e859a4..c747c3f 100644 --- a/app/nose/bin/projects.tsx +++ b/app/nose/bin/projects.tsx @@ -1,10 +1,10 @@ // Show the projects on this NOSEputer. import { apps } from "app/src/webapp" -import { getState } from "@/session" +import { sessionGet } from "@/session" export default function () { - const state = getState() + const state = sessionGet() if (!state) return { error: "no state" } return <> diff --git a/app/nose/bin/pwd.ts b/app/nose/bin/pwd.ts index 2bec605..cf89dac 100644 --- a/app/nose/bin/pwd.ts +++ b/app/nose/bin/pwd.ts @@ -2,9 +2,9 @@ import { dirname } from "path" import { projectDir } from "@/project" -import { getState } from "@/session" +import { sessionGet } from "@/session" export default async function () { const root = projectDir() - return (getState("cwd") || root).replace(dirname(root), "") + return (sessionGet("cwd") || root).replace(dirname(root), "") } \ No newline at end of file diff --git a/app/nose/bin/rm.ts b/app/nose/bin/rm.ts index a80e1ba..662539e 100644 --- a/app/nose/bin/rm.ts +++ b/app/nose/bin/rm.ts @@ -3,13 +3,13 @@ import { unlinkSync, readdirSync } from "fs" import { join } from "path" import { projectDir } from "@/project" -import { getState } from "@/session" +import { sessionGet } from "@/session" export default function (path: string) { let target = "" if (path.endsWith("/")) path = path.slice(0, path.length - 1) - const root = getState("cwd") || projectDir() + const root = sessionGet("cwd") || projectDir() for (const file of readdirSync(root, { withFileTypes: true })) if (file.name === path) { if (file.isDirectory()) diff --git a/app/nose/bin/rmdir.ts b/app/nose/bin/rmdir.ts index a924434..8a8bc5c 100644 --- a/app/nose/bin/rmdir.ts +++ b/app/nose/bin/rmdir.ts @@ -3,13 +3,13 @@ import { rmdirSync, readdirSync } from "fs" import { join } from "path" import { projectDir } from "@/project" -import { getState } from "@/session" +import { sessionGet } from "@/session" export default function (path: string) { let target = "" if (path.endsWith("/")) path = path.slice(0, path.length - 1) - const root = getState("cwd") || projectDir() + const root = sessionGet("cwd") || projectDir() for (const file of readdirSync(root, { withFileTypes: true })) if (file.name === path) { if (file.isFile()) diff --git a/app/nose/bin/touch.ts b/app/nose/bin/touch.ts index 5f6f6b9..716570e 100644 --- a/app/nose/bin/touch.ts +++ b/app/nose/bin/touch.ts @@ -3,12 +3,12 @@ import { join } from "path" import { readdirSync } from "fs" import { projectDir } from "@/project" -import { getState } from "@/session" +import { sessionGet } from "@/session" export default async function (path: string) { if (path.endsWith("/")) path = path.slice(0, path.length - 1) - const root = getState("cwd") || projectDir() + const root = sessionGet("cwd") || projectDir() for (const file of readdirSync(root, { withFileTypes: true })) if (file.name === path) throw `${path} exists` diff --git a/app/src/project.ts b/app/src/project.ts index 800d07c..7c74073 100644 --- a/app/src/project.ts +++ b/app/src/project.ts @@ -2,11 +2,11 @@ // Helpers for working with projects in the CLI. import { readdirSync, type Dirent } from "fs" -import { getState } from "./session" +import { sessionGet } from "./session" import { appDir } from "./webapp" export function projectName(): string { - const state = getState() + const state = sessionGet() if (!state) throw "no state" const project = state.project diff --git a/app/src/session.ts b/app/src/session.ts index a42a52f..9a83621 100644 --- a/app/src/session.ts +++ b/app/src/session.ts @@ -15,7 +15,7 @@ export type Session = { const g = globalThis as typeof globalThis & { __thread?: AsyncLocalStorage } export const ALS = g.__thread ??= new AsyncLocalStorage() -export function getState(key?: keyof Session): Session | any | undefined { +export function sessionGet(key?: keyof Session): Session | any | undefined { const store = ALS.getStore() if (!store) return @@ -24,7 +24,7 @@ export function getState(key?: keyof Session): Session | any | undefined { return store } -export function setState(key: keyof Session, value: any) { +export function sessionSet(key: keyof Session, value: any) { const store = ALS.getStore() if (!store) return store[key] = value diff --git a/app/src/stream.ts b/app/src/stream.ts index e4db35c..4b36434 100644 --- a/app/src/stream.ts +++ b/app/src/stream.ts @@ -1,5 +1,5 @@ import { send as sendWs } from "./websocket" -import { getState } from "./session" +import { sessionGet } from "./session" import { processExecOutput } from "./shell" import type { Child } from "hono/jsx" import type { CommandOutput, Message } from "./shared/types" @@ -9,7 +9,7 @@ type StreamFns = { replace: StreamFn, append: StreamFn } type StreamParamFn = (fns: StreamFns) => Promise export async function stream(initOrFn: StreamParamFn | string | any, fn?: StreamParamFn) { - const state = getState() + const state = sessionGet() if (!state) throw "stream() called outside runCommand()" let error = false