importUrl

This commit is contained in:
Chris Wanstrath 2025-10-07 18:37:28 -07:00
parent 238b3c4dd6
commit 9ddc36e014
5 changed files with 13 additions and 10 deletions

View File

@ -7,6 +7,7 @@ import { join, basename } from "path"
import { isFile } from "./utils"
import { sendAll } from "./websocket"
import { expectDir } from "./utils"
import { importUrl } from "./shared/utils"
import type { Command, Commands } from "./shared/types"
import { projectBin, projectName } from "./project"
import { DEFAULT_PROJECT, NOSE_DIR, NOSE_ROOT_BIN, NOSE_BIN } from "./config"
@ -81,8 +82,7 @@ export async function commandSource(name: string): Promise<string> {
export async function loadCommandModule(cmd: string) {
const path = commandPath(cmd)
if (!path) return
console.log("-> import", path + "?t+" + Date.now())
return await import(path + "?t+" + Date.now())
return await importUrl(path + "?t+" + Date.now())
}
let noseDirWatcher

View File

@ -2,7 +2,7 @@ import type { GameStartMessage } from "../shared/types"
import { GameContext, type InputState } from "../shared/game"
import { focusInput } from "./focus"
import { $$ } from "./dom"
import { randomId } from "../shared/utils"
import { randomId, importUrl } from "../shared/utils"
import { setStatus, addOutput, insert } from "./scrollback"
import { sessionId } from "./session"
import { mode } from "./commands"
@ -35,8 +35,7 @@ export async function handleGameStart(msg: GameStartMessage) {
let game
try {
console.log("-> import", `/source/${name}?session=${sessionId}&t=${Date.now()}`)
game = await import(`/source/${name}?session=${sessionId}&t=${Date.now()}`)
game = await importUrl(`/source/${name}?session=${sessionId}&t=${Date.now()}`)
} catch (err: any) {
setStatus(msgId, "error")
addOutput(msgId, `Error: ${err.message ? err.message : err}`)

View File

@ -4,7 +4,7 @@
import type { InputMessage } from "../shared/types"
import { addInput, setStatus, addOutput } from "./scrollback"
import { send } from "./websocket"
import { randomId } from "../shared/utils"
import { randomId, importUrl } from "../shared/utils"
import { addToHistory } from "./history"
import { commands } from "./commands"
@ -24,8 +24,7 @@ export async function runCommand(input: string) {
const [cmd = "", ...args] = input.split(" ")
if (commands[cmd]?.type === "browser") {
console.log("-> import", `/source/${cmd}?t=${Date.now()}`)
const mod = await import(`/source/${cmd}?t=${Date.now()}`)
const mod = await importUrl(`/source/${cmd}?t=${Date.now()}`)
if (!mod.default) {
addOutput(id, `no default export in ${cmd}`)
setStatus(id, "error")

View File

@ -38,4 +38,9 @@ export function randomIndex<T>(list: T[]): number | undefined {
// unique([1,1,2,2,3,3]) #=> [1,2,3]
export function unique<T>(array: T[]): T[] {
return [...new Set(array)]
}
export async function importUrl(url: string) {
console.log("-> import", url)
return import(url)
}

View File

@ -10,6 +10,7 @@ import { sendAll } from "./websocket"
import { expectDir } from "./utils"
import { NOSE_DIR } from "./config"
import { isFile, isDir } from "./utils"
import { importUrl } from "./shared/utils"
export type Handler = (r: Context) => string | Child | Response | Promise<Response>
export type App = Hono | Handler
@ -73,8 +74,7 @@ async function findApp(name: string): Promise<App | undefined> {
async function loadApp(path: string): Promise<App | undefined> {
if (!await Bun.file(path).exists()) return
console.log("-> import", path + `?t+${Date.now()}`)
const mod = await import(path + `?t=${Date.now()}`)
const mod = await importUrl(path + `?t=${Date.now()}`)
if (mod?.default)
return mod.default as App
}