Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

config.ts

83 lines2.4 KB

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 appearanceConfigPath: string;
25 readonly stateDir: string;
26 readonly staticDir: string | undefined;
27 readonly devUrl: URL | undefined;
28 readonly noBrowser: boolean;
29 readonly authToken: string | undefined;
30 readonly autoBootstrapProjectFromCwd: boolean;
31 readonly logWebSocketEvents: boolean;
33​
34/**
35 * ServerConfig - Service tag for server runtime configuration.
36 */
37export class ServerConfig extends ServiceMap.Service<ServerConfig, ServerConfigShape>()(
38 "t3/config/ServerConfig",
39) {
40 static readonly layerTest = (cwd: string, statedir: string) =>
41 Layer.effect(
42 ServerConfig,
43 Effect.gen(function* () {
44 const path = yield* Path.Path;
45 return {
46 cwd,
47 stateDir: statedir,
48 mode: "web",
49 autoBootstrapProjectFromCwd: false,
50 logWebSocketEvents: false,
51 port: 0,
52 host: undefined,
53 authToken: undefined,
54 keybindingsConfigPath: path.join(statedir, "keybindings.json"),
55 appearanceConfigPath: path.join(statedir, "appearance.json"),
56 staticDir: undefined,
57 devUrl: undefined,
58 noBrowser: false,
59 };
60 }),
61 );
63​
64export const resolveStaticDir = Effect.fn(function* () {
65 const { join, resolve } = yield* Path.Path;
66 const { exists } = yield* FileSystem.FileSystem;
67 const bundledClient = resolve(join(import.meta.dirname, "client"));
68 const bundledStat = yield* exists(join(bundledClient, "index.html")).pipe(
69 Effect.orElseSucceed(() => false),
70 );
71 if (bundledStat) {
72 return bundledClient;
73 }
74​
75 const monorepoClient = resolve(join(import.meta.dirname, "../../web/dist"));
76 const monorepoStat = yield* exists(join(monorepoClient, "index.html")).pipe(
77 Effect.orElseSucceed(() => false),
78 );
79 if (monorepoStat) {
80 return monorepoClient;
81 }
82 return undefined;
83});