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 { awaitServiceLauncherStop } from "../cloud/serviceLauncherClient.ts";
10import * as ServerConfig from "../config.ts";
11import { stopOnLauncherLoss } from "../launcherWatch.ts";
12import * as DatabaseMaintenance from "../persistence/DatabaseMaintenance.ts";
13import * as ProcessRunner from "../processRunner.ts";
14import * as AutomaticPort from "./automaticPort.ts";
15import {
16 type CliServerFlags,
17 normalizeServerCwdArgument,
18 resolveServerConfig,
19 sharedServerCommandFlags,
20} from "./config.ts";
21
22const encodeCommand = Schema.encodeEffect(Schema.fromJsonString(Schema.String));
23
24// Untraced: a span that lasts the whole process would parent everything the server does.
25const runServerCommand = Effect.fnUntraced(function* (
26 flags: CliServerFlags,
27 options?: {
28 readonly startupPresentation?: ServerConfig.StartupPresentation;
29 readonly forceAutoBootstrapProjectFromCwd?: boolean;
30 readonly rejectRunningServer?: boolean;
31 },
32) {
33 // Before config resolution, which claims a port and creates directories.
34 const { runServer } = yield* Effect.promise(() => import("../server.ts"));
35 const logLevel = yield* GlobalFlag.LogLevel;
36 const selectAutomaticPort = yield* AutomaticPort.make.pipe(Effect.provide(ProcessRunner.layer));
37 const config = yield* resolveServerConfig(flags, logLevel, { ...options, selectAutomaticPort });
38 const run = Effect.raceFirst(
39 runServer.pipe(Effect.provideService(ServerConfig.ServerConfig, config)),
40 awaitServiceLauncherStop().pipe(Effect.andThen(Effect.interrupt)),
41 ).pipe(
42 // Settings' Reclaim stops the server; repair once the database is closed, before the restart.
43 Effect.ensuring(
44 DatabaseMaintenance.runRequestedReclaim(config.dbPath).pipe(
45 Effect.provide(DatabaseMaintenance.layer),
46 ),
47 ),
48 );
49 if (config.mode !== "desktop" || config.desktopBootstrapToken === undefined) return yield* run;
50 return yield* stopOnLauncherLoss(run);
51});
52
53export const runDefaultServerCommand = Effect.fnUntraced(function* (flags: CliServerFlags) {
54 if (Option.isSome(flags.cwd)) {
55 const cwd = yield* normalizeServerCwdArgument(flags.cwd.value);
56 const fs = yield* FileSystem.FileSystem;
57 const platform = yield* HostProcess.Platform;
58 const explicitPath =
59 cwd === "." ||
60 cwd === ".." ||
61 cwd === "~" ||
62 /[/\\]/.test(cwd) ||
63 (platform === "win32" && /^[a-z]:/i.test(cwd));
64 if (!explicitPath && (!(yield* fs.exists(cwd)) || (yield* fs.stat(cwd)).type !== "Directory")) {
65 return yield* new CliError.UserError({
66 cause: cwd,
67 userMessage: `Unknown command ${yield* encodeCommand(cwd)}. Use "t3 --help" for commands or an explicit path such as "t3 ./my-project" for a new directory.`,
68 });
69 }
70 }
71 return yield* runServerCommand(flags, { rejectRunningServer: true });
72});
73
74export const startCommand = Command.make("start", { ...sharedServerCommandFlags }).pipe(
75 Command.withDescription("Same as `t3`."),
76 Command.unlisted,
77 Command.withHandler((flags) => runServerCommand(flags, { rejectRunningServer: true })),
78);
79
80// Serve never bootstraps a project, so it does not offer the flag that asks for one.
81const { autoBootstrapProjectFromCwd: _, ...serveCommandFlags } = sharedServerCommandFlags;
82
83export const serveCommand = Command.make("serve", serveCommandFlags).pipe(
84 Command.withShortDescription("Run T3 Code in this terminal, with no browser."),
85 Command.withDescription(
86 "Run T3 Code in this terminal without opening a browser, and print pairing details. The background service runs this too, without printing them: pair with `t3 pair`. To set up access from your other devices, run `t3 up`.",
87 ),
88 Command.withHandler((flags) =>
89 runServerCommand(
90 { ...flags, autoBootstrapProjectFromCwd: Option.none() },
91 {
92 startupPresentation: "headless",
93 forceAutoBootstrapProjectFromCwd: false,
94 },
95 ),
96 ),
97);