Open History to see the last commit
1/**
2 * ServerConfig - Runtime configuration services.
3 *
4 * Defines process-level server configuration and networking helpers used by
5 * startup and runtime layers.
6 *
7 * @module ServerConfig
8 */
9import { Effect, FileSystem, Layer, Path, ServiceMap } from "effect";
10
11export const DEFAULT_PORT = 3773;
12
13export type RuntimeMode = "web" | "desktop";
14
15/**
16 * ServerConfigShape - Process/runtime configuration required by the server.
17 */
18export interface ServerConfigShape {
19 readonly mode: RuntimeMode;
20 readonly port: number;
21 readonly host: string | undefined;
22 readonly cwd: string;
23 readonly keybindingsConfigPath: string;
24 readonly stateDir: string;
25 readonly staticDir: string | undefined;
26 readonly devUrl: URL | undefined;
27 readonly noBrowser: boolean;
28 readonly authToken: string | undefined;
29 readonly autoBootstrapProjectFromCwd: boolean;
30 readonly logWebSocketEvents: boolean;
31}
32
33/**
34 * ServerConfig - Service tag for server runtime configuration.
35 */
36export class ServerConfig extends ServiceMap.Service<ServerConfig, ServerConfigShape>()(
37 "t3/config/ServerConfig",
38) {
39 static readonly layerTest = (cwd: string, statedir: string) =>
40 Layer.effect(
41 ServerConfig,
42 Effect.gen(function* () {
43 const path = yield* Path.Path;
44 return {
45 cwd,
46 stateDir: statedir,
47 mode: "web",
48 autoBootstrapProjectFromCwd: false,
49 logWebSocketEvents: false,
50 port: 0,
51 host: undefined,
52 authToken: undefined,
53 keybindingsConfigPath: path.join(statedir, "keybindings.json"),
54 staticDir: undefined,
55 devUrl: undefined,
56 noBrowser: false,
57 };
58 }),
59 );
60}
61
62export const resolveStaticDir = Effect.fn(function* () {
63 const { join, resolve } = yield* Path.Path;
64 const { exists } = yield* FileSystem.FileSystem;
65 const bundledClient = resolve(join(import.meta.dirname, "client"));
66 const bundledStat = yield* exists(join(bundledClient, "index.html")).pipe(
67 Effect.orElseSucceed(() => false),
68 );
69 if (bundledStat) {
70 return bundledClient;
71 }
72
73 const monorepoClient = resolve(join(import.meta.dirname, "../../web/dist"));
74 const monorepoStat = yield* exists(join(monorepoClient, "index.html")).pipe(
75 Effect.orElseSucceed(() => false),
76 );
77 if (monorepoStat) {
78 return monorepoClient;
79 }
80 return undefined;
81});