Skip to content

Commit bc84747

Browse files
committed
feat(client): add watcher
1 parent 16342a1 commit bc84747

File tree

4 files changed

+63
-4
lines changed

4 files changed

+63
-4
lines changed

apps/client/src/env.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,11 @@ export const getGameDir = () => {
1313
if (process.env.GAME_DIR) return process.env.GAME_DIR;
1414
throw new Error("GAME_DIR env variable not found");
1515
};
16+
17+
export const getWatch = () => {
18+
return process.env.WATCH;
19+
};
20+
21+
export const getWatchPort = () => {
22+
return process.env.WATCH_PORT;
23+
};

apps/client/src/server.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
11
import { join } from "node:path";
22

3-
import { getGameDir, getPort, getPublicEnv } from "./env";
3+
import { getGameDir, getPort, getPublicEnv, getWatch, getWatchPort } from "./env";
44
import { updateManifest } from "./manifest";
5+
import { startWatch } from "./watch";
56

6-
export const MANIFEST: { version: string; files: { path: string }[] } = {
7+
type IManifest = {
8+
version: string;
9+
files: { path: string }[];
10+
watch: { enable: false } | { enable: true; url: string };
11+
};
12+
13+
const watch = getWatch() === "true";
14+
15+
export const MANIFEST: IManifest = {
716
version: "",
817
files: [],
18+
watch: {
19+
enable: false,
20+
},
921
};
1022

1123
const resolveWebDir = (str: string) => {
@@ -68,4 +80,6 @@ const server = Bun.serve({
6880
},
6981
});
7082

71-
console.log(`Client started on port ${server.port}`);
83+
console.log(`Client started on url ${server.url.toString()}`);
84+
85+
if (watch) startWatch(gameDir, getWatchPort());

apps/client/src/watch.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { watch } from "fs";
2+
3+
import { MANIFEST } from "./server";
4+
5+
export const startWatch = (gameDir: string, port?: string) => {
6+
const server = Bun.serve({
7+
port: port ?? 0,
8+
fetch(req, server) {
9+
if (server.upgrade(req)) {
10+
return;
11+
}
12+
return new Response("Upgrade failed", { status: 500 });
13+
},
14+
websocket: {
15+
message(ws) {
16+
ws.send("not allowed");
17+
},
18+
open(ws) {
19+
ws.subscribe("watch");
20+
},
21+
close(ws) {
22+
ws.unsubscribe("watch");
23+
},
24+
},
25+
});
26+
27+
MANIFEST.watch = {
28+
enable: true,
29+
url: server.url.toString(),
30+
};
31+
32+
watch(gameDir, { recursive: true }, () => {
33+
server.publish("watch", "update");
34+
console.log(`Game updated`);
35+
});
36+
37+
console.log(`Watcher started on url ${server.url.toString()}`);
38+
};

tsconfig.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
// Type Checking
66
"allowUnreachableCode": false,
77
"allowUnusedLabels": false,
8-
"exactOptionalPropertyTypes": true,
98
"noFallthroughCasesInSwitch": true,
109
"noImplicitOverride": true,
1110
"noImplicitReturns": true,

0 commit comments

Comments
 (0)