Compare commits

..

2 Commits

Author SHA1 Message Date
Chris Wanstrath
872a1c3009 give NOSE_DIR its own package.json 2025-10-08 13:45:11 -07:00
Chris Wanstrath
2b7a7eea1c cache cmds/webapps by epoch in prod 2025-10-08 13:44:16 -07:00
6 changed files with 38 additions and 10 deletions

14
nose/package.json Normal file
View File

@ -0,0 +1,14 @@
{
"name": "nose_dir",
"type": "module",
"private": true,
"dependencies": {
"hono": "^4.9.10"
},
"devDependencies": {
"@types/bun": "latest"
},
"peerDependencies": {
"typescript": "^5"
}
}

View File

@ -1,5 +1,5 @@
////
// version: c584db7
// version: df700ff
var __defProp = Object.defineProperty;
var __export = (target, all) => {
@ -83,9 +83,12 @@ __export(exports_scrollback, {
function randomId() {
return Math.random().toString(36).slice(7);
}
async function importUrl(url) {
async function importUrl(url, cacheTag) {
url += url.includes("?") ? "&" : "?";
url += "t=" + (nodeEnv() === "production" ? gitSHA() : Date.now());
if (cacheTag)
url += "t=" + cacheTag;
else
url += "t=" + (nodeEnv() === "production" ? gitSHA() : Date.now());
console.log("-> import", url);
return import(url);
}

View File

@ -6,7 +6,7 @@ import { watch, readFileSync } from "fs"
import { join, basename } from "path"
import { isFile } from "./utils"
import { sendAll } from "./websocket"
import { expectDir } from "./utils"
import { expectDir, mtimeEpoch } from "./utils"
import { importUrl } from "./shared/utils"
import type { Command, Commands } from "./shared/types"
import { projectBin, projectName } from "./project"
@ -82,7 +82,11 @@ export async function commandSource(name: string): Promise<string> {
export async function loadCommandModule(cmd: string) {
const path = commandPath(cmd)
if (!path) return
return await importUrl(path)
if (path.startsWith(NOSE_DIR))
return await importUrl(path, await mtimeEpoch(path))
else
return await importUrl(path)
}
let noseDirWatcher

View File

@ -40,10 +40,13 @@ export function unique<T>(array: T[]): T[] {
return [...new Set(array)]
}
// import a typescript module. caches in production, doesn't in dev.
export async function importUrl(url: string) {
// import a typescript module. caches by sha in production, doesn't cache in dev.
export async function importUrl(url: string, cacheTag?: string) {
url += url.includes("?") ? "&" : "?"
url += "t=" + (nodeEnv() === "production" ? gitSHA() : Date.now())
if (cacheTag)
url += "t=" + cacheTag
else
url += "t=" + (nodeEnv() === "production" ? gitSHA() : Date.now())
console.log("-> import", url)
return import(url)

View File

@ -63,6 +63,10 @@ export async function mtime(path: string): Promise<Date> {
return mtime
}
export async function mtimeEpoch(path: string): Promise<string> {
return (await mtime(path)).getTime().toString()
}
// is the given file binary?
export async function isBinaryFile(path: string): Promise<boolean> {
// Create a stream to read just the beginning

View File

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