diff --git a/src/commands.ts b/src/commands.ts index 3ed3b99..644f76d 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -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 { 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 diff --git a/src/js/game.ts b/src/js/game.ts index 9ef1434..ee43188 100644 --- a/src/js/game.ts +++ b/src/js/game.ts @@ -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}`) diff --git a/src/js/shell.ts b/src/js/shell.ts index 2889536..e6a80de 100644 --- a/src/js/shell.ts +++ b/src/js/shell.ts @@ -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") diff --git a/src/shared/utils.ts b/src/shared/utils.ts index 7f19216..ee420dd 100644 --- a/src/shared/utils.ts +++ b/src/shared/utils.ts @@ -38,4 +38,9 @@ export function randomIndex(list: T[]): number | undefined { // unique([1,1,2,2,3,3]) #=> [1,2,3] export function unique(array: T[]): T[] { return [...new Set(array)] +} + +export async function importUrl(url: string) { + console.log("-> import", url) + return import(url) } \ No newline at end of file diff --git a/src/webapp.ts b/src/webapp.ts index af346f7..bc3ad74 100644 --- a/src/webapp.ts +++ b/src/webapp.ts @@ -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 export type App = Hono | Handler @@ -73,8 +74,7 @@ async function findApp(name: string): Promise { async function loadApp(path: string): Promise { 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 }