nose-pluto/src/webapp/utils.ts
2025-10-10 15:40:40 -07:00

53 lines
1.5 KiB
TypeScript

import { join } from "path"
import { readdirSync } from "fs"
import type { Child } from "hono/jsx"
import { NOSE_DIR } from "../config"
import { isFile, isDir } from "../utils"
export async function appPath(appName: string): Promise<string | undefined> {
const files = [join(NOSE_DIR, appName, "index.ts"), join(NOSE_DIR, appName, "index.tsx")]
for (const file of files)
if (await Bun.file(file).exists())
return file
}
export async function toResponse(source: string | Child | Response): Promise<Response> {
if (source instanceof Response)
return source
else if (typeof source === "string")
return new Response(source)
else
return new Response(await source?.toString(), {
headers: {
"Content-Type": "text/html; charset=utf-8"
}
})
}
export function apps(): string[] {
const apps: string[] = []
for (const entry of readdirSync(NOSE_DIR))
if (isApp(entry))
apps.push(entry)
return apps.sort()
}
export function isApp(name: string): boolean {
return isFile(join(NOSE_DIR, name, "index.ts"))
|| isFile(join(NOSE_DIR, name, "index.tsx"))
|| isDir(join(NOSE_DIR, name, "pub"))
}
export function isStaticApp(name: string): boolean {
return !isFile(join(NOSE_DIR, name, "index.ts"))
&& !isFile(join(NOSE_DIR, name, "index.tsx"))
&& isDir(join(NOSE_DIR, name, "pub"))
}
export function appDir(name: string): string | undefined {
if (isApp(name))
return join(NOSE_DIR, name)
}