release: WRNexusJS 0.8.0
This commit is contained in:
+81
-2
@@ -13,6 +13,7 @@
|
||||
|
||||
import { spawn, type ChildProcess } from "node:child_process";
|
||||
import { resolve, join } from "node:path";
|
||||
import { existsSync, watch, type FSWatcher } from "node:fs";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { RESTART_EXIT_CODE } from "@wrnexus/dev-server";
|
||||
|
||||
@@ -20,7 +21,12 @@ import { RESTART_EXIT_CODE } from "@wrnexus/dev-server";
|
||||
// it works whether @wrnexus/dev-server is a workspace or an installed dependency.
|
||||
const SERVE_ENTRY = fileURLToPath(import.meta.resolve("@wrnexus/dev-server/serve-entry"));
|
||||
|
||||
export function runDev(appRoot: string, port: number, hostname = "::"): void {
|
||||
export function runDev(
|
||||
appRoot: string,
|
||||
port: number,
|
||||
hostname = "::",
|
||||
tls?: { certFile: string; keyFile: string },
|
||||
): void {
|
||||
const appDir = join(resolve(appRoot), "app");
|
||||
let child: ChildProcess | null = null;
|
||||
let shuttingDown = false;
|
||||
@@ -28,7 +34,15 @@ export function runDev(appRoot: string, port: number, hostname = "::"): void {
|
||||
const spawnChild = (): void => {
|
||||
child = spawn(
|
||||
process.execPath, // the Bun binary
|
||||
[SERVE_ENTRY, appDir, String(port), "development", hostname],
|
||||
[
|
||||
SERVE_ENTRY,
|
||||
appDir,
|
||||
String(port),
|
||||
"development",
|
||||
hostname,
|
||||
"true",
|
||||
...(tls ? [tls.certFile, tls.keyFile] : []),
|
||||
],
|
||||
{ stdio: "inherit" },
|
||||
);
|
||||
|
||||
@@ -81,3 +95,68 @@ export function runDev(appRoot: string, port: number, hostname = "::"): void {
|
||||
process.on("SIGINT", shutdown);
|
||||
process.on("SIGTERM", shutdown);
|
||||
}
|
||||
|
||||
export async function runProductionDev(
|
||||
appRoot: string,
|
||||
port: number,
|
||||
hostname = "::",
|
||||
): Promise<void> {
|
||||
const root = resolve(appRoot);
|
||||
let child: ChildProcess | undefined;
|
||||
let building = false;
|
||||
let pending = false;
|
||||
let timer: ReturnType<typeof setTimeout> | undefined;
|
||||
const { runBuild } = await import("./build.ts");
|
||||
|
||||
const rebuild = async (): Promise<void> => {
|
||||
if (building) {
|
||||
pending = true;
|
||||
return;
|
||||
}
|
||||
building = true;
|
||||
try {
|
||||
await runBuild(root);
|
||||
child?.kill();
|
||||
const { runPreview } = await import("./preview.ts");
|
||||
child = runPreview(root, { port, hostname, developmentRuntime: true });
|
||||
} catch (error) {
|
||||
console.error(
|
||||
"[wrnexus] production-runtime rebuild failed; keeping the last good server.",
|
||||
error,
|
||||
);
|
||||
} finally {
|
||||
building = false;
|
||||
if (pending) {
|
||||
pending = false;
|
||||
void rebuild();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
console.log(`\n ⚡ WrNexus dev (exact production runtime) — ${root}`);
|
||||
await rebuild();
|
||||
const watchers: FSWatcher[] = [];
|
||||
for (const name of [
|
||||
"app",
|
||||
"public",
|
||||
"wrnexus.config.ts",
|
||||
"wrnexus.config.js",
|
||||
"wrnexus.config.mjs",
|
||||
]) {
|
||||
const target = join(root, name);
|
||||
if (!existsSync(target)) continue;
|
||||
watchers.push(
|
||||
watch(target, { recursive: true }, () => {
|
||||
if (timer) clearTimeout(timer);
|
||||
timer = setTimeout(() => void rebuild(), 120);
|
||||
}),
|
||||
);
|
||||
}
|
||||
const shutdown = (): void => {
|
||||
watchers.forEach((watcher) => watcher.close());
|
||||
child?.kill();
|
||||
process.exit(0);
|
||||
};
|
||||
process.on("SIGINT", shutdown);
|
||||
process.on("SIGTERM", shutdown);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user