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

server.ts

75 lines2.9 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​
16const runServerCommand = Effect.fn("server.runServerCommand")(function* (
17 flags: CliServerFlags,
18 options?: {
19 readonly startupPresentation?: ServerConfig.StartupPresentation;
20 readonly forceAutoBootstrapProjectFromCwd?: boolean;
21 readonly rejectRunningServer?: boolean;
22 },
23) {
24 const logLevel = yield* GlobalFlag.LogLevel;
25 const config = yield* resolveServerConfig(flags, logLevel, options);
26 const run = runServer.pipe(Effect.provideService(ServerConfig.ServerConfig, config));
27 if (config.mode !== "desktop" || config.desktopBootstrapToken === undefined) return yield* run;
28 return yield* Effect.raceFirst(
29 run,
30 awaitLauncherLoss().pipe(
31 // End like SIGTERM does (interrupt-only exit, so the runtime forces process exit).
32 Effect.andThen(Effect.interrupt),
33 ),
34 );
35});
36​
37export const runDefaultServerCommand = Effect.fn("server.runDefaultServerCommand")(function* (
38 flags: CliServerFlags,
39) {
40 if (Option.isSome(flags.cwd)) {
41 const cwd = flags.cwd.value.trim();
42 const fs = yield* FileSystem.FileSystem;
43 const platform = yield* HostProcess.Platform;
44 const explicitPath =
45 cwd === "." ||
46 cwd === ".." ||
47 cwd === "~" ||
48 /[/\\]/.test(cwd) ||
49 (platform === "win32" && /^[a-z]:/i.test(cwd));
50 if (!explicitPath && (!(yield* fs.exists(cwd)) || (yield* fs.stat(cwd)).type !== "Directory")) {
51 return yield* new CliError.UserError({
52 cause: cwd,
53 userMessage: `Unknown command ${yield* encodeCommand(cwd)}. Use "t3 --help" for commands or an explicit path such as "t3 ./my-project" for a new directory.`,
54 });
55 }
56 }
57 return yield* runServerCommand(flags, { rejectRunningServer: true });
58});
59​
60export const startCommand = Command.make("start", { ...sharedServerCommandFlags }).pipe(
61 Command.withDescription("Run the T3 Code server."),
62 Command.withHandler((flags) => runServerCommand(flags, { rejectRunningServer: true })),
63);
64​
65export const serveCommand = Command.make("serve", { ...sharedServerCommandFlags }).pipe(
66 Command.withDescription(
67 "Run the T3 Code server without opening a browser and print headless pairing details.",
68 ),
69 Command.withHandler((flags) =>
70 runServerCommand(flags, {
71 startupPresentation: "headless",
72 forceAutoBootstrapProjectFromCwd: false,
73 }),
74 ),
75);