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