sessionGet/Set

This commit is contained in:
Chris Wanstrath 2025-09-29 19:08:04 -07:00
parent 8b8f17a9fc
commit 312abc11d8
15 changed files with 33 additions and 33 deletions

View File

@ -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[] = []

View File

@ -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 {

View File

@ -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[] = []

View File

@ -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 <project name>`
if (state && apps().includes(project)) {

View File

@ -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[] = []

View File

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

View File

@ -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"

View File

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

View File

@ -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), "")
}

View File

@ -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())

View File

@ -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())

View File

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

View File

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

View File

@ -15,7 +15,7 @@ export type Session = {
const g = globalThis as typeof globalThis & { __thread?: AsyncLocalStorage<Session> }
export const ALS = g.__thread ??= new AsyncLocalStorage<Session>()
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

View File

@ -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<void>
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