111 lines
2.6 KiB
TypeScript

#!/usr/bin/env -S deno run -A
/**
* Dev server without the Vite dev server: build + serve the Fresh app with
* inspector for debugging, and rebuild/restart on file changes.
* Use this when "deno task dev" (Vite) breaks form redirects or other behavior.
*
* Run: deno task dev:no-vite
* Debug: attach to ws://localhost:9229 (Chrome DevTools, VS Code, etc.)
*/
const PORT = Deno.env.get("PORT") || "8080";
const INSPECT = Deno.env.get("INSPECT") !== "false"; // set INSPECT=false to disable
const WATCH_DEBOUNCE_MS = 400;
const WATCH_PATHS = [
"routes",
"islands",
"main.ts",
"services",
"db",
"utils.ts",
"utils",
"helpers.ts",
"client.ts",
"assets",
"static",
];
async function build(): Promise<boolean> {
const c = new Deno.Command("deno", {
args: ["task", "build"],
cwd: Deno.cwd(),
stdout: "inherit",
stderr: "inherit",
});
const child = c.spawn();
const status = await child.status;
return status.success;
}
function startServer(): Deno.ChildProcess {
const args = [
"serve",
"-A",
"--port",
PORT,
"_fresh/server.js",
];
if (INSPECT) {
args.splice(2, 0, "--inspect=0.0.0.0:9229");
}
const c = new Deno.Command("deno", {
args,
cwd: Deno.cwd(),
stdout: "inherit",
stderr: "inherit",
});
return c.spawn();
}
async function main() {
console.log("Building...");
if (!(await build())) {
console.error("Build failed.");
Deno.exit(1);
}
let serverProcess: Deno.ChildProcess | null = startServer();
console.log(
`Server running at http://localhost:${PORT} (no Vite dev server).`,
);
if (INSPECT) {
console.log("Debugger: attach to ws://localhost:9229");
}
console.log("Watching for changes (rebuild + restart on save)...\n");
let debounceTimer: number | undefined;
const watcher = Deno.watchFs(WATCH_PATHS);
try {
for await (const event of watcher) {
const path = event.paths[0] ?? "";
if (
path.includes("node_modules") ||
path.includes("/_fresh/") ||
path.endsWith(".map")
) {
continue;
}
clearTimeout(debounceTimer);
debounceTimer = setTimeout(async () => {
debounceTimer = undefined;
console.log("\nChange detected, rebuilding...");
serverProcess?.kill();
serverProcess = null;
if (await build()) {
serverProcess = startServer();
console.log("Restarted.\n");
} else {
console.error("Build failed, not restarting.\n");
}
}, WATCH_DEBOUNCE_MS);
}
} finally {
watcher.close();
serverProcess?.kill();
}
}
main();