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

server.ts

86 lines3.4 KB

1import * as HostProcess from "@t3tools/shared/HostProcess";
2import * as Effect from "effect/Effect";
3import * as FileSystem from "effect/FileSystem";
4import * as Option from "effect/Option";
5import * as Schema from "effect/Schema";
6import { Command, GlobalFlag } from "effect/cli";
7import * as CliError from "effect/cli/CliError";
8​
9import * as ServerConfig from "../config.ts";
10import { awaitLauncherLoss } from "../launcherWatch.ts";
11import { runServer } from "../server.ts";
12import { type CliServerFlags, resolveServerConfig, sharedServerCommandFlags } from "./config.ts";
13​
14const encodeCommand = Schema.encodeEffect(Schema.fromJsonString(Schema.String));
15​
16// The desktop launcher reads this process's stdout and stderr. Once it is gone,
17// every log write fails, and Node turns an unhandled stream error into a crash
18// that skips shutdown and leaves the ownership lock and runtime record behind.
19function ignoreClosedLauncherPipe(error: NodeJS.ErrnoException): void {
20 if (error.code !== "EPIPE" && error.code !== "ERR_STREAM_DESTROYED") throw error;
22​
23const runServerCommand = Effect.fn("server.runServerCommand")(function* (
24 flags: CliServerFlags,
25 options?: {
26 readonly startupPresentation?: ServerConfig.StartupPresentation;
27 readonly forceAutoBootstrapProjectFromCwd?: boolean;
28 readonly rejectRunningServer?: boolean;
29 },
30) {
31 const logLevel = yield* GlobalFlag.LogLevel;
32 const config = yield* resolveServerConfig(flags, logLevel, options);
33 const run = runServer.pipe(Effect.provideService(ServerConfig.ServerConfig, config));
34 if (config.mode !== "desktop" || config.desktopBootstrapToken === undefined) return yield* run;
35 yield* Effect.sync(() => {
36 process.stdout.on("error", ignoreClosedLauncherPipe);
37 process.stderr.on("error", ignoreClosedLauncherPipe);
38 });
39 return yield* Effect.raceFirst(
40 run,
41 awaitLauncherLoss().pipe(
42 // End like SIGTERM does (interrupt-only exit, so the runtime forces process exit).
43 Effect.andThen(Effect.interrupt),
44 ),
45 );
46});
47​
48export const runDefaultServerCommand = Effect.fn("server.runDefaultServerCommand")(function* (
49 flags: CliServerFlags,
50) {
51 if (Option.isSome(flags.cwd)) {
52 const cwd = flags.cwd.value.trim();
53 const fs = yield* FileSystem.FileSystem;
54 const platform = yield* HostProcess.Platform;
55 const explicitPath =
56 cwd === "." ||
57 cwd === ".." ||
58 cwd === "~" ||
59 /[/\\]/.test(cwd) ||
60 (platform === "win32" && /^[a-z]:/i.test(cwd));
61 if (!explicitPath && (!(yield* fs.exists(cwd)) || (yield* fs.stat(cwd)).type !== "Directory")) {
62 return yield* new CliError.UserError({
63 cause: cwd,
64 userMessage: `Unknown command ${yield* encodeCommand(cwd)}. Use "t3 --help" for commands or an explicit path such as "t3 ./my-project" for a new directory.`,
65 });
66 }
67 }
68 return yield* runServerCommand(flags, { rejectRunningServer: true });
69});
70​
71export const startCommand = Command.make("start", { ...sharedServerCommandFlags }).pipe(
72 Command.withDescription("Run the T3 Code server."),
73 Command.withHandler((flags) => runServerCommand(flags, { rejectRunningServer: true })),
74);
75​
76export const serveCommand = Command.make("serve", { ...sharedServerCommandFlags }).pipe(
77 Command.withDescription(
78 "Run the T3 Code server without opening a browser and print headless pairing details.",
79 ),
80 Command.withHandler((flags) =>
81 runServerCommand(flags, {
82 startupPresentation: "headless",
83 forceAutoBootstrapProjectFromCwd: false,
84 }),
85 ),
86);